add PHP support to FileHashComputer

This commit is contained in:
TomasVotruba 2020-07-16 20:59:54 +02:00
parent 34ff14709c
commit 78f830a6f7
10 changed files with 117 additions and 50 deletions

View File

@ -6,6 +6,8 @@ declare(strict_types=1);
use Rector\Caching\ChangedFilesDetector;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\MinimalVersionChecker;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonReader;
use Rector\Core\Console\Application;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\DependencyInjection\RectorContainerFactory;
@ -31,10 +33,10 @@ $autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
$autoloadIncluder->autoloadFromCommandLine();
try {
$composerJsonReader = new MinimalVersionChecker\ComposerJsonReader(__DIR__ . '/../composer.json');
$composerJsonReader = new ComposerJsonReader(__DIR__ . '/../composer.json');
$versionChecker = new MinimalVersionChecker(
PHP_VERSION,
new MinimalVersionChecker\ComposerJsonParser($composerJsonReader->read())
new ComposerJsonParser($composerJsonReader->read())
);
$versionChecker->check();

View File

@ -41,7 +41,7 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"johnkary/phpunit-speedtrap": "^3.2",
"migrify/config-transformer": "^0.3.10",
"migrify/config-transformer": "^0.3.11",
"ocramius/package-versions": "^1.4|^1.5",
"phpunit/phpunit": "^8.5|^9.0",
"psr/event-dispatcher": "^1.0",
@ -282,8 +282,8 @@
"bin/rector dump-rectors > docs/rector_rules_overview.md",
"bin/rector dump-nodes > docs/nodes_overview.md"
],
"rector-ci": "bin/rector process --config rector-ci.yaml --dry-run --ansi",
"rector": "bin/rector process --config rector-ci.yaml --ansi"
"rector-ci": "bin/rector process --config rector-ci.php --dry-run --ansi",
"rector": "bin/rector process --config rector-ci.php --ansi"
},
"config": {
"sort-packages": true

View File

@ -56,6 +56,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
'auto_names',
'import_name(space)?(s)?',
],
'paths' => ['path', 'source'], ]
'paths' => ['path', 'source'],
]
);
};

30
ecs.php
View File

@ -1,19 +1,22 @@
<?php
declare(strict_types=1);
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff;
use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer;
use PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer;
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer;
use PhpCsFixer\Fixer\Strict\StrictComparisonFixer;
use SlevomatCodingStandard\Sniffs\Commenting\DisallowCommentAfterCodeSniff;
use SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff;
use SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff;
use SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff;
use SlevomatCodingStandard\Sniffs\Whitespaces\DuplicateSpacesSniff;
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff;
use Symplify\EasyCodingStandard\Configuration\Option;
@ -25,8 +28,12 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DuplicateSpacesSniff::class);
$services->set(StandaloneLineInMultilineArrayFixer::class);
$services->set(GeneralPhpdocAnnotationRemoveFixer::class)
->call('configure', [['annotations' => ['throws', 'author', 'package', 'group']]]);
->call('configure', [[
'annotations' => ['throws', 'author', 'package', 'group'],
]]);
$services->set(LineLengthFixer::class);
@ -43,6 +50,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
__DIR__ . '/config',
__DIR__ . '/ecs.php',
__DIR__ . '/rector-ci.php',
__DIR__ . '/config/set',
]);
$parameters->set(Option::SETS, ['psr12', 'php70', 'php71', 'symplify', 'common', 'clean-code']);
@ -52,18 +60,16 @@ return static function (ContainerConfigurator $containerConfigurator): void {
'*/Fixture/*', '*/Expected/*',
# generated from /vendor
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php',
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingAnnotationReader.php'
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingAnnotationReader.php',
]);
$parameters->set(Option::SKIP, [
GlobalNamespaceImportFixer::class => null,
MethodDeclarationSniff::class . '.Underscore' => null,
PhpdocTypesFixer::class => [
__DIR__ . '/rules/php74/src/Rector/Double/RealToFloatTypeCastRector.php'
],
PhpdocTypesFixer::class => [__DIR__ . '/rules/php74/src/Rector/Double/RealToFloatTypeCastRector.php'],
AssignmentInConditionSniff::class . '.FoundInWhileCondition' => null,
UnusedVariableSniff::class . '.' . UnusedVariableSniff::CODE_UNUSED_VARIABLE => [
__DIR__ . '/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php'
__DIR__ . '/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php',
],
CommentedOutCodeSniff::class . '.Found' => [
__DIR__ . '/rules/php72/src/Rector/Each/ListEachRector.php',
@ -73,18 +79,18 @@ return static function (ContainerConfigurator $containerConfigurator): void {
PhpUnitStrictFixer::class => [
__DIR__ . '/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php',
__DIR__ . '/tests/PhpParser/Node/NodeFactoryTest.php',
'*TypeResolverTest.php'
'*TypeResolverTest.php',
],
UnaryOperatorSpacesFixer::class => null,
StrictComparisonFixer::class => [
__DIR__ . '/packages/polyfill/src/ConditionEvaluator.php'
],
StrictComparisonFixer::class => [__DIR__ . '/packages/polyfill/src/ConditionEvaluator.php'],
ReferenceUsedNamesOnlySniff::class . '.' . ReferenceUsedNamesOnlySniff::CODE_PARTIAL_USE => null,
ReferenceUsedNamesOnlySniff::class . '.' . ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME => [
__DIR__ . '/packages/better-php-doc-parser/src/PhpDocNodeFactory/Doctrine/Property_/DoctrineTargetEntityPhpDocNodeFactory.php',
__DIR__ . '/packages/better-php-doc-parser/src/PhpDocNodeFactory/Doctrine/Property_/JoinTablePhpDocNodeFactory.php',
__DIR__ . '/packages/better-php-doc-parser/src/PhpDocNodeFactory/Doctrine/Class_/TablePhpDocNodeFactory.php',
__DIR__ . '/packages/better-php-doc-parser/src/PhpDocNodeFactory/JMS/JMSInjectPhpDocNodeFactory.php']]);
__DIR__ . '/packages/better-php-doc-parser/src/PhpDocNodeFactory/JMS/JMSInjectPhpDocNodeFactory.php',
],
]);
$parameters->set('line_ending', "\n");
};

