diff --git a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php index 0daa060b7c6..8aaf4e922c5 100644 --- a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php +++ b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php @@ -819,7 +819,7 @@ private function specifyEqualAgainstConstantSide( if (in_array($funcName, ['gettype', 'get_class', 'get_debug_type'], true) && $constantType->isString()->yes()) { return $this->specifyIdentical($nodeScopeResolver, $left, $right, $leftResult, $rightResult, $context, $evaluationScope, $leftArgResult, $rightArgResult, $identicalTypeCallback); } - if ($context->true() && $funcName === 'preg_match' && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes()) { + if ($value === 1 && $this->isOnlyTruthyResultOne($subjectResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted))) { return $this->specifyIdentical($nodeScopeResolver, $left, $right, $leftResult, $rightResult, $context, $evaluationScope, $leftArgResult, $rightArgResult, $identicalTypeCallback); } } @@ -835,6 +835,15 @@ private function specifyEqualAgainstConstantSide( return false; } + private function isOnlyTruthyResultOne(Type $callType): bool + { + if ($callType->toBoolean()->isFalse()->yes()) { + return false; + } + + return TypeCombinator::remove($callType, new ConstantIntegerType(1))->toBoolean()->isFalse()->yes(); + } + /** * The function-family compositions, shared by the literal and the * TYPE-based constant sides: a family answer, null to fall back to the @@ -858,16 +867,13 @@ private function specifyFuncCallFamilies( return false; } - // preg_match(...) === 1 is the call's own truthy narrowing - the - // type-specifying extensions narrow the by-ref \$matches argument + // f(...) === 1 narrows like the truthy call when 1 is its only truthy result if ( - $call->name->toLowerString() === 'preg_match' + $context->true() + && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() + && $this->isOnlyTruthyResultOne($subjectResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted)) ) { - if ($context->true() && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes()) { - return $subjectResult->getSpecifiedTypesForScope($evaluationScope, $context); - } - - // other constants and contexts only pin the call below + return $subjectResult->getSpecifiedTypesForScope($evaluationScope, $context); } // a trimmed string that is not '' was a non-empty string already diff --git a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCall.neon b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCall.neon new file mode 100644 index 00000000000..56c47ab3ed3 --- /dev/null +++ b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCall.neon @@ -0,0 +1,5 @@ +services: + - + class: PHPStan\Tests\MatchesIntFunctionTypeSpecifyingExtension + tags: + - phpstan.typeSpecifier.functionTypeSpecifyingExtension diff --git a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php new file mode 100644 index 00000000000..83e2c66ecde --- /dev/null +++ b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php @@ -0,0 +1,27 @@ += 8.0.0')] + public function testAsserts(): void + { + // gathered here instead of in a data provider, so that mutation testing sees the coverage + foreach (self::gatherAssertTypes(__DIR__ . '/data/type-specifying-extensions-identical-function-call.php') as $args) { + $this->assertFileAsserts(...$args); + } + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/TypeSpecifyingExtensionIdenticalFunctionCall.neon', + ]; + } + +} diff --git a/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php b/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php new file mode 100644 index 00000000000..c0fa1e8a052 --- /dev/null +++ b/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php @@ -0,0 +1,135 @@ += 8.0 + +namespace IdenticalFunctionCallExtension; + +use function PHPStan\Testing\assertType; + +/** @return 0|1 */ +function matchesInt(mixed $value): int +{ + return is_int($value) ? 1 : 0; +} + +/** @return 0|1|false */ +function matchesIntOrFails(mixed $value): int|false +{ + return is_int($value) ? 1 : 0; +} + +function matchesIntOrFalse(mixed $value): int|false +{ + return is_int($value) ? 1 : false; +} + +/** @return 0 */ +function neverMatches(mixed $value): int +{ + return 0; +} + +function truthy(mixed $value): void +{ + if (matchesInt($value)) { + assertType('int', $value); + } +} + +function identicalToOne(mixed $value): void +{ + if (matchesInt($value) === 1) { + assertType('int', $value); + } + if (1 === matchesInt($value)) { + assertType('int', $value); + } + if (matchesIntOrFails($value) === 1) { + assertType('int', $value); + } +} + +function looselyEqualToOne(mixed $value): void +{ + if (matchesInt($value) == 1) { + assertType('int', $value); + } + if (1 == matchesIntOrFails($value)) { + assertType('int', $value); + } +} + +function notIdenticalToOne(mixed $value): void +{ + if (matchesInt($value) !== 1) { + return; + } + assertType('int', $value); +} + +function notIdenticalToOneIsTrue(mixed $value): void +{ + if ((matchesInt($value) !== 1) === true) { + assertType('mixed', $value); + } + if ((matchesInt($value) != 1) === true) { + assertType('mixed', $value); + } +} + +function otherConstants(mixed $value): void +{ + if (matchesInt($value) === 0) { + assertType('mixed', $value); + } + if (matchesIntOrFalse($value) === 1) { + assertType('mixed', $value); + } + if (matchesIntOrFalse($value) == 1) { + assertType('mixed', $value); + } + if (matchesInt($value) === 2) { + assertType('mixed', $value); + } + if (neverMatches($value) === 1) { + assertType('mixed', $value); + } + if (neverMatches($value) == 1) { + assertType('mixed', $value); + } +} + +function looselyEqualToZero(mixed $value): void +{ + if (matchesIntOrFails($value) == 0) { + assertType('0|false', matchesIntOrFails($value)); + } +} + +function constantsIncludingOne(mixed $value, bool $flag): void +{ + $oneOrTwo = $flag ? 1 : 2; + if (matchesInt($value) === $oneOrTwo) { + assertType('mixed', $value); + } + if (matchesInt($value) == $oneOrTwo) { + assertType('mixed', $value); + } +} + +/** + * @return 0|1 + * @phpstan-assert-if-true int $value + */ +function isIntish(mixed $value): int +{ + return is_int($value) ? 1 : 0; +} + +function assertIfTrue(mixed $value): void +{ + if (isIntish($value) === 1) { + assertType('int', $value); + } + if (isIntish($value) == 1) { + assertType('int', $value); + } +} diff --git a/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php b/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php new file mode 100644 index 00000000000..42035c2f0c9 --- /dev/null +++ b/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php @@ -0,0 +1,36 @@ +typeSpecifier = $typeSpecifier; + } + + public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool + { + return in_array($functionReflection->getName(), ['IdenticalFunctionCallExtension\\matchesInt', 'IdenticalFunctionCallExtension\\matchesIntOrFails', 'IdenticalFunctionCallExtension\\matchesIntOrFalse', 'IdenticalFunctionCallExtension\\neverMatches'], true) && !$context->null(); + } + + public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes + { + return $this->typeSpecifier->create($node->getArgs()[0]->value, new IntegerType(), $context, $scope); + } + +} diff --git a/turbo-ext/src/IdenticalNarrowingHelper.cpp b/turbo-ext/src/IdenticalNarrowingHelper.cpp index 9c917270c48..a3d3561ab10 100644 --- a/turbo-ext/src/IdenticalNarrowingHelper.cpp +++ b/turbo-ext/src/IdenticalNarrowingHelper.cpp @@ -367,6 +367,28 @@ zv::Val constantStringValue(zval *constantString) return true; } +/* the twin's isOnlyTruthyResultOne(): $callType can be truthy and + * removing 1 leaves only falsy results; false = pending exception */ +[[nodiscard]] bool isOnlyTruthyResultOne(zval *callType, bool &out) +{ + out = false; + zv::Val callBool = typeCall(callType, "toBoolean", PT_LC("toboolean")); + if (UNEXPECTED(callBool.isUndef())) return false; + zend_long alwaysFalse = PT_INH_IS_FALSE(callBool.raw()); + if (UNEXPECTED(alwaysFalse < 0)) return false; + if (alwaysFalse == PT_TRI_YES) return true; + zv::Val one = newConstantInteger(1); + if (UNEXPECTED(one.isUndef())) return false; + zv::Val remaining = pt_type_combinator_remove(callType, one.raw()); + if (UNEXPECTED(remaining.isUndef())) return false; + zv::Val remainingBool = typeCall(remaining.raw(), "toBoolean", PT_LC("toboolean")); + if (UNEXPECTED(remainingBool.isUndef())) return false; + zend_long isFalse = PT_INH_IS_FALSE(remainingBool.raw()); + if (UNEXPECTED(isFalse < 0)) return false; + out = isFalse == PT_TRI_YES; + return true; +} + /* IntegerRangeType::fromInterval($min, $max)->isSuperTypeOf($type)->yes() */ [[nodiscard]] bool rangeContains(phpstanturbo::NullableLong min, phpstanturbo::NullableLong max, zval *type, bool &out) { @@ -1370,12 +1392,12 @@ class IdenticalNarrowingHelper if (UNEXPECTED(isString < 0)) return zv::Val(); if (isString == PT_TRI_YES) return specifyIdentical(nodeScopeResolver, left, right, leftResult, rightResult, context, evaluationScope, leftArgResult, rightArgResult, identicalTypeCallback); } - bool isTrue; - if (UNEXPECTED(!ctxTrue(context, isTrue))) return zv::Val(); - if (isTrue && funcName != NULL && lowerNameIs(funcName, "preg_match")) { - bool one; - if (UNEXPECTED(!constantIntegerContains(1, constantType, one))) return zv::Val(); - if (one) return specifyIdentical(nodeScopeResolver, left, right, leftResult, rightResult, context, evaluationScope, leftArgResult, rightArgResult, identicalTypeCallback); + if (Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) == 1) { + zv::Val callType = typeOnScope(subjectResult, evaluationScope); + if (UNEXPECTED(callType.isUndef())) return zv::Val(); + bool onlyTruthyOne; + if (UNEXPECTED(!isOnlyTruthyResultOne(callType.raw(), onlyTruthyOne))) return zv::Val(); + if (onlyTruthyOne) return specifyIdentical(nodeScopeResolver, left, right, leftResult, rightResult, context, evaluationScope, leftArgResult, rightArgResult, identicalTypeCallback); } } } @@ -1437,17 +1459,19 @@ class IdenticalNarrowingHelper zend_string *name = identifierString(nameOf(call)); if (UNEXPECTED(name == NULL)) return zv::Val::boolean(false); - // preg_match(...) === 1 is the call's own truthy narrowing - if (lowerNameIs(name, "preg_match")) { - bool isTrue; - if (UNEXPECTED(!ctxTrue(context, isTrue))) return zv::Val(); - if (isTrue) { - bool one; - if (UNEXPECTED(!constantIntegerContains(1, constantType, one))) return zv::Val(); - if (one) return pt_expression_result_get_specified_types_for_scope(subjectResult, evaluationScope, context); + // f(...) === 1 narrows like the truthy call when 1 is its only truthy result + bool isTrue; + if (UNEXPECTED(!ctxTrue(context, isTrue))) return zv::Val(); + if (isTrue) { + bool one; + if (UNEXPECTED(!constantIntegerContains(1, constantType, one))) return zv::Val(); + if (one) { + zv::Val callType = typeOnScope(subjectResult, evaluationScope); + if (UNEXPECTED(callType.isUndef())) return zv::Val(); + bool onlyTruthyOne; + if (UNEXPECTED(!isOnlyTruthyResultOne(callType.raw(), onlyTruthyOne))) return zv::Val(); + if (onlyTruthyOne) return pt_expression_result_get_specified_types_for_scope(subjectResult, evaluationScope, context); } - - // other constants and contexts only pin the call below } // a trimmed string that is not '' was a non-empty string already diff --git a/turbo-ext/src/generated/IdenticalNarrowingHelper.h b/turbo-ext/src/generated/IdenticalNarrowingHelper.h index 5271c7958bd..a17350c041e 100644 --- a/turbo-ext/src/generated/IdenticalNarrowingHelper.h +++ b/turbo-ext/src/generated/IdenticalNarrowingHelper.h @@ -83,18 +83,20 @@ inline constexpr char strings[] = "subjectArgResult\0" /* 936 */ "specifyIdenticalAgainstType\0" /* 953 */ "specifyEqualAgainstConstantSide\0" /* 981 */ - "call\0" /* 1013 */ - "PhpParser\\Node\\Expr\\FuncCall\0" /* 1018 */ - "argResult\0" /* 1047 */ - "specifyFuncCallFamilies\0" /* 1057 */ - "side\0" /* 1081 */ - "storage\0" /* 1086 */ - "PHPStan\\Analyser\\ExpressionResultStorage\0" /* 1094 */ - "captureFirstArgResult\0" /* 1135 */ - "expr\0" /* 1157 */ - "literalType\0" /* 1162 */ - "isScalarLiteral\0" /* 1174 */ - "isSubjectCoveredAgainstConstant"; /* 1190 */ + "callType\0" /* 1013 */ + "isOnlyTruthyResultOne\0" /* 1022 */ + "call\0" /* 1044 */ + "PhpParser\\Node\\Expr\\FuncCall\0" /* 1049 */ + "argResult\0" /* 1078 */ + "specifyFuncCallFamilies\0" /* 1088 */ + "side\0" /* 1112 */ + "storage\0" /* 1117 */ + "PHPStan\\Analyser\\ExpressionResultStorage\0" /* 1125 */ + "captureFirstArgResult\0" /* 1166 */ + "expr\0" /* 1188 */ + "literalType\0" /* 1193 */ + "isScalarLiteral\0" /* 1205 */ + "isSubjectCoveredAgainstConstant"; /* 1221 */ inline constexpr reg::PackedArg args[] = { reg::packed(0, 0, 23), /* __construct $defaultNarrowingHelper */ reg::packed(82, 0, 101), /* __construct $reflectionProvider */ @@ -190,21 +192,23 @@ inline constexpr reg::PackedArg args[] = { reg::packed(589, MAY_BE_NULL, 436), /* specifyEqualAgainstConstantSide $rightArgResult */ reg::packed(604, MAY_BE_CALLABLE), /* specifyEqualAgainstConstantSide $identicalTypeCallback */ reg::packed(626, MAY_BE_NULL | MAY_BE_FALSE, 627), /* specifyEqualAgainstConstantSide return */ + reg::packed(1013, 0, 776), /* isOnlyTruthyResultOne $callType */ + reg::packed(626, MAY_BE_BOOL), /* isOnlyTruthyResultOne return */ reg::packed(676, 0, 399), /* specifyFuncCallFamilies $subject */ reg::packed(684, 0, 436), /* specifyFuncCallFamilies $subjectResult */ - reg::packed(1013, 0, 1018), /* specifyFuncCallFamilies $call */ + reg::packed(1044, 0, 1049), /* specifyFuncCallFamilies $call */ reg::packed(910, 0, 399), /* specifyFuncCallFamilies $constantExpr */ reg::packed(923, 0, 776), /* specifyFuncCallFamilies $constantType */ reg::packed(482, 0, 490), /* specifyFuncCallFamilies $context */ reg::packed(528, 0, 544), /* specifyFuncCallFamilies $evaluationScope */ - reg::packed(1047, MAY_BE_NULL, 436), /* specifyFuncCallFamilies $argResult */ + reg::packed(1078, MAY_BE_NULL, 436), /* specifyFuncCallFamilies $argResult */ reg::packed(626, MAY_BE_NULL | MAY_BE_FALSE, 627), /* specifyFuncCallFamilies return */ - reg::packed(1081, 0, 399), /* captureFirstArgResult $side */ - reg::packed(1086, 0, 1094), /* captureFirstArgResult $storage */ + reg::packed(1112, 0, 399), /* captureFirstArgResult $side */ + reg::packed(1117, 0, 1125), /* captureFirstArgResult $storage */ reg::packed(626, MAY_BE_NULL, 436), /* captureFirstArgResult return */ - reg::packed(1157, 0, 399), /* literalType $expr */ + reg::packed(1188, 0, 399), /* literalType $expr */ reg::packed(626, MAY_BE_NULL, 776), /* literalType return */ - reg::packed(1157, 0, 399), /* isScalarLiteral $expr */ + reg::packed(1188, 0, 399), /* isScalarLiteral $expr */ reg::packed(626, MAY_BE_BOOL), /* isScalarLiteral return */ reg::packed(676, 0, 399), /* isSubjectCoveredAgainstConstant $subject */ reg::packed(626, MAY_BE_BOOL), /* isSubjectCoveredAgainstConstant return */ @@ -225,11 +229,12 @@ inline constexpr sigtab::Sig specifyEqual = { { 839 /* specifyEqual */, 9, 52, 9 inline constexpr sigtab::Sig createLooselyEqualTypes = { { 886 /* createLooselyEqualTypes */, 7, 62, 7, 69, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig specifyIdenticalAgainstType = { { 953 /* specifyIdenticalAgainstType */, 8, 70, 8, 78, ZEND_ACC_PUBLIC } }; inline constexpr sigtab::Sig specifyEqualAgainstConstantSide = { { 981 /* specifyEqualAgainstConstantSide */, 14, 79, 14, 93, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig specifyFuncCallFamilies = { { 1057 /* specifyFuncCallFamilies */, 8, 94, 8, 102, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig captureFirstArgResult = { { 1135 /* captureFirstArgResult */, 2, 103, 2, 105, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig literalType = { { 1162 /* literalType */, 1, 106, 1, 107, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isScalarLiteral = { { 1174 /* isScalarLiteral */, 1, 108, 1, 109, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isSubjectCoveredAgainstConstant = { { 1190 /* isSubjectCoveredAgainstConstant */, 1, 110, 1, 111, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isOnlyTruthyResultOne = { { 1022 /* isOnlyTruthyResultOne */, 1, 94, 1, 95, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig specifyFuncCallFamilies = { { 1088 /* specifyFuncCallFamilies */, 8, 96, 8, 104, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig captureFirstArgResult = { { 1166 /* captureFirstArgResult */, 2, 105, 2, 107, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig literalType = { { 1193 /* literalType */, 1, 108, 1, 109, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isScalarLiteral = { { 1205 /* isScalarLiteral */, 1, 110, 1, 111, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isSubjectCoveredAgainstConstant = { { 1221 /* isSubjectCoveredAgainstConstant */, 1, 112, 1, 113, ZEND_ACC_PRIVATE } }; } // namespace sig } // namespace ptdecl::IdenticalNarrowingHelper