3030use PHPStan \Type \UnionType ;
3131use function array_key_exists ;
3232use function array_merge ;
33- use function count ;
3433use function hexdec ;
3534use function is_int ;
3635use 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}
0 commit comments