Report missed loose constant-array comparisons in IfConditionRule#62
Report missed loose constant-array comparisons in IfConditionRule#62
Conversation
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/733b771b-827c-4fd0-ac6b-5492d3b5e403 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/733b771b-827c-4fd0-ac6b-5492d3b5e403 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/7a96bef6-59ab-4429-a442-85b33d67d6f2 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/7a96bef6-59ab-4429-a442-85b33d67d6f2 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/07b50965-b08c-424d-983b-49d382d010b8 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/07b50965-b08c-424d-983b-49d382d010b8 Co-authored-by: voku <264695+voku@users.noreply.github.com>
|
/gemini review |
Coverage Report for CI Build 25046427843Coverage decreased (-0.3%) to 94.603%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
There was a problem hiding this comment.
Code Review
This pull request introduces a new helper method extractSingleConstantArrayType to IfConditionHelper and integrates it into processEqualRules and processNotEqualRules to detect type mismatches involving constant arrays. It also adds several test cases and fixtures to verify these new rules and prevent regressions. The review feedback highlights significant issues with the error messages: the grammar is incorrect, and more importantly, the logic for 'Not Equal' comparisons is inverted, as disjoint types result in a condition that is always true rather than falsy. Additionally, there is an opportunity to refactor the duplicated logic for constant scalars and arrays into a shared method.
| $type_1ConstantArray = self::extractSingleConstantArrayType($type_1); | ||
| if ( | ||
| $type_1ConstantArray !== null | ||
| && | ||
| $type_2 | ||
| && | ||
| $type_2->accepts($type_1ConstantArray, true)->no() | ||
| ) { | ||
| $errors[] = self::buildErrorMessage( | ||
| $origNode, | ||
| sprintf('Condition between %s and %s are falsy, please do not mix types.', $type_1ConstantArray->describe(VerbosityLevel::value()), $type_2->describe(VerbosityLevel::value())), | ||
| $cond->getAttribute('startLine') | ||
| ); | ||
| } |
There was a problem hiding this comment.
The logic for checking constant arrays is identical to the logic for constant scalars (lines 255-269). Consider refactoring this into a shared private method to improve maintainability and avoid duplication.
Additionally, the error message has a grammatical issue: "Condition ... are falsy" should be "Condition ... is falsy".
Finally, note that using accepts(..., true)->no() for loose equality (==) can be misleading. For example, [] == null and [] == false are true in PHP, but this rule will report them as "falsy" because null and false do not strictly accept an array. While this might be intended to enforce strictness, the wording "are falsy" is technically incorrect in those cases.
$type_1ConstantArray = self::extractSingleConstantArrayType($type_1);
if (
$type_1ConstantArray !== null
&&
$type_2
&&
$type_2->accepts($type_1ConstantArray, true)->no()
) {
$errors[] = self::buildErrorMessage(
$origNode,
sprintf('Condition between %s and %s is always false, please do not mix types.', $type_1ConstantArray->describe(VerbosityLevel::value()), $type_2->describe(VerbosityLevel::value())),
$cond->getAttribute('startLine')
);
}| $type_1ConstantArray = self::extractSingleConstantArrayType($type_1); | ||
| if ( | ||
| $type_1ConstantArray !== null | ||
| && | ||
| $type_2 | ||
| && | ||
| $type_2->accepts($type_1ConstantArray, true)->no() | ||
| ) { | ||
| $errors[] = self::buildErrorMessage( | ||
| $origNode, | ||
| sprintf('Condition between %s and %s are falsy, please do not mix types.', $type_1ConstantArray->describe(VerbosityLevel::value()), $type_2->describe(VerbosityLevel::value())), | ||
| $cond->getAttribute('startLine') | ||
| ); | ||
| } |
There was a problem hiding this comment.
In addition to the duplication and grammar issues mentioned in processEqualRules, the error message here is logically incorrect for a "Not Equal" (!=) comparison. If the types are disjoint (accepts()->no()), the condition is actually always true, not "falsy". Reporting it as "falsy" will be confusing to users.
$type_1ConstantArray = self::extractSingleConstantArrayType($type_1);
if (
$type_1ConstantArray !== null
&&
$type_2
&&
$type_2->accepts($type_1ConstantArray, true)->no()
) {
$errors[] = self::buildErrorMessage(
$origNode,
sprintf('Condition between %s and %s is always true, please do not mix types.', $type_1ConstantArray->describe(VerbosityLevel::value()), $type_2->describe(VerbosityLevel::value())),
$cond->getAttribute('startLine')
);
}Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/e30b8e5f-d074-42a1-8c85-8fd7028b1db4 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/e30b8e5f-d074-42a1-8c85-8fd7028b1db4 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/e30b8e5f-d074-42a1-8c85-8fd7028b1db4 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/e30b8e5f-d074-42a1-8c85-8fd7028b1db4 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/153dad30-8a9f-4c3f-8f0a-bd5054e37732 Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/fb87b1a3-25d2-4cdf-9b6c-37fd2899f4af Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/fb87b1a3-25d2-4cdf-9b6c-37fd2899f4af Co-authored-by: voku <264695+voku@users.noreply.github.com>
Agent-Logs-Url: https://github.com/voku/phpstan-rules/sessions/a96a94d3-e995-43c3-bc4c-07d7a453ec69 Co-authored-by: voku <264695+voku@users.noreply.github.com>
== null/!= nullreadability cases