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,21 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;

class SkipAlreadySetEncodingNamedArg
{
public function run(string $text, string $encoding)
{
return mb_strrpos($text, 'abc', encoding: $encoding);
}

public function runWithOffset(string $text, int $offset, string $encoding)
{
return mb_strrpos($text, 'abc', $offset, encoding: $encoding);
}

public function runWithNamedOffset(string $text, int $offset, string $encoding)
{
return mb_strrpos($text, 'abc', encoding: $encoding, offset: $offset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;

class SkipMixedOffset
{
public function run($string, $needle, $offset = 0)
{
return mb_strrpos($string, $needle, $offset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;

class SkipSpreadOrDocblock
{
public function runSpread(array $args)
{
return mb_strrpos(...$args);
}

public function runFirstClassCallable()
{
return mb_strrpos(...);
}

/**
* @param string $encoding
*/
public function runDocblock($text, $encoding)
{
return mb_strrpos($text, 'abc', $encoding);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;

class StringEncodingVariable
{
public function run($text, string $encoding)
{
return mb_strrpos($text, 'abc', $encoding);
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;

class StringEncodingVariable
{
public function run($text, string $encoding)
{
return mb_strrpos($text, 'abc', 0, $encoding);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->args[2])) {
// encoding already passed, positionally or by name
if ($node->getArg('encoding', 3) instanceof Arg) {
return null;
}

if (isset($node->args[3])) {
$offsetArg = $node->getArg('offset', 2);
if (! $offsetArg instanceof Arg) {
return null;
}

if (! $node->args[2] instanceof Arg) {
// only string can be an encoding; mixed/unknown can be the int $offset
$offsetArgType = $this->nodeTypeResolver->getNativeType($offsetArg->value);
if (! $offsetArgType->isString()->yes()) {
return null;
}

$secondArgType = $this->getType($node->args[2]->value);
if ($secondArgType->isInteger()->yes()) {
return null;
}

$node->args[3] = $node->args[2];
$node->args[3] = $offsetArg;
$node->args[2] = new Arg(new Int_(0));

return $node;
Expand Down
Loading