Skip to content

Commit 0221a95

Browse files
authored
skip already dim fetch variable on OrdSingleByteRector (#7681)
1 parent 0e351ec commit 0221a95

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
3+
4+
$a = 'abc';
5+
echo ord($a[0]);

rules/Php85/Rector/FuncCall/OrdSingleByteRector.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Rector\Php85\Rector\FuncCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
810
use PhpParser\Node\Expr\ArrayDimFetch;
911
use PhpParser\Node\Expr\FuncCall;
1012
use PhpParser\Node\Scalar\Int_;
@@ -65,12 +67,9 @@ public function refactor(Node $node): ?Node
6567
}
6668

6769
$args = $node->getArgs();
70+
$firstArg = $args[0];
6871

69-
if (! isset($node->args[0])) {
70-
return null;
71-
}
72-
73-
$argExpr = $args[0]->value;
72+
$argExpr = $firstArg->value;
7473
$type = $this->nodeTypeResolver->getNativeType($argExpr);
7574

7675
if (! $type->isString()->yes() && ! $type->isInteger()->yes()) {
@@ -81,30 +80,49 @@ public function refactor(Node $node): ?Node
8180
$isInt = is_int($value);
8281

8382
if (! $argExpr instanceof Int_) {
83+
return $this->refactorStringType($argExpr, $isInt, $args, $node);
84+
}
8485

85-
if ($isInt) {
86-
return null;
87-
}
86+
return $this->refactorInt($value, $isInt, $args, $node);
87+
}
8888

89-
$args[0]->value = new ArrayDimFetch($argExpr, new Int_(0));
90-
$node->args = $args;
89+
public function provideMinPhpVersion(): int
90+
{
91+
return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING;
92+
}
9193

92-
return $node;
94+
/**
95+
* @param Arg[] $args
96+
*/
97+
private function refactorStringType(Expr $argExpr, bool $isInt, array $args, FuncCall $funcCall): null|FuncCall
98+
{
99+
if ($argExpr instanceof ArrayDimFetch) {
100+
return null;
93101
}
94102

103+
if ($isInt) {
104+
return null;
105+
}
106+
107+
$args[0]->value = new ArrayDimFetch($argExpr, new Int_(0));
108+
$funcCall->args = $args;
109+
110+
return $funcCall;
111+
}
112+
113+
/**
114+
* @param Arg[] $args
115+
*/
116+
private function refactorInt(mixed $value, bool $isInt, array $args, FuncCall $funcCall): FuncCall
117+
{
95118
$value = (string) $value;
96119
$byte = $value[0] ?? '';
97120

98121
$byteValue = $isInt ? new Int_((int) $byte) : new String_($byte);
99122

100123
$args[0]->value = $byteValue;
101-
$node->args = $args;
102-
103-
return $node;
104-
}
124+
$funcCall->args = $args;
105125

106-
public function provideMinPhpVersion(): int
107-
{
108-
return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING;
126+
return $funcCall;
109127
}
110128
}

0 commit comments

Comments
 (0)