Repository navigation
Conversation
| private function areSameStrictScalarTypes(Type $needleType, Type $haystackValueType): bool | ||
| { | ||
| return ( | ||
| $needleType->isBoolean()->yes() | ||
| && $haystackValueType->isBoolean()->yes() | ||
| ) || ( | ||
| $needleType->isInteger()->yes() | ||
| && $haystackValueType->isInteger()->yes() | ||
| ); | ||
| } |
There was a problem hiding this comment.
only fixing the most valuable and least risky for now
| $numericStringNeedle = '01'; // edge case: numeric string compared against int-string | ||
| in_array($numericStringNeedle, ['1', '2']); |
There was a problem hiding this comment.
this case shows why we don't try to auto-fix numeric-string based haystack/needle.
There was a problem hiding this comment.
🟡 Changes recommended
The fix can discard evaluation of an existing strict expression and alter subsequent behavior, so side-effecting expressions must remain unfixable, with regression coverage for positional and named arguments.
1 open issue
What changed in this PR
Adds automatic fixes to StrictFunctionCallsRule for selected array-search calls, addressing #347 while retaining existing strictness diagnostics.
Changes:
- Adds or replaces
strict: truewhen native needle and haystack value types are both integers or both booleans. - Supports positional and named arguments, while skipping unpacked calls and excluding unrelated functions.
- Adds fix tests covering eligible calls and cases that should remain unchanged.
| File | Description |
|---|---|
tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php |
Adds positional and named-argument fix tests. |
tests/Rules/StrictCalls/data/strict-function-calls-named-arguments.php.fixed |
Defines expected named-argument fixes. |
tests/Rules/StrictCalls/data/strict-function-calls-named-arguments.php |
Supplies named-argument and native-type eligibility cases. |
tests/Rules/StrictCalls/data/strict-function-calls-fixable.php.fixed |
Defines expected positional fixes and unchanged calls. |
tests/Rules/StrictCalls/data/strict-function-calls-fixable.php |
Supplies eligible calls and excluded comparison cases. |
src/Rules/StrictCalls/StrictFunctionCallsRule.php |
Adds type-based fix eligibility and strict-argument rewriting. |
Review effort: Balanced
|
//cc @SanderMuller |
|
I checked out b972f1a and ran the tests, the PHPStan check and a set of extra cases. The fix logic held in every case I tried. I found one fixture problem and one performance cost. Fixture lines that do not compile. In Performance. For every fixable error, PHPStan builds the fix during analysis:
A file with one fixable call shows no difference. The cost is in phpstan-src, not in this rule, and phpstan/phpstan-src#6934 reduces it. With that PR, the 1816-line file takes 0.76-0.78 s with either rule, and the 600-error file takes 6.87 to 7.38 s instead of 12.97 to 13.45 s (two runs). Whether to merge this before or after that change is your decision. Checked, no problem found:
Tests (77) and |
thats interessting. lets wait a bit whether we can speedup the fixing use-case in phpstan-src a bit |
I personally don't use PHPStan for auto fixing code, I prefer to fix it myself. If I understand correctly the more we make rule autofixable, the slower PHPStan will run just for analyze ? |
yeah thats what I understand. its a pretty new feature, so lets see whether @ondrejmirtes has ideas so it does not matter much perf wise |

closes #347