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 eef1826
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Source\ReturnSelfFromSource;

final class ReturnSelfDifferentNamespace
{
public function run()
{
return
array_map(function () {
return ReturnSelfFromSource::fromEvent();
}, ['event']);
}
}

?>
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
@@ -0,0 +1,17 @@
<?php

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

final class SkipArrayPossibleString
{
public function resolve($number)
{
$check = 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 eef1826

Please sign in to comment.