From 9880a168081ba31ff121f0c0c9f33b8347823cec Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 13 Nov 2026 11:41:04 +0100 Subject: [PATCH 1/4] Narrow arguments for `f(...) === 1` of any function with a type-specifying extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The identity comparison `preg_match(...) === 1` was forwarded to the function's type-specifying extensions only when the function was literally named `preg_match`, so wrappers such as `Safe\preg_match` kept `$matches` at `array{}|array{...}`. Forward `f(...) === 1` for any function whose call result is only truthy when it is 1 (e.g. `0|1` or `0|1|false`), where the comparison is equivalent to the call's truthiness. Other constants and result types are unchanged. 🤖 Generated with Claude Code --- .../Helper/IdenticalNarrowingHelper.php | 30 +++++--- ...cifyingExtensionIdenticalFunctionCall.neon | 5 ++ ...yingExtensionIdenticalFunctionCallTest.php | 36 +++++++++ ...ing-extensions-identical-function-call.php | 76 +++++++++++++++++++ ...chesIntFunctionTypeSpecifyingExtension.php | 36 +++++++++ 5 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCall.neon create mode 100644 tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php create mode 100644 tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php create mode 100644 tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php diff --git a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php index 0daa060b7c6..804006cb141 100644 --- a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php +++ b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php @@ -819,7 +819,11 @@ 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 ( + $context->true() + && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() + && $this->isOnlyTruthyResultOne($subjectResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted)) + ) { return $this->specifyIdentical($nodeScopeResolver, $left, $right, $leftResult, $rightResult, $context, $evaluationScope, $leftArgResult, $rightArgResult, $identicalTypeCallback); } } @@ -835,6 +839,16 @@ private function specifyEqualAgainstConstantSide( return false; } + private function isOnlyTruthyResultOne(Type $callType): bool + { + $one = new ConstantIntegerType(1); + if ($one->isSuperTypeOf($callType)->no()) { + return false; + } + + return TypeCombinator::remove($callType, $one)->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 +872,14 @@ 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 is the call's own truthy narrowing when 1 is its only truthy result + // - the type-specifying extensions narrow arguments such as the by-ref \$matches 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..ca251c6d2bd --- /dev/null +++ b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php @@ -0,0 +1,36 @@ +assertFileAsserts($assertType, $file, ...$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..e221f56eeeb --- /dev/null +++ b/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php @@ -0,0 +1,76 @@ += 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; +} + +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 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); + } +} diff --git a/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php b/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php new file mode 100644 index 00000000000..101a839823d --- /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'], 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); + } + +} From 198d4d5a77011bf3c90a5b53722e20b83d72661b Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 13 Nov 2026 12:30:16 +0100 Subject: [PATCH 2/4] Mirror isOnlyTruthyResultOne() in the turbo extension, skip test on PHP 7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Port the type check to turbo-ext/src/IdenticalNarrowingHelper.cpp for both the === and == paths and regenerate its declarations - Skip the new type inference test below PHP 8.0, as its data file uses mixed and union types - Cover constants that include 1 and @phpstan-assert-if-true functions 🤖 Generated with Claude Code --- .../Helper/IdenticalNarrowingHelper.php | 3 +- ...yingExtensionIdenticalFunctionCallTest.php | 5 ++ ...ing-extensions-identical-function-call.php | 30 +++++++++++ turbo-ext/src/IdenticalNarrowingHelper.cpp | 54 ++++++++++++++----- .../src/generated/IdenticalNarrowingHelper.h | 51 ++++++++++-------- 5 files changed, 106 insertions(+), 37 deletions(-) diff --git a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php index 804006cb141..5e625741961 100644 --- a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php +++ b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php @@ -872,8 +872,7 @@ private function specifyFuncCallFamilies( return false; } - // f(...) === 1 is the call's own truthy narrowing when 1 is its only truthy result - // - the type-specifying extensions narrow arguments such as the by-ref \$matches + // f(...) === 1 narrows like the truthy call when 1 is its only truthy result if ( $context->true() && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() diff --git a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php index ca251c6d2bd..b6285097547 100644 --- a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php +++ b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php @@ -4,12 +4,17 @@ use PHPStan\Testing\TypeInferenceTestCase; use PHPUnit\Framework\Attributes\DataProvider; +use const PHP_VERSION_ID; class TypeSpecifyingExtensionIdenticalFunctionCallTest extends TypeInferenceTestCase { public static function dataAsserts(): iterable { + if (PHP_VERSION_ID < 80000) { + return []; + } + yield from self::gatherAssertTypes(__DIR__ . '/data/type-specifying-extensions-identical-function-call.php'); } 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 index e221f56eeeb..00119bc6c4a 100644 --- a/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php +++ b/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php @@ -74,3 +74,33 @@ function otherConstants(mixed $value): void assertType('mixed', $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/turbo-ext/src/IdenticalNarrowingHelper.cpp b/turbo-ext/src/IdenticalNarrowingHelper.cpp index 9c917270c48..dffa668924c 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(): 1 is among $callType's results and + * removing it leaves only falsy ones; false = pending exception */ +[[nodiscard]] bool isOnlyTruthyResultOne(zval *callType, bool &out) +{ + out = false; + zv::Val one = newConstantInteger(1); + if (UNEXPECTED(one.isUndef())) return false; + zv::Val superType = pt_type_op(Z_OBJ_P(one.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, callType); + if (UNEXPECTED(superType.isUndef())) return false; + zend_long superValue = pt_type_result_trinary(superType.raw()); + if (UNEXPECTED(superValue < 0)) return false; + if (superValue == PT_TRI_NO) return true; + 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) { @@ -1372,10 +1394,16 @@ class IdenticalNarrowingHelper } bool isTrue; if (UNEXPECTED(!ctxTrue(context, isTrue))) return zv::Val(); - if (isTrue && funcName != NULL && lowerNameIs(funcName, "preg_match")) { + if (isTrue) { 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); + bool onlyTruthyOne = false; + if (one) { + zv::Val callType = typeOnScope(subjectResult, evaluationScope); + if (UNEXPECTED(callType.isUndef())) return zv::Val(); + 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 +1465,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 From d2995269b4d1e2f1157751c1af134279e13eac47 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 17 Nov 2026 10:45:52 +0100 Subject: [PATCH 3/4] Compare the loose == 1 value directly and cover the escaped mutants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `f(...) == 1` now checks the literal value and forwards to the strict comparison in every context, which applies its own context check. `isOnlyTruthyResultOne()` asks whether the call can be truthy instead of whether it can be 1, which is the same for the types that reach it. Tests cover `(f(...) !== 1) === true`, a call that is never truthy, and `f(...) == 0` staying loose. 🤖 Generated with Claude Code --- .../Helper/IdenticalNarrowingHelper.php | 11 ++----- ...ing-extensions-identical-function-call.php | 29 ++++++++++++++++++ ...chesIntFunctionTypeSpecifyingExtension.php | 2 +- turbo-ext/src/IdenticalNarrowingHelper.cpp | 30 ++++++++----------- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php index 5e625741961..8aaf4e922c5 100644 --- a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php +++ b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php @@ -819,11 +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() - && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() - && $this->isOnlyTruthyResultOne($subjectResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted)) - ) { + if ($value === 1 && $this->isOnlyTruthyResultOne($subjectResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted))) { return $this->specifyIdentical($nodeScopeResolver, $left, $right, $leftResult, $rightResult, $context, $evaluationScope, $leftArgResult, $rightArgResult, $identicalTypeCallback); } } @@ -841,12 +837,11 @@ private function specifyEqualAgainstConstantSide( private function isOnlyTruthyResultOne(Type $callType): bool { - $one = new ConstantIntegerType(1); - if ($one->isSuperTypeOf($callType)->no()) { + if ($callType->toBoolean()->isFalse()->yes()) { return false; } - return TypeCombinator::remove($callType, $one)->toBoolean()->isFalse()->yes(); + return TypeCombinator::remove($callType, new ConstantIntegerType(1))->toBoolean()->isFalse()->yes(); } /** 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 index 00119bc6c4a..c0fa1e8a052 100644 --- a/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php +++ b/tests/PHPStan/Analyser/data/type-specifying-extensions-identical-function-call.php @@ -21,6 +21,12 @@ 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)) { @@ -59,6 +65,16 @@ function notIdenticalToOne(mixed $value): void 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) { @@ -73,6 +89,19 @@ function otherConstants(mixed $value): void 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 diff --git a/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php b/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php index 101a839823d..42035c2f0c9 100644 --- a/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php +++ b/tests/PHPStan/Tests/MatchesIntFunctionTypeSpecifyingExtension.php @@ -25,7 +25,7 @@ public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool { - return in_array($functionReflection->getName(), ['IdenticalFunctionCallExtension\\matchesInt', 'IdenticalFunctionCallExtension\\matchesIntOrFails', 'IdenticalFunctionCallExtension\\matchesIntOrFalse'], true) && !$context->null(); + 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 diff --git a/turbo-ext/src/IdenticalNarrowingHelper.cpp b/turbo-ext/src/IdenticalNarrowingHelper.cpp index dffa668924c..a3d3561ab10 100644 --- a/turbo-ext/src/IdenticalNarrowingHelper.cpp +++ b/turbo-ext/src/IdenticalNarrowingHelper.cpp @@ -367,18 +367,18 @@ zv::Val constantStringValue(zval *constantString) return true; } -/* the twin's isOnlyTruthyResultOne(): 1 is among $callType's results and - * removing it leaves only falsy ones; false = pending exception */ +/* 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 superType = pt_type_op(Z_OBJ_P(one.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, callType); - if (UNEXPECTED(superType.isUndef())) return false; - zend_long superValue = pt_type_result_trinary(superType.raw()); - if (UNEXPECTED(superValue < 0)) return false; - if (superValue == PT_TRI_NO) return true; 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")); @@ -1392,17 +1392,11 @@ 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) { - bool one; - if (UNEXPECTED(!constantIntegerContains(1, constantType, one))) return zv::Val(); - bool onlyTruthyOne = false; - if (one) { - zv::Val callType = typeOnScope(subjectResult, evaluationScope); - if (UNEXPECTED(callType.isUndef())) return zv::Val(); - if (UNEXPECTED(!isOnlyTruthyResultOne(callType.raw(), onlyTruthyOne))) return zv::Val(); - } + 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); } } From 74fa3a5d3e9d96d780b3dd33635e611da4e07907 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 17 Nov 2026 11:18:02 +0100 Subject: [PATCH 4/4] Gather the identical function call asserts in the test method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Infection only runs tests that cover a mutated line, and code run from a data provider records no coverage, so the narrowing in IdenticalNarrowingHelper looked untested. 🤖 Generated with Claude Code --- ...yingExtensionIdenticalFunctionCallTest.php | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php index b6285097547..83e2c66ecde 100644 --- a/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php +++ b/tests/PHPStan/Analyser/TypeSpecifyingExtensionIdenticalFunctionCallTest.php @@ -3,32 +3,18 @@ namespace PHPStan\Analyser; use PHPStan\Testing\TypeInferenceTestCase; -use PHPUnit\Framework\Attributes\DataProvider; -use const PHP_VERSION_ID; +use PHPUnit\Framework\Attributes\RequiresPhp; class TypeSpecifyingExtensionIdenticalFunctionCallTest extends TypeInferenceTestCase { - public static function dataAsserts(): iterable + #[RequiresPhp('>= 8.0.0')] + public function testAsserts(): void { - if (PHP_VERSION_ID < 80000) { - return []; + // 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); } - - yield from self::gatherAssertTypes(__DIR__ . '/data/type-specifying-extensions-identical-function-call.php'); - } - - /** - * @param mixed ...$args - */ - #[DataProvider('dataAsserts')] - public function testAsserts( - string $assertType, - string $file, - ...$args, - ): void - { - $this->assertFileAsserts($assertType, $file, ...$args); } public static function getAdditionalConfigFiles(): array