From c67cf06492781a5527d7d468df76056236f8ed26 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 28 Nov 2025 23:26:36 +0100 Subject: [PATCH] skip already dim fetch variable on OrdSingleByteRector --- .../skip_already_dim_fetched_value.php.inc | 5 ++ .../Rector/FuncCall/OrdSingleByteRector.php | 54 ++++++++++++------- 2 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_already_dim_fetched_value.php.inc diff --git a/rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_already_dim_fetched_value.php.inc b/rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_already_dim_fetched_value.php.inc new file mode 100644 index 00000000000..69092b8865a --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_already_dim_fetched_value.php.inc @@ -0,0 +1,5 @@ +getArgs(); + $firstArg = $args[0]; - if (! isset($node->args[0])) { - return null; - } - - $argExpr = $args[0]->value; + $argExpr = $firstArg->value; $type = $this->nodeTypeResolver->getNativeType($argExpr); if (! $type->isString()->yes() && ! $type->isInteger()->yes()) { @@ -81,30 +80,49 @@ public function refactor(Node $node): ?Node $isInt = is_int($value); if (! $argExpr instanceof Int_) { + return $this->refactorStringType($argExpr, $isInt, $args, $node); + } - if ($isInt) { - return null; - } + return $this->refactorInt($value, $isInt, $args, $node); + } - $args[0]->value = new ArrayDimFetch($argExpr, new Int_(0)); - $node->args = $args; + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING; + } - return $node; + /** + * @param Arg[] $args + */ + private function refactorStringType(Expr $argExpr, bool $isInt, array $args, FuncCall $funcCall): null|FuncCall + { + if ($argExpr instanceof ArrayDimFetch) { + return null; } + if ($isInt) { + return null; + } + + $args[0]->value = new ArrayDimFetch($argExpr, new Int_(0)); + $funcCall->args = $args; + + return $funcCall; + } + + /** + * @param Arg[] $args + */ + private function refactorInt(mixed $value, bool $isInt, array $args, FuncCall $funcCall): FuncCall + { $value = (string) $value; $byte = $value[0] ?? ''; $byteValue = $isInt ? new Int_((int) $byte) : new String_($byte); $args[0]->value = $byteValue; - $node->args = $args; - - return $node; - } + $funcCall->args = $args; - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING; + return $funcCall; } }