View File

@ -7,7 +7,11 @@ namespace Rector\Caching\Config;
use Nette\Utils\Strings;
use Rector\Core\Exception\ShouldNotHappenException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/**
@ -17,25 +21,25 @@ final class FileHashComputer
{
public function compute(string $filePath): string
{
$this->ensureIsYaml($filePath);
$this->ensureIsYamlOrPhp($filePath);
$containerBuilder = new ContainerBuilder();
$fileLoader = $this->createFileLoader($filePath, $containerBuilder);
$yamlFileLoader = new YamlFileLoader($containerBuilder, new FileLocator(dirname($filePath)));
$yamlFileLoader->load($filePath);
$fileLoader->load($filePath);
return $this->arrayToHash($containerBuilder->getDefinitions()) .
$this->arrayToHash($containerBuilder->getParameterBag()->all());
}
private function ensureIsYaml(string $filePath): void
private function ensureIsYamlOrPhp(string $filePath): void
{
if (Strings::match($filePath, '#\.(yml|yaml)$#')) {
if (Strings::match($filePath, '#\.(yml|yaml|php)$#')) {
return;
}
throw new ShouldNotHappenException(sprintf(
'Provide only yml/yaml file, ready for Symfony DI. "%s" given', $filePath
'Provide only YAML/PHP file, ready for Symfony Dependency Injection. "%s" given', $filePath
));
}
@ -46,4 +50,21 @@ final class FileHashComputer
{
return md5(serialize($array));
}
private function createFileLoader(string $filePath, ContainerBuilder $containerBuilder): LoaderInterface
{
$fileLocator = new FileLocator([dirname($filePath)]);
$loaderResolver = new LoaderResolver([
new GlobFileLoader($containerBuilder, $fileLocator),
new PhpFileLoader($containerBuilder, $fileLocator),
new YamlFileLoader($containerBuilder, $fileLocator),
]);
$loader = $loaderResolver->resolve($filePath);
if (! $loader) {
throw new ShouldNotHappenException();
}
return $loader;
}
}

View File

@ -43,6 +43,6 @@ final class FileHashComputerTest extends AbstractKernelTestCase
public function testInvalidType(): void
{
$this->expectException(ShouldNotHappenException::class);
$this->fileHashComputer->compute(__DIR__ . '/Source/file.php');
$this->fileHashComputer->compute(__DIR__ . '/Source/file.xml');
}
}

View File

@ -31,7 +31,12 @@ return static function (ContainerConfigurator $containerConfigurator): void {
->args(['$markdownDiffer' => ref('markdownDiffer')]);
$services->set('diffOutputBuilder', StrictUnifiedDiffOutputBuilder::class)
->args(['$options' => ['fromFile' => 'Original', 'toFile' => 'New']]);
->args([
'$options' => [
'fromFile' => 'Original',
'toFile' => 'New',
],
]);
$services->set('differ', Differ::class)
->args([ref('diffOutputBuilder')]);

View File

@ -7,39 +7,46 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use PHPUnit\Framework\TestCase;
use Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\Core\Configuration\Option;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReturnArrayClassMethodToYieldRector::class)
->args([
'$methodsByType' => [
TestCase::class => ['provideData', 'provideData*', 'dataProvider', 'dataProvider*'],
],
->arg('$methodsByType', [
TestCase::class => ['provideData', 'provideData*', 'dataProvider', 'dataProvider*'],
]);
$parameters = $containerConfigurator->parameters();
$parameters->set(
'sets',
['coding-style', 'code-quality', 'dead-code', 'nette-utils-code-quality', 'solid', 'privatization', 'naming']
);
$parameters->set(Option::SETS, [
'coding-style', 'code-quality', 'dead-code', 'nette-utils-code-quality', 'solid', 'privatization', 'naming',
]);
$parameters->set(
'paths',
['src', 'rules', 'packages', 'tests', 'utils', 'compiler/src', 'compiler/bin/compile', 'compiler/build/scoper.inc.php']
);
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__DIR__ . '/rules',
__DIR__ . '/packages',
__DIR__ . '/tests',
__DIR__ . '/utils',
__DIR__ . '/compiler/src',
__DIR__ . '/compiler/bin/compile',
__DIR__ . '/compiler/build/scoper.inc.php',
__DIR__ . '/config/set',
]);
$parameters->set('autoload_paths', ['compiler/src']);
$parameters->set(Option::AUTOLOAD_PATHS, [__DIR__ . '/compiler/src']);
$parameters->set(
'exclude_paths',
['/Fixture/', '/Source/', '/Expected/', 'packages/doctrine-annotation-generated/src/*']
);
$parameters->set(Option::EXCLUDE_PATHS, [
'/Fixture/', '/Source/', '/Expected/', __DIR__ . '/packages/doctrine-annotation-generated/src/*',
]);
$parameters->set(
'exclude_rectors',
[StringClassNameToClassConstantRector::class, SplitStringClassConstantToClassConstFetchRector::class]
);
$parameters->set(Option::EXCLUDE_RECTORS, [
StringClassNameToClassConstantRector::class,
SplitStringClassConstantToClassConstFetchRector::class,
// false positives on constants used in rector-ci.php
RemoveUnusedClassConstantRector::class,
]);
};

View File

@ -59,7 +59,7 @@ final class Option
/**
* @var string
*/
public const EXCLUDE_RECTORS_PARAMETER = 'exclude_rectors';
public const EXCLUDE_RECTORS = 'exclude_rectors';
/**
* @var string
@ -100,4 +100,29 @@ final class Option
* @var string
*/
public const CACHE_DEBUG = 'cache-debug';
/**
* @var string
*/
public const PATHS = 'paths';
/**
* @var string
*/
public const AUTOLOAD_PATHS = 'autoload_paths';
/**
* @var string
*/
public const EXCLUDE_PATHS = 'exclude_paths';
/**
* @var string
*/
public const SETS = 'sets';
/**
* @var string
*/
public const FILE_EXTENSIONS = 'file_extensions';
}

View File

@ -61,7 +61,7 @@ final class RemoveExcludedRectorsCompilerPass implements CompilerPassInterface
throw new ShouldNotHappenException(sprintf(
'Class "%s" defined in "parameters > %s" was not found ',
$excludedRector,
Option::EXCLUDE_RECTORS_PARAMETER
Option::EXCLUDE_RECTORS
));
}
@ -74,7 +74,7 @@ final class RemoveExcludedRectorsCompilerPass implements CompilerPassInterface
throw new ShouldNotHappenException(sprintf(
'Class "%s" defined in "parameters > %s" is not a Rector rule = does not implement "%s" ',
$excludedRector,
Option::EXCLUDE_RECTORS_PARAMETER,
Option::EXCLUDE_RECTORS,
RectorInterface::class
));
}