Skip to content

Commit

Permalink
Fix StrictArrayParamDimFetchRector for string access
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 23, 2023
1 parent 3cef752 commit 1179dc3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class TwiceSameType
{
public function getData()
{
if (mt_rand(0, 100)) {
return $this->getNumber(100);
}

return $this->getNumber(10);
}

private function getNumber(int $value): int
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFet

final class SkipPossibleString
{
public function resolve($item)
public function resolve($number)
{
$item .= 'yes';
$check = 0;

return $item[0];
for ($i = 0; $i < strlen($number); $i++) {
$check = $number[$i];
}

return $check;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$hasChanged = false;

if ($node instanceof ClassMethod && $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($node)) {
return null;
}
Expand Down Expand Up @@ -136,6 +135,10 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
return null;
}

if (! $node->dim instanceof Expr) {
return null;
}

if (! $node->var instanceof Variable) {
return null;
}
Expand All @@ -150,6 +153,12 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
return null;
}

// skip integer in possibly string type as string can be accessed via int
$dimType = $this->getType($node->dim);
if ($dimType->isInteger()->yes() && $variableType->isString()->maybe()) {
return null;
}

$isParamAccessedArrayDimFetch = true;
return null;
});
Expand Down

0 comments on commit 1179dc3

Please sign in to comment.