Skip to content

Commit 6cde0aa

Browse files
committed
Refactor thanks to @VincentLanglet + add new test cases.
1 parent eaa26a9 commit 6cde0aa

2 files changed

Lines changed: 60 additions & 49 deletions

File tree

‎src/Type/Php/FilterFunctionReturnTypeHelper.php‎

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use PHPStan\Type\UnionType;
3131
use function array_key_exists;
3232
use function array_merge;
33-
use function count;
3433
use function hexdec;
3534
use function is_int;
3635
use function octdec;
@@ -193,7 +192,8 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
193192
}
194193

195194
if ($exactType === null || $hasOptions->maybe() || (!$inputType->equals($type) && $inputType->isSuperTypeOf($type)->yes())) {
196-
if (!$defaultType->isSuperTypeOf($type)->yes()) {
195+
// Default type is handled as the exactType when flag FILTER_FLAG_EMPTY_STRING_NULL is set.
196+
if (!$defaultType->isSuperTypeOf($type)->yes() && $this->hasFlag('FILTER_FLAG_EMPTY_STRING_NULL', $flagsType)->no()) {
197197
$type = TypeCombinator::union($type, $defaultType);
198198
}
199199
}
@@ -209,7 +209,7 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
209209
return new ArrayType($inputArrayKeyType ?? $mixedType, $type);
210210
}
211211

212-
if ($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->yes()) {
212+
if ($this->isValidationFilter($filterValue) && $this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->yes()) {
213213
$type = TypeCombinator::remove($type, $defaultType);
214214
}
215215

@@ -390,55 +390,44 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
390390
}
391391

392392
if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
393-
if ($this->hasFlag('FILTER_FLAG_EMPTY_STRING_NULL', $flagsType)->yes()) {
394-
if ($in->isFalse()->yes() || $in->isNull()->yes()) {
395-
return new NullType();
393+
$scalarOrNull = new UnionType([
394+
new StringType(),
395+
new FloatType(),
396+
new BooleanType(),
397+
new IntegerType(),
398+
new NullType(),
399+
]);
400+
401+
if ($scalarOrNull->isSuperTypeOf($in)->yes()) {
402+
$canBeSanitized = $this->canStringBeSanitized($filterValue, $flagsType);
403+
if ($canBeSanitized->no()) {
404+
$stringType = $in->toString();
405+
} else {
406+
$stringType = $in->isString()->no()
407+
? $in->toString()
408+
: TypeCombinator::union(TypeCombinator::remove($in, new StringType()), new StringType());
396409
}
397410

398-
if ($in->isFloat()->yes() || $in->isInteger()->yes() || $in->isTrue()->yes()) {
399-
return $in->toString();
400-
}
401-
402-
if ($in->isString()->yes()) {
403-
if ($in->isNonEmptyString()->yes()) {
404-
return $in;
405-
}
406-
407-
if ($in->isNonEmptyString()->maybe()) {
408-
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
409-
}
410-
411-
if ($in->isNonEmptyString()->no()) {
412-
return new NullType();
413-
}
414-
}
415-
416-
$inString = $in->toString();
417-
if ($inString instanceof UnionType) {
418-
return $inString->traverse(
419-
static function (Type $type): Type {
420-
$typeConstantStrings = $type->getConstantStrings();
421-
if (count($typeConstantStrings) === 1 && $typeConstantStrings[0]->getValue() === '') {
422-
return new NullType();
423-
}
424-
return $type;
425-
},
426-
);
427-
}
428-
429-
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
411+
return $this->handleEmptyStringNullFlag($stringType, $flagsType);
430412
}
413+
}
431414

432-
if ($this->canStringBeSanitized($filterValue, $flagsType)->no() && $in->isString()->yes()) {
433-
return $in;
434-
}
415+
return null;
416+
}
435417

