mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 15:42:22 +00:00
907c0dcefa
2d9fc68b65
Bump Rector deps (#38)
83 lines
2.9 KiB
PHP
83 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\NetteToSymfony\Rector\MethodCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PhpParser\Node\Identifier;
|
|
use PHPStan\Type\ObjectType;
|
|
use Rector\Core\NodeManipulator\ClassMethodManipulator;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://doc.nette.org/en/2.4/http-request-response https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php
|
|
*
|
|
* @see \Rector\NetteToSymfony\Tests\Rector\MethodCall\FromHttpRequestGetHeaderToHeadersGetRector\FromHttpRequestGetHeaderToHeadersGetRectorTest
|
|
*/
|
|
final class FromHttpRequestGetHeaderToHeadersGetRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
/**
|
|
* @var \Rector\Core\NodeManipulator\ClassMethodManipulator
|
|
*/
|
|
private $classMethodManipulator;
|
|
public function __construct(\Rector\Core\NodeManipulator\ClassMethodManipulator $classMethodManipulator)
|
|
{
|
|
$this->classMethodManipulator = $classMethodManipulator;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Changes getHeader() to $request->headers->get()', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
use Nette\Request;
|
|
|
|
final class SomeController
|
|
{
|
|
public static function someAction(Request $request)
|
|
{
|
|
$header = $this->httpRequest->getHeader('x');
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
use Nette\Request;
|
|
|
|
final class SomeController
|
|
{
|
|
public static function someAction(Request $request)
|
|
{
|
|
$header = $request->headers->get('x');
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\MethodCall::class];
|
|
}
|
|
/**
|
|
* @param MethodCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->isObjectType($node->var, new \PHPStan\Type\ObjectType('Nette\\Http\\Request'))) {
|
|
return null;
|
|
}
|
|
if (!$this->isName($node->name, 'getHeader')) {
|
|
return null;
|
|
}
|
|
$requestName = $this->classMethodManipulator->addMethodParameterIfMissing($node, new \PHPStan\Type\ObjectType('Symfony\\Component\\HttpFoundation\\Request'), ['request', 'symfonyRequest']);
|
|
$variable = new \PhpParser\Node\Expr\Variable($requestName);
|
|
$headersPropertyFetch = new \PhpParser\Node\Expr\PropertyFetch($variable, 'headers');
|
|
$node->var = $headersPropertyFetch;
|
|
$node->name = new \PhpParser\Node\Identifier('get');
|
|
return $node;
|
|
}
|
|
}
|