Skip to content
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,5 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;

$a = 'abc';
echo ord($a[0]);
54 changes: 36 additions & 18 deletions rules/Php85/Rector/FuncCall/OrdSingleByteRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\Php85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
Expand Down Expand Up @@ -65,12 +67,9 @@ public function refactor(Node $node): ?Node
}

$args = $node->getArgs();
$firstArg = $args[0];

if (! isset($node->args[0])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset is needed in case someone for whatever doesn't have argument, or have remove argument via custom rule, this will cause error undefined index 0 and error:

Trying to get property 'value' of non-object

in php 7.4, it just warning, but not error

https://3v4l.org/batdI#v7.4.33

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Didn't know older Php version allow less args. Thanks 👌

return null;
}

$argExpr = $args[0]->value;
$argExpr = $firstArg->value;
$type = $this->nodeTypeResolver->getNativeType($argExpr);

if (! $type->isString()->yes() && ! $type->isInteger()->yes()) {
Expand All @@ -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;
}
}