436-
if ($in->isBoolean()->yes() || $in->isFloat()->yes() || $in->isInteger()->yes() || $in->isNull()->yes()) {
437-
return $in->toString();
438-
}
418+
private function handleEmptyStringNullFlag(Type $in, ?Type $flagsType): Type
419+
{
420+
$hasFlag = $this->hasFlag('FILTER_FLAG_EMPTY_STRING_NULL', $flagsType);
421+
if ($hasFlag->no()) {
422+
return $in;
439423
}
440424

441-
return null;
425+
$hasEmptyString = !$in->isSuperTypeOf(new ConstantStringType(''))->no();
426+
if ($hasFlag->maybe()) {
427+
return $hasEmptyString ? TypeCombinator::addNull($in) : $in;
428+
}
429+
430+
return $hasEmptyString ? TypeCombinator::remove(TypeCombinator::addNull($in), new ConstantStringType('')) : $in;
442431
}
443432

444433
/** @param array<string, ?Type> $typeOptions */
@@ -572,7 +561,7 @@ private function getFlagsValue(Type $exprType): Type
572561
private function canStringBeSanitized(int $filterValue, ?Type $flagsType): TrinaryLogic
573562
{
574563
// If it is a validation filter, the string will not be changed
575-
if (($filterValue & self::VALIDATION_FILTER_BITMASK) !== 0) {
564+
if ($this->isValidationFilter($filterValue)) {
576565
return TrinaryLogic::createNo();
577566
}
578567

@@ -587,4 +576,9 @@ private function canStringBeSanitized(int $filterValue, ?Type $flagsType): Trina
587576
return TrinaryLogic::createYes();
588577
}
589578

579+
private function isValidationFilter(int $filterValue): bool
580+
{
581+
return ($filterValue & self::VALIDATION_FILTER_BITMASK) !== 0;
582+
}
583+
590584
}

‎tests/PHPStan/Analyser/nsrt/filter-var-returns-non-empty-string.php‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function run(
2727
bool $bool,
2828
): void
2929
{
30+
$object = (object)[];
31+
3032
assertType('non-empty-string', $str);
3133

3234
$return = filter_var($str, FILTER_DEFAULT);
@@ -38,14 +40,23 @@ public function run(
3840
$return = filter_var($maybe_empty_string, FILTER_DEFAULT, FILTER_FLAG_EMPTY_STRING_NULL);
3941
assertType('non-empty-string|null', $return);
4042

43+
$return = filter_var($object, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW);
44+
assertType('false', $return);
45+
4146
$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW);
42-
assertType('string|false', $return);
47+
assertType('string', $return);
48+
49+
$return = filter_var($object, FILTER_DEFAULT, FILTER_FLAG_STRIP_HIGH);
50+
assertType('false', $return);
4351

4452
$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_STRIP_HIGH);
45-
assertType('string|false', $return);
53+
assertType('string', $return);
54+
55+
$return = filter_var($object, FILTER_DEFAULT, FILTER_FLAG_STRIP_BACKTICK);
56+
assertType('false', $return);
4657

4758
$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_STRIP_BACKTICK);
48-
assertType('string|false', $return);
59+
assertType('string', $return);
4960

5061
$return = filter_var($str, FILTER_VALIDATE_EMAIL);
5162
assertType('non-falsy-string|false', $return);
@@ -71,6 +82,9 @@ public function run(
7182
$return = filter_var($str, FILTER_SANITIZE_STRING);
7283
assertType('string|false', $return);
7384

85+
$return = filter_var($object, FILTER_SANITIZE_STRING);
86+
assertType('false', $return);
87+
7488
$return = filter_var($str, FILTER_VALIDATE_INT);
7589
assertType('int|false', $return);
7690

@@ -177,6 +191,9 @@ public function run(
177191
$return = filter_var($nullable_non_empty_string, options: FILTER_FLAG_EMPTY_STRING_NULL);
178192
assertType('non-empty-string|null', $return);
179193

194+
$return = filter_var($object, options: FILTER_FLAG_EMPTY_STRING_NULL);
195+
assertType('false', $return);
196+
180197
$return = filter_var($this->anyOf(0.0, true), options: FILTER_FLAG_EMPTY_STRING_NULL);
181198
assertType("'-0'|'0'|'1'", $return);
182199

0 commit comments

Comments
 (0)