mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Transform\Rector\String_;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Cast\String_;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Identifier;
|
|
use PHPStan\Type\ObjectType;
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\Transform\Rector\String_\ToStringToMethodCallRector\ToStringToMethodCallRectorTest
|
|
*/
|
|
final class ToStringToMethodCallRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @api
|
|
* @var string
|
|
*/
|
|
public const METHOD_NAMES_BY_TYPE = 'method_names_by_type';
|
|
/**
|
|
* @var array<string, string>
|
|
*/
|
|
private $methodNamesByType = [];
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Turns defined code uses of "__toString()" method to specific method calls.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
$someValue = new SomeObject;
|
|
$result = (string) $someValue;
|
|
$result = $someValue->__toString();
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
$someValue = new SomeObject;
|
|
$result = $someValue->getPath();
|
|
$result = $someValue->getPath();
|
|
CODE_SAMPLE
|
|
, [self::METHOD_NAMES_BY_TYPE => ['SomeObject' => 'getPath']])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\Cast\String_::class, \PhpParser\Node\Expr\MethodCall::class];
|
|
}
|
|
/**
|
|
* @param String_|MethodCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if ($node instanceof \PhpParser\Node\Expr\Cast\String_) {
|
|
return $this->processStringNode($node);
|
|
}
|
|
return $this->processMethodCall($node);
|
|
}
|
|
/**
|
|
* @param array<string, array<string, string>> $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
$this->methodNamesByType = $configuration[self::METHOD_NAMES_BY_TYPE] ?? [];
|
|
}
|
|
private function processStringNode(\PhpParser\Node\Expr\Cast\String_ $string) : ?\PhpParser\Node
|
|
{
|
|
foreach ($this->methodNamesByType as $type => $methodName) {
|
|
if (!$this->isObjectType($string->expr, new \PHPStan\Type\ObjectType($type))) {
|
|
continue;
|
|
}
|
|
return $this->nodeFactory->createMethodCall($string->expr, $methodName);
|
|
}
|
|
return null;
|
|
}
|
|
private function processMethodCall(\PhpParser\Node\Expr\MethodCall $methodCall) : ?\PhpParser\Node
|
|
{
|
|
foreach ($this->methodNamesByType as $type => $methodName) {
|
|
if (!$this->isObjectType($methodCall->var, new \PHPStan\Type\ObjectType($type))) {
|
|
continue;
|
|
}
|
|
if (!$this->isName($methodCall->name, '__toString')) {
|
|
continue;
|
|
}
|
|
$methodCall->name = new \PhpParser\Node\Identifier($methodName);
|
|
return $methodCall;
|
|
}
|
|
return null;
|
|
}
|
|
}
|