mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 16:02:23 +02:00
[PHP Deglobalize] Add ChangeGlobalVariablesToPropertiesRector
This commit is contained in:
parent
f7d3d26a40
commit
4e9ab470de
@ -1,4 +1,4 @@
|
||||
# All 403 Rectors Overview
|
||||
# All 404 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -16,6 +16,7 @@
|
||||
- [DoctrineCodeQuality](#doctrinecodequality)
|
||||
- [DynamicTypeAnalysis](#dynamictypeanalysis)
|
||||
- [ElasticSearchDSL](#elasticsearchdsl)
|
||||
- [FileSystemRector](#filesystemrector)
|
||||
- [Guzzle](#guzzle)
|
||||
- [Laravel](#laravel)
|
||||
- [Legacy](#legacy)
|
||||
@ -2701,6 +2702,16 @@ Migrates addFilter to addQuery
|
||||
|
||||
<br>
|
||||
|
||||
## FileSystemRector
|
||||
|
||||
### `RemoveProjectFileRector`
|
||||
|
||||
- class: `Rector\FileSystemRector\Rector\Removing\RemoveProjectFileRector`
|
||||
|
||||
Remove file relative to project directory
|
||||
|
||||
<br>
|
||||
|
||||
## Guzzle
|
||||
|
||||
### `MessageAsArrayRector`
|
||||
|
@ -16,6 +16,8 @@ use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
* @sponsor Thanks https://spaceflow.io/ for sponsoring this rule - visit them on https://github.com/SpaceFlow-app
|
||||
*
|
||||
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
|
||||
*
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\MoveInterfacesToContractNamespaceDirectoryRectorTest
|
||||
*/
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRector extends AbstractFileSystemRector
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ final class MoveEntitiesToEntityDirectoryRectorTest extends AbstractFileSystemRe
|
||||
{
|
||||
yield [
|
||||
__DIR__ . '/Source/Controller/RandomEntity.php',
|
||||
__DIR__ . '/Source/Fixture/Entity/RandomEntity.php',
|
||||
__DIR__ . '/Source/Entity/Fixture/RandomEntity.php',
|
||||
__DIR__ . '/Expected/ExpectedRandomEntity.php',
|
||||
];
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector\Source\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
final class RandomEntity
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector\Source\Contract;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
|
||||
}
|
@ -31,7 +31,7 @@ final class MoveServicesBySuffixToDirectoryRectorTest extends AbstractFileSystem
|
||||
|
||||
yield 'prefix_same_namespace' => [
|
||||
__DIR__ . '/Source/Controller/BananaCommand.php',
|
||||
__DIR__ . '/Source/Fixture/Command/BananaCommand.php',
|
||||
__DIR__ . '/Source/Command/Fixture/BananaCommand.php',
|
||||
__DIR__ . '/Expected/Command/ExpectedBananaCommand.php',
|
||||
];
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector\Source\Command;
|
||||
|
||||
final class BananaCommand
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return new \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector\Source\Controller\Orange();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector\Source\Repository;
|
||||
|
||||
class AppleRepository
|
||||
{
|
||||
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
parameters:
|
||||
project_directory: ~
|
||||
|
||||
services:
|
||||
_defaults:
|
||||
public: true
|
||||
|
13
packages/FileSystemRector/src/Configuration/Option.php
Normal file
13
packages/FileSystemRector/src/Configuration/Option.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\FileSystemRector\Configuration;
|
||||
|
||||
final class Option
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PROJECT_DIRECTORY_PARAMETER = 'project_directory';
|
||||
}
|
@ -17,6 +17,7 @@ use Rector\PhpParser\Parser\Parser;
|
||||
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\PhpParser\Printer\FormatPerservingPrinter;
|
||||
use Rector\Rector\AbstractRector\AbstractRectorTrait;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use TypeError;
|
||||
|
||||
@ -29,6 +30,11 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
/**
|
||||
* @var ParameterProvider
|
||||
*/
|
||||
protected $parameterProvider;
|
||||
|
||||
/**
|
||||
* @var Parser
|
||||
*/
|
||||
@ -75,7 +81,8 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
|
||||
NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
|
||||
RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
|
||||
Configuration $configuration,
|
||||
BetterStandardPrinter $betterStandardPrinter
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
ParameterProvider $parameterProvider
|
||||
): void {
|
||||
$this->parser = $parser;
|
||||
$this->parserFactory = $parserFactory;
|
||||
@ -85,6 +92,7 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
|
||||
$this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector;
|
||||
$this->configuration = $configuration;
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\FileSystemRector\Rector\Removing;
|
||||
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\FileSystemRector\Configuration\Option;
|
||||
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @see \Rector\FileSystemRector\Tests\Rector\Removing\RemoveProjectFileRector\RemoveProjectFileRectorTest
|
||||
*/
|
||||
final class RemoveProjectFileRector extends AbstractFileSystemRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $filePathsToRemove = [];
|
||||
|
||||
/**
|
||||
* @param string[] $filePathsToRemove
|
||||
*/
|
||||
public function __construct(array $filePathsToRemove = [])
|
||||
{
|
||||
$this->filePathsToRemove = $filePathsToRemove;
|
||||
}
|
||||
|
||||
public function refactor(SmartFileInfo $smartFileInfo): void
|
||||
{
|
||||
if ($this->filePathsToRemove === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$projectDirectory = $this->getProjectDirectory();
|
||||
|
||||
$relativePathInProject = $smartFileInfo->getRelativeFilePathFromDirectory($projectDirectory);
|
||||
|
||||
foreach ($this->filePathsToRemove as $filePathsToRemove) {
|
||||
if ($relativePathInProject !== $filePathsToRemove) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->removeFile($smartFileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove file relative to project directory');
|
||||
}
|
||||
|
||||
private function getProjectDirectory(): string
|
||||
{
|
||||
$this->ensureProjectDirectoryIsSet();
|
||||
|
||||
return (string) $this->parameterProvider->provideParameter(Option::PROJECT_DIRECTORY_PARAMETER);
|
||||
}
|
||||
|
||||
private function ensureProjectDirectoryIsSet(): void
|
||||
{
|
||||
$projectDirectory = $this->parameterProvider->provideParameter(Option::PROJECT_DIRECTORY_PARAMETER);
|
||||
|
||||
// has value? → skip
|
||||
if ($projectDirectory) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
'Complete "parameters > %s" in rector.yaml, so Rector can found your directory',
|
||||
Option::PROJECT_DIRECTORY_PARAMETER
|
||||
));
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ use Rector\FileSystemRector\FileSystemFileProcessor;
|
||||
use Rector\HttpKernel\RectorKernel;
|
||||
use ReflectionClass;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTestCase
|
||||
@ -58,6 +59,7 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes
|
||||
$fileInfo = new SmartFileInfo($file);
|
||||
|
||||
$temporaryFilePath = $this->createTemporaryFilePath($fileInfo, $file);
|
||||
|
||||
require_once $temporaryFilePath;
|
||||
|
||||
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($temporaryFilePath));
|
||||
@ -86,6 +88,12 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes
|
||||
return FileSystemRectorInterface::class;
|
||||
}
|
||||
|
||||
protected function setParameter(string $name, $value): void
|
||||
{
|
||||
$parameterProvider = self::$container->get(ParameterProvider::class);
|
||||
$parameterProvider->changeParameter($name, $value);
|
||||
}
|
||||
|
||||
private function createContainerWithProvidedRector(): void
|
||||
{
|
||||
$configFileTempPath = $this->createConfigFileTempPath();
|
||||
|
@ -15,9 +15,9 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
|
||||
* @param string[] $expectedExceptions
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $originaFile, array $expectedExceptions, bool $shouldDeleteOriginalFile): void
|
||||
public function test(string $originalFile, array $expectedExceptions, bool $shouldDeleteOriginalFile): void
|
||||
{
|
||||
$temporaryFilePath = $this->doTestFile($originaFile);
|
||||
$temporaryFilePath = $this->doTestFile($originalFile);
|
||||
|
||||
foreach ($expectedExceptions as $expectedExceptionLocation => $expectedFormat) {
|
||||
$this->assertFileExists($expectedExceptionLocation);
|
||||
|
Loading…
x
Reference in New Issue
Block a user