Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StrictArrayParamDimFetchRector for string access #5383

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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