mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-13 20:12:07 +01:00
[DowngradePhp70] Add DowngradeGeneratedScalarTypesRector (#6392)
This commit is contained in:
parent
d7a5324e6b
commit
5784f734f8
@ -12,6 +12,7 @@ use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRect
|
||||
use Rector\DowngradePhp70\Rector\GroupUse\SplitGroupedUseImportsRector;
|
||||
use Rector\DowngradePhp70\Rector\New_\DowngradeAnonymousClassRector;
|
||||
use Rector\DowngradePhp70\Rector\Spaceship\DowngradeSpaceshipRector;
|
||||
use Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
@ -27,4 +28,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services->set(DowngradeDefineArrayConstantRector::class);
|
||||
$services->set(DowngradeSessionStartArrayOptionsRector::class);
|
||||
$services->set(SplitGroupedUseImportsRector::class);
|
||||
$services->set(DowngradeGeneratedScalarTypesRector::class);
|
||||
};
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DowngradeGeneratedScalarTypesRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
/**
|
||||
* @return array|bool|float|int|string|null
|
||||
*/
|
||||
public function getParameter(string $name)
|
||||
{
|
||||
if (isset($this->buildParameters[$name])) {
|
||||
return $this->buildParameters[$name];
|
||||
}
|
||||
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
|
||||
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
||||
}
|
||||
if (isset($this->loadedDynamicParameters[$name])) {
|
||||
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
||||
}
|
||||
return $this->parameters[$name];
|
||||
}
|
||||
public function hasParameter(string $name): bool
|
||||
{
|
||||
if (isset($this->buildParameters[$name])) {
|
||||
return true;
|
||||
}
|
||||
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters);
|
||||
}
|
||||
public function setParameter(string $name, $value): void
|
||||
{
|
||||
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
|
||||
}
|
||||
public function getParameterBag(): ParameterBagInterface
|
||||
{
|
||||
if (null === $this->parameterBag) {
|
||||
$parameters = $this->parameters;
|
||||
foreach ($this->loadedDynamicParameters as $name => $loaded) {
|
||||
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
||||
}
|
||||
foreach ($this->buildParameters as $name => $value) {
|
||||
$parameters[$name] = $value;
|
||||
}
|
||||
$this->parameterBag = new FrozenParameterBag($parameters);
|
||||
}
|
||||
return $this->parameterBag;
|
||||
}
|
||||
EOF;
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
/**
|
||||
* @return array|bool|float|int|string|null
|
||||
* @param string $name
|
||||
*/
|
||||
public function getParameter($name)
|
||||
{
|
||||
if (is_object($name)) {
|
||||
$name = (string) $name;
|
||||
}
|
||||
if (isset($this->buildParameters[$name])) {
|
||||
return $this->buildParameters[$name];
|
||||
}
|
||||
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
|
||||
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
||||
}
|
||||
if (isset($this->loadedDynamicParameters[$name])) {
|
||||
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
||||
}
|
||||
return $this->parameters[$name];
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function hasParameter($name): bool
|
||||
{
|
||||
if (is_object($name)) {
|
||||
$name = (string) $name;
|
||||
}
|
||||
if (isset($this->buildParameters[$name])) {
|
||||
return true;
|
||||
}
|
||||
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters);
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function setParameter($name, $value)
|
||||
{
|
||||
if (is_object($name)) {
|
||||
$name = (string) $name;
|
||||
}
|
||||
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
|
||||
}
|
||||
public function getParameterBag()
|
||||
{
|
||||
if (null === $this->parameterBag) {
|
||||
$parameters = $this->parameters;
|
||||
foreach ($this->loadedDynamicParameters as $name => $loaded) {
|
||||
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
||||
}
|
||||
foreach ($this->buildParameters as $name => $value) {
|
||||
$parameters[$name] = $value;
|
||||
}
|
||||
$this->parameterBag = new FrozenParameterBag($parameters);
|
||||
}
|
||||
return $this->parameterBag;
|
||||
}
|
||||
|
||||
EOF;
|
||||
|
||||
?>
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
return $name;
|
||||
EOF;
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
public function getParameter($name)
|
||||
{
|
||||
return $name;
|
||||
}
|
||||
EOF;
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
public function getParameter($name)
|
||||
{
|
||||
return $name;
|
||||
}
|
||||
|
||||
EOF;
|
||||
|
||||
?>
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
public function getParameter(string $name)
|
||||
{
|
||||
return $name;
|
||||
}
|
||||
EOF;
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\Fixture;
|
||||
|
||||
$code = <<<'EOF'
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function getParameter($name)
|
||||
{
|
||||
if (is_object($name)) {
|
||||
$name = (string) $name;
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
EOF;
|
||||
|
||||
?>
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector;
|
||||
use Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DowngradeGeneratedScalarTypesRector::class);
|
||||
|
||||
// dependendent rules
|
||||
$services->set(DowngradeScalarTypeDeclarationRector::class);
|
||||
$services->set(DowngradeVoidTypeDeclarationRector::class);
|
||||
};
|
@ -19,6 +19,9 @@ final class PhpSpecToPHPUnitRectorTest extends AbstractRectorTestCase
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
|
||||
|
37
rules/DowngradePhp70/NodeFactory/StringifyIfFactory.php
Normal file
37
rules/DowngradePhp70/NodeFactory/StringifyIfFactory.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp70\NodeFactory;
|
||||
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Cast\String_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use Rector\Core\PhpParser\Node\NodeFactory;
|
||||
|
||||
final class StringifyIfFactory
|
||||
{
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
public function __construct(NodeFactory $nodeFactory)
|
||||
{
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
}
|
||||
|
||||
public function createObjetVariableStringCast(string $variableName): If_
|
||||
{
|
||||
$variable = new Variable($variableName);
|
||||
$isObjectFuncCall = $this->nodeFactory->createFuncCall('is_object', [$variable]);
|
||||
|
||||
$if = new If_($isObjectFuncCall);
|
||||
$assign = new Assign($variable, new String_($variable));
|
||||
$if->stmts[] = new Expression($assign);
|
||||
|
||||
return $if;
|
||||
}
|
||||
}
|
@ -5,20 +5,16 @@ declare(strict_types=1);
|
||||
namespace Rector\DowngradePhp70\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Cast\String_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Type\BooleanType;
|
||||
use PHPStan\Type\FloatType;
|
||||
use PHPStan\Type\IntegerType;
|
||||
use PHPStan\Type\StringType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DowngradePhp70\NodeFactory\StringifyIfFactory;
|
||||
use Rector\DowngradePhp71\TypeDeclaration\PhpDocFromTypeDeclarationDecorator;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -35,9 +31,17 @@ final class DowngradeScalarTypeDeclarationRector extends AbstractRector
|
||||
*/
|
||||
private $phpDocFromTypeDeclarationDecorator;
|
||||
|
||||
public function __construct(PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator)
|
||||
{
|
||||
/**
|
||||
* @var StringifyIfFactory
|
||||
*/
|
||||
private $stringifyIfFactory;
|
||||
|
||||
public function __construct(
|
||||
PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator,
|
||||
StringifyIfFactory $stringifyIfFactory
|
||||
) {
|
||||
$this->phpDocFromTypeDeclarationDecorator = $phpDocFromTypeDeclarationDecorator;
|
||||
$this->stringifyIfFactory = $stringifyIfFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -105,17 +109,6 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function createObjetVariableStringCast(string $variableName): If_
|
||||
{
|
||||
$variable = new Variable($variableName);
|
||||
$isObjectFuncCall = $this->nodeFactory->createFuncCall('is_object', [$variable]);
|
||||
|
||||
$if = new If_($isObjectFuncCall);
|
||||
$assign = new Assign($variable, new String_($variable));
|
||||
$if->stmts[] = new Expression($assign);
|
||||
return $if;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Function_|ClassMethod $functionLike
|
||||
* @return Function_|ClassMethod
|
||||
@ -134,7 +127,7 @@ CODE_SAMPLE
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($param->var);
|
||||
|
||||
$if = $this->createObjetVariableStringCast($variableName);
|
||||
$if = $this->stringifyIfFactory->createObjetVariableStringCast($variableName);
|
||||
$functionLike->stmts = array_merge([$if], (array) $functionLike->stmts);
|
||||
|
||||
return $functionLike;
|
||||
|
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp70\Rector\String_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Error;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use Rector\Core\Contract\Rector\PhpRectorInterface;
|
||||
use Rector\Core\PhpParser\Parser\InlineCodeParser;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @changelog https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1516
|
||||
*
|
||||
* @see \Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\DowngradeGeneratedScalarTypesRectorTest
|
||||
*/
|
||||
final class DowngradeGeneratedScalarTypesRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* Extends list here as needed
|
||||
* @var string[]
|
||||
*/
|
||||
private const FILES_TO_INCLUDE = [
|
||||
// https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1516
|
||||
'src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var PhpRectorInterface[]
|
||||
*/
|
||||
private $phpRectors = [];
|
||||
|
||||
/**
|
||||
* @var InlineCodeParser
|
||||
*/
|
||||
private $inlineCodeParser;
|
||||
|
||||
public function __construct(
|
||||
InlineCodeParser $inlineCodeParser,
|
||||
DowngradeScalarTypeDeclarationRector $downgradeScalarTypeDeclarationRector,
|
||||
DowngradeVoidTypeDeclarationRector $downgradeVoidTypeDeclarationRector
|
||||
) {
|
||||
$this->phpRectors = [$downgradeScalarTypeDeclarationRector, $downgradeVoidTypeDeclarationRector];
|
||||
$this->inlineCodeParser = $inlineCodeParser;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Refactor scalar types in PHP code in string snippets, e.g. generated container code from symfony/dependency-injection',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$code = <<<'EOF'
|
||||
public function getParameter(string $name)
|
||||
{
|
||||
return $name;
|
||||
}
|
||||
EOF;
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$code = <<<'EOF'
|
||||
/**
|
||||
* @param string
|
||||
*/
|
||||
public function getParameter($name)
|
||||
{
|
||||
return $name;
|
||||
}
|
||||
EOF;
|
||||
CODE_SAMPLE
|
||||
),
|
||||
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [String_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$fileInfo = $this->file->getSmartFileInfo();
|
||||
|
||||
// this rule is parsing strings, so it heavy on performance; to lower it, we'll process only known opt-in files
|
||||
if (! $this->isRelevantFileInfo($fileInfo)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stringKind = $node->getAttribute(AttributeKey::KIND);
|
||||
if (! in_array($stringKind, [String_::KIND_NOWDOC, String_::KIND_HEREDOC], true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// we assume its a function list - see https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1513-L1560
|
||||
|
||||
try {
|
||||
$nodes = $this->inlineCodeParser->parse('<?php class SomeClass { ' . $node->value . ' }');
|
||||
} catch (Error $error) {
|
||||
// nothing we can do
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($nodes === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// * replace scalar types with docs
|
||||
// * remove return type
|
||||
// somehow we want to call all Rector rules here
|
||||
$nodeTraverser = $this->createNodeTraverser();
|
||||
$changedNodes = $nodeTraverser->traverse($nodes);
|
||||
if (! $changedNodes[0] instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->value = $this->printClassStmts($changedNodes[0]);
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isRelevantFileInfo(SmartFileInfo $fileInfo): bool
|
||||
{
|
||||
// for tests
|
||||
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (self::FILES_TO_INCLUDE as $fileToInclude) {
|
||||
if (Strings::endsWith($fileInfo->getRealPath(), $fileToInclude)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function printClassStmts(Class_ $class): string
|
||||
{
|
||||
$refactoredContent = '';
|
||||
foreach ($class->stmts as $classStmt) {
|
||||
$refactoredContent .= $this->betterStandardPrinter->prettyPrint([$classStmt]) . PHP_EOL;
|
||||
}
|
||||
|
||||
return $refactoredContent;
|
||||
}
|
||||
|
||||
private function createNodeTraverser(): NodeTraverser
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser();
|
||||
foreach ($this->phpRectors as $phpRector) {
|
||||
$nodeTraverser->addVisitor($phpRector);
|
||||
}
|
||||
return $nodeTraverser;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user