Skip to content

Commit 968316d

Browse files
committed
skip already dim fetch variable on OrdSingleByteRector
1 parent 0e351ec commit 968316d

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

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: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Php85\Rector\FuncCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
89
use PhpParser\Node\Expr\ArrayDimFetch;
910
use PhpParser\Node\Expr\FuncCall;
1011
use PhpParser\Node\Scalar\Int_;
@@ -65,12 +66,9 @@ public function refactor(Node $node): ?Node
6566
}
6667

6768
$args = $node->getArgs();
69+
$firstArg = $args[0];
6870

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

7674
if (! $type->isString()->yes() && ! $type->isInteger()->yes()) {
@@ -81,30 +79,49 @@ public function refactor(Node $node): ?Node
8179
$isInt = is_int($value);
8280

8381
if (! $argExpr instanceof Int_) {
82+
return $this->refactorStringType($argExpr, $isInt, $args, $node);
83+
}
8484

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

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

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

95-
$value = (string) $value;
96-
$byte = $value[0] ?? '';
97-
98-
$byteValue = $isInt ? new Int_((int) $byte) : new String_($byte);
102+
if ($isInt) {
103+
return null;
104+
}
99105

100-
$args[0]->value = $byteValue;
106+
$args[0]->value = new ArrayDimFetch($argExpr, new Int_(0));
101107
$node->args = $args;
102108

103109
return $node;
104110
}
105111

106-
public function provideMinPhpVersion(): int
112+
/**
113+
* @param Arg[] $args
114+
*/
115+
private function refactorInt(mixed $value, bool $isInt, array $args, FuncCall $funcCall): FuncCall
107116
{
108-
return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING;
117+
$value = (string) $value;
118+
$byte = $value[0] ?? '';
119+
120+
$byteValue = $isInt ? new Int_((int) $byte) : new String_($byte);
121+
122+
$args[0]->value = $byteValue;
123+
$funcCall->args = $args;
124+
125+
return $funcCall;
109126
}
110127
}

0 commit comments

Comments
 (0)