[CodeQuality] Add ForRepeatedCountToOwnVariableRector#2559
[CodeQuality] Add ForRepeatedCountToOwnVariableRector#2559TomasVotruba merged 1 commit intomasterfrom
Conversation
ed1f6da to
fbc6b4c
Compare
fbc6b4c to
7f99a93
Compare
| } else { | ||
| $countedValueName = $valueName . 'Count'; | ||
| } | ||
|
|
There was a problem hiding this comment.
Should the rector make sure that there is no other - already existing variable - in the current scope which might collide with the newly introduced one?
There was a problem hiding this comment.
Could you provide code example?
How would you name new variable in case of conflict?
There was a problem hiding this comment.
class SomeClass
{
public function run($items)
{
$itemsCount = 25;
for ($i = 5; $i <= count($items); $i++) {
echo $items[$i];
}
If ($itemsCount == 25 ) echo „whoo“;
}
}I guess you would need just make sure the var name is unique, e.g. by appending a 1 or similar (after you checked the final var name is unique)
| public function run($items, \stdClass $someObject) | ||
| { | ||
| for ($i = 5; $i <= count($someObject->getItems() + 10); $i++) { | ||
| echo $items[$i]; |
There was a problem hiding this comment.
In case the loop body changes the number of elements of the array the count() cannot be calculated beforehand .. could happen when the array get re-assigned, changed by reference or elements get added/deleted
There was a problem hiding this comment.
There are many theoretical edge cases. Not worth covering unless really exist
rectorphp/rector-src@bab3835 [TypeDeclaration] Skip string type override nullable string return on AddReturnTypeDeclarationRector (#2559)
Closes #1573