Skip to content

Add comprehensive test coverage across all phpstan rules#55

Merged
voku merged 2 commits intomasterfrom
copilot/add-more-tests-and-fix-issues
Apr 9, 2026
Merged

Add comprehensive test coverage across all phpstan rules#55
voku merged 2 commits intomasterfrom
copilot/add-more-tests-and-fix-issues

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

  • Investigate all three review findings
  • Fix ExtendedBinaryOpRuleFixtures.php: change "Error: null-coalesce..." → "OK: null-coalesce is not handled by this rule (ExtendedBinaryOpRule skips Coalesce nodes)"
  • Fix IfConditionMatchRuleFixtures.php: change "Error: use a method..." → accurate OK comment explaining the rule checks match subject type vs arm condition type, not inner expressions
  • Fix IfConditionSwitchCaseRuleFixtures.php: change three "Error:" comments → accurate OK comments explaining why float/null don't trigger and why stdClass inside && is not checked
  • Run all 18 tests twice — both runs pass consistently

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 8, 2026

Coverage Report for CI Build 24198084383

Coverage increased (+3.5%) to 94.118%

Details

  • Coverage increased (+3.5%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 799
Covered Lines: 752
Line Coverage: 94.12%
Coverage Strength: 2.98 hits per line

💛 - Coveralls

@voku voku marked this pull request as ready for review April 9, 2026 13:53
@voku
Copy link
Copy Markdown
Owner

voku commented Apr 9, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request expands the test suite for several PHPStan rules, including DisallowedCallMethodOnNullRule, ElseIfConditionBasicRule, ExtendedBinaryOpRule, and various boolean condition rules. It also introduces a new WrongCastRuleTest along with its fixtures. The review feedback points out that new error scenarios added to the match and switch fixtures lack corresponding assertions in their respective test files, which will cause test failures. Additionally, a comment in the ExtendedBinaryOpRule fixture incorrectly identifies a null-coalesce operation as an error when the rule actually ignores it.

$obj = new \stdClass();
$flag = rand(0, 1) ? true : false;
$result = match (true) {
$obj && $flag => 'Has object and flag',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This fixture introduces a new error scenario for IfConditionMatchRule, but the corresponding test file tests/IfConditionMatchRuleTest.php is not updated in this pull request to assert this error. This will likely cause the test suite to fail as PHPStan will report an un-asserted error.

$obj = new \stdClass();
$flag = rand(0, 1) ? true : false;
switch (true) {
case $obj && $flag:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the match fixtures, this file adds several new error scenarios (lines 319, 352, 360), but the test file tests/IfConditionSwitchCaseRuleTest.php is missing from the pull request. These new errors must be asserted in the test case to ensure the build passes and the rule is correctly verified.

}
}

// Error: null-coalesce that is always non-null because the left side is a non-empty string
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment indicates an error is expected for the null-coalesce operator, but ExtendedBinaryOpRule explicitly ignores Coalesce nodes (see ExtendedBinaryOpRule.php line 74). Additionally, the corresponding test tests/ExtendedBinaryOpRuleTest.php does not assert an error for this line. This comment should be corrected to avoid confusion.

// OK: null-coalesce is not handled by this rule

@voku
Copy link
Copy Markdown
Owner

voku commented Apr 9, 2026

@copilot Review the following cleaned github pull request excerpt.
Focus on the substantive content and ignore any residual site chrome.

Add comprehensive test coverage across all phpstan rules#55
Copilot
master
copilot/add-more-tests-and-fix-issues
+440-22Lines changed: 440 additions & 22 deletions
Add comprehensive test coverage across all phpstan rules#55
Copilot
master
copilot/add-more-tests-and-fix-issues

WrongCastRule had zero test coverage, and all other rule tests had minimal fixture coverage with few edge cases or OK-path assertions.
New: WrongCastRuleTest

Three test methods covering the full behavior matrix:

String→int cast inside a class extending the configured base → error
String→int cast in a non-subclass → no error
String→int cast in a sub-subclass (inheritance chain) → error (verifies is_a depth)

Four PSR-4-named fixture files required because $scope->getClassReflection() resolves via the autoloader at rule runtime, not PHPStan's AST — classes must be in correctly named files to be found.
Extended existing tests
Test Added coverage
DisallowedCallMethodOnNull Non-null variable (no error), null-guarded block (no error), second nullable type (error)
IfConditionBooleanOr stdClass on right side of ||, OK cases with bool/int and null-checked nullable
IfConditionBooleanNot !$obj where $obj is stdClass outside an if
IfConditionBasic Negation of a non-empty array literal
ExtendedBinaryOp bool == '' mixed-type comparison
ElseIfConditionBasic Non-empty array in elseif condition
IfConditionBooleanAnd Null-checked nullable pair (OK), extra stdClass direct use (error)
IfConditionTernaryOperator stdClass used directly as ternary condition
ExtendedAssignOp String concat and int addition OK-path cases
IfConditionMatch / IfConditionSwitchCase Additional integer/string OK cases and further object-in-condition scenarios
Add comprehensive tests for WrongCastRule and extend existing tests
coveralls
Coverage Report for CI Build 24139695304
Coverage increased (+3.5%) to 94.118%
Details

Coverage increased (+3.5%) from the base build.
Patch coverage: No coverable lines changed in this PR.
No coverage regressions found.

Uncovered Changes

No uncovered changes found.
Coverage Regressions

No coverage regressions found.
Coverage Stats
Coverage Status
Relevant Lines: 799
Covered Lines: 752
Line Coverage: 94.12%
Coverage Strength: 2.98 hits per line
💛 - Coveralls

/gemini review

This pull request expands the test suite for several PHPStan rules, including DisallowedCallMethodOnNullRule, ElseIfConditionBasicRule, ExtendedBinaryOpRule, and various boolean condition rules. It also introduces a new WrongCastRuleTest along with its fixtures. The review feedback points out that new error scenarios added to the match and switch fixtures lack corresponding assertions in their respective test files, which will cause test failures. Additionally, a comment in the ExtendedBinaryOpRule fixture incorrectly identifies a null-coalesce operation as an error when the rule actually ignores it.
tests/fixtures/IfConditionMatchRuleFixtures.php
$obj = new \stdClass();
$flag = rand(0, 1) ? true : false;
$result = match (true) {
$obj && $flag => 'Has object and flag',

@gemini-code-assist
Copy link
Copy Markdown

Thank you for requesting a review. This pull request significantly improves the test coverage for several PHPStan rules, which is a valuable contribution. The new WrongCastRuleTest and its associated fixtures are well-structured and cover the intended scenarios effectively.

However, I've identified a few areas where the test assertions do not fully align with the error conditions described in the fixture files. These discrepancies could lead to confusion or mask potential issues in the rules themselves. Please consider the following:

  • tests/fixtures/ExtendedBinaryOpRuleFixtures.php

    • Severity: high
    • Description: The comment on line 79, // Error: null-coalesce that is always non-null because the left side is a non-empty string, indicates an expected error. However, there is no corresponding assertion for this line in ExtendedBinaryOpRuleTest.php. If the rule is not intended to flag this as an error, the comment should be updated to reflect the correct behavior (e.g., // OK: null-coalesce...). If it should be an error, an assertion needs to be added to the test file.
  • tests/fixtures/IfConditionMatchRuleFixtures.php

    • Severity: high
    • Description: The fixture includes an error scenario on line 265: // Error: use a method - stdClass used directly in match condition. This error is not asserted in IfConditionMatchRuleTest.php. Please add the expected error assertion to the test file for this case.
  • tests/fixtures/IfConditionSwitchCaseRuleFixtures.php

    • Severity: high
    • Description: There are multiple error scenarios in this fixture that lack corresponding assertions in IfConditionSwitchCaseRuleTest.php:
      • Line 315: // Error: stdClass used directly in switch case
      • Line 349: // Error: Do not compare boolean and float
      • Line 357: // Error: Do not compare boolean and null (explicit null case)
    • Please add the necessary error assertions to IfConditionSwitchCaseRuleTest.php for these cases to ensure full test coverage for the described error conditions.

Addressing these points will ensure the tests accurately reflect the intended behavior of the rules and prevent future regressions.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot Review the following cleaned github pull request excerpt.
Focus on the substantive content and ignore any residual site chrome.

...

All three issues from the review were already addressed in commit 53026b9:

  1. ExtendedBinaryOpRuleFixtures.php// Error: null-coalesce… corrected to // OK: null-coalesce is not handled by this rule (ExtendedBinaryOpRule skips Coalesce nodes).
  2. IfConditionMatchRuleFixtures.php// Error: use a method - stdClass used directly in match condition corrected to an // OK: comment explaining that the rule checks the match-subject type vs arm-condition type, not sub-expressions inside &&.
  3. IfConditionSwitchCaseRuleFixtures.php — Three // Error: comments changed to // OK: comments: the $obj && $flag case (same reason as match), case 1.5: (float not covered by processBooleanComparison), and case null: (null not covered by processBooleanComparison).

All 18 tests continue to pass.

Copilot finished work on behalf of voku April 9, 2026 15:21
@voku voku merged commit 42109bf 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