[DeadCode] Add RemoveRedundantTypeCheckRector - #8307
Merged
Conversation
TomasVotruba
force-pushed
the
remove-redundant-nullable-type-check
branch
from
August 6, 2026 09:44
99190b5 to
ba03075
Compare
samsonasik
reviewed
Aug 6, 2026
| return null; | ||
| } | ||
|
|
||
| $comparedType = $this->getType($nullComparedExpr); |
Member
There was a problem hiding this comment.
This should use getNativeType(), ensure type via @param docblock skipped, eg:
/**
* @param ?string $p
*/
function p($p) {
}Also, there is ExprAnalyzer::isNonTypedFromParam() check for it
rector-src/src/NodeAnalyzer/ExprAnalyzer.php
Line 142 in 2a551e7
Member
Author
There was a problem hiding this comment.
Good catch, switched both branches to getNativeType() and added the ExprAnalyzer::isNonTypedFromParam() bail-out. Two skip fixtures cover it:
/**
* @param ?string $value
*/
public function run($value): void
{
if (null === $value || ! is_string($value)) {
return;
}
}
/**
* @param mixed[] $clauses
*/
public function run($clauses): void
{
if ($clauses && is_array($clauses)) {
return;
}
}
Member
Author
There was a problem hiding this comment.
Good catch, thank you 👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removes
is_<type>()check that can never fail, because the type is already known.Two shapes are handled.
1)
null ===compare + negated type check - real-world case ongetSecurity(): ?stringOnce null is excluded,
is_string()on a?stringis always true, so the negated check is dead. The condition narrows to the first one.2) truthy check + type check
public function buildWhereClauseFromArray($query, array $clauses, $expr = null) { - if ($clauses && is_array($clauses)) { + if ($clauses) {$clausesis typedarray, sois_array()can never fail.Handles
is_string,is_int/is_integer/is_long,is_float/is_double,is_bool,is_array,is_object.Skipped:
Registered in
DeadCodeLevel.Includes #8308 as its first commit, as CI on
mainis currently red on a stray fixture file; the diff shrinks once that is merged.