Skip to content

Fix PHPStan level 8 errors: replace deprecated type instanceof checks with PHPStan Type API#56

Merged
voku merged 3 commits intomasterfrom
copilot/check-php-warnings-and-run-phpstan
Apr 9, 2026
Merged

Fix PHPStan level 8 errors: replace deprecated type instanceof checks with PHPStan Type API#56
voku merged 3 commits intomasterfrom
copilot/check-php-warnings-and-run-phpstan

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

PHPStan 2.x deprecates direct instanceof checks on type classes in favour of dedicated predicate methods, producing 49 level-8 errors. CI PHP job logs had no PHP warnings or notices.

Changes

  • IfConditionHelper — Replace all instanceof ConstantStringType, instanceof ConstantScalarType, instanceof IntegerType, instanceof NullType, instanceof ObjectType, instanceof IntersectionType checks with the supported API (->isString(), ->isNull(), ->isObject(), ->getConstantStrings(), ->getConstantScalarTypes(), ->getArrays(), etc.). Add private helpers extractSingleConstantStringType() and extractSingleConstantScalarType() to safely unwrap single-value constant types without widening rule behaviour. Replace instanceof BooleanAndNode / BooleanOrNode with get_class() string comparison to satisfy phpstanApi.instanceofAssumption.

  • ExtendedBinaryOpRule / ExtendedAssignOpRule — Replace deprecated instanceof StringType / instanceof ArrayType guards with ->isString()->yes() / ->isArray()->yes(). Remove now-redundant property_exists guards (the node type is statically known from getNodeType()).

  • WrongCastRule — Replace instanceof StringType, instanceof IntegerType, and runtime is_a() (flagged as phpstanApi.runtimeReflection) with ->isString()->yes(), ->isInteger()->yes(), and ClassReflection::isSubclassOf().

Example

// Before (deprecated — phpstanApi.instanceofType)
if ($type_1 instanceof \PHPStan\Type\ConstantScalarType && $type_1->getValue() !== null) { ... }

// After
$scalar = self::extractSingleConstantScalarType($type_1);
if ($scalar !== null && $scalar->getValue() !== null) { ... }

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 9, 2026

Coverage Report for CI Build 24215948743

Coverage increased (+0.1%) to 94.219%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (138 of 139 lines covered, 99.28%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
src/voku/PHPStan/Rules/IfConditionHelper.php 94 93 98.94%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 813
Covered Lines: 766
Line Coverage: 94.22%
Coverage Strength: 18.41 hits per line

💛 - Coveralls

@voku voku marked this pull request as ready for review April 9, 2026 22:31
@voku voku merged commit 6ac9e06 into master Apr 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants