From 82fec5a9710debc033fe8104d0d44467fdad1eb2 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 13 Mar 2021 11:32:14 +0100 Subject: [PATCH] [CI] drop validate fixture class name, not needed with PSR-4 --- .github/workflows/code_analysis.yaml | 4 - .../ValidateFixtureClassnameCommand.php | 260 ------------------ .../src/Finder/FixtureFinder.php | 61 ---- .../src/Naming/ExpectedNameResolver.php | 38 --- .../src/Naming/NamespaceMatcher.php | 30 -- 5 files changed, 393 deletions(-) delete mode 100644 utils/project-validator/src/Command/ValidateFixtureClassnameCommand.php delete mode 100644 utils/project-validator/src/Finder/FixtureFinder.php delete mode 100644 utils/project-validator/src/Naming/ExpectedNameResolver.php delete mode 100644 utils/project-validator/src/Naming/NamespaceMatcher.php diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 610ed72699be..72b1904d4847 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -17,10 +17,6 @@ jobs: name: Validate Fixtures run: bin/rector validate-fixture-suffix --ansi - - - name: Validate Fixtures class name - run: bin/rector validate-fixture-classname --ansi - # make sure skipped files have "skip_" prefix - name: Validate Fixtures skip file prefix diff --git a/utils/project-validator/src/Command/ValidateFixtureClassnameCommand.php b/utils/project-validator/src/Command/ValidateFixtureClassnameCommand.php deleted file mode 100644 index 0f421fa9961c..000000000000 --- a/utils/project-validator/src/Command/ValidateFixtureClassnameCommand.php +++ /dev/null @@ -1,260 +0,0 @@ -symfonyStyle = $symfonyStyle; - $this->currentDirectory = getcwd(); - $this->smartFileSystem = $smartFileSystem; - $this->fixtureFinder = $fixtureFinder; - $this->namespaceMatcher = $namespaceMatcher; - $this->expectedNameResolver = $expectedNameResolver; - } - - protected function configure(): void - { - $this->addOption(Option::FIX, null, null, 'Fix found violations.'); - $this->setDescription('[CI] Validate tests fixtures class name'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $optionFix = (bool) $input->getOption(Option::FIX); - - $fixtureFileInfos = $this->fixtureFinder->findFixtureFileInfos(); - $incorrectClassNameFiles = []; - - foreach ($fixtureFileInfos as $fixtureFileInfo) { - // 1. geting expected namespace ... - $paths = explode('/tests/', (string) $fixtureFileInfo); - if (count($paths) > 2) { - continue; - } - - $path = ltrim(Strings::substring($paths[0], strlen($this->currentDirectory)) . '/tests', '/'); - - $expectedNamespace = $this->expectedNameResolver->resolve($path, $paths[1]); - if ($expectedNamespace === null) { - continue; - } - - // 2. reading file contents - $fileContent = $this->smartFileSystem->readFile((string) $fixtureFileInfo); - - $matchAll = Strings::matchAll($fileContent, self::NAMESPACE_REGEX); - $namespaceMatcherIsFoundCorrectNamespace = $this->namespaceMatcher->isFoundCorrectNamespace( - $matchAll, - $expectedNamespace - ); - - if (! $namespaceMatcherIsFoundCorrectNamespace) { - continue; - } - - $incorrectClassNameFiles = $this->checkAndFixClassName( - $fileContent, - $fixtureFileInfo, - $incorrectClassNameFiles, - $optionFix - ); - } - - if ($incorrectClassNameFiles !== []) { - $this->symfonyStyle->listing($incorrectClassNameFiles); - - $message = sprintf( - 'Found %d fixture files with invalid class name which not follow psr-4 defined in composer.json', - count($incorrectClassNameFiles) - ); - - if (! $optionFix) { - $message .= '. Just add "--fix" to console command and rerun to apply.'; - $this->symfonyStyle->error($message); - return ShellCode::ERROR; - } - - $this->symfonyStyle->success($message . ' and all fixtures are corrected', ); - return ShellCode::SUCCESS; - } - - $this->symfonyStyle->success('All fixtures are correct'); - return ShellCode::SUCCESS; - } - - /** - * @param string[] $incorrectClassNameFiles - * @return string[] - */ - private function checkAndFixClassName( - string $fileContent, - SmartFileInfo $fixtureFile, - array $incorrectClassNameFiles, - bool $optionFix - ): array { - $matchAll = Strings::matchAll($fileContent, self::CLASS_REGEX); - if ($matchAll === []) { - return $incorrectClassNameFiles; - } - if (count($matchAll) > 2) { - return $incorrectClassNameFiles; - } - - $fileName = Strings::substring($fixtureFile->getFileName(), 0, -8); - - if (in_array($fileName, self::EXCLUDE_NAMES, true)) { - return $incorrectClassNameFiles; - } - - $hasTrait = (bool) Strings::match($fileContent, self::CLASS_USE_TRAIT_REGEX); - if ($hasTrait) { - return $incorrectClassNameFiles; - } - - $fileName = str_replace('-', '_', $fileName); - $expectedClassName = ucfirst(StaticRectorStrings::uppercaseUnderscoreToCamelCase($fileName)); - $incorrectClassName = $this->getClassName($matchAll); - if ($expectedClassName === $incorrectClassName) { - return $incorrectClassNameFiles; - } - - $hasExtendsImplements = (bool) Strings::match($fileContent, self::CLASS_WITH_EXTENDS_IMPLEMENTS_REGEX); - if ($hasExtendsImplements) { - return $incorrectClassNameFiles; - } - - $incorrectClassNameFiles[] = (string) $fixtureFile; - - if ($optionFix) { - $this->fixClassName((string) $fixtureFile, $incorrectClassName, $fileContent, $expectedClassName); - } - - return $incorrectClassNameFiles; - } - - /** - * @param array> $matchAll - */ - private function getClassName(array $matchAll): string - { - return $matchAll[0][2]; - } - - private function fixClassName( - string $incorrectClassNameFile, - string $incorrectClassName, - string $incorrectFileContent, - string $expectedClassName - ): void { - $newContent = str_replace('class ' . $incorrectClassName, 'class ' . $expectedClassName, $incorrectFileContent); - $this->smartFileSystem->dumpFile($incorrectClassNameFile, $newContent); - } -} diff --git a/utils/project-validator/src/Finder/FixtureFinder.php b/utils/project-validator/src/Finder/FixtureFinder.php deleted file mode 100644 index b488054f1c2d..000000000000 --- a/utils/project-validator/src/Finder/FixtureFinder.php +++ /dev/null @@ -1,61 +0,0 @@ -finderSanitizer = $finderSanitizer; - } - - /** - * @return SmartFileInfo[] - */ - public function findFixtureFileInfos(): array - { - $finder = new Finder(); - $finder = $finder->files() - ->name('#\.php\.inc$#') - ->notName('#empty_file\.php\.inc$#') - ->path('#/Fixture(Php\d+)?/#') - ->notPath('#/blade-template/#') - ->notPath('#/Name/RenameClassRector/#') - ->notPath('#/Namespace_/RenameNamespaceRector/#') - ->notPath('#/TemplateAnnotationToThisRenderRector/#') - ->notPath('#/ParamTypeDeclarationRector/#') - ->notPath('#/ReturnTypeDeclarationRector/#') - ->notPath('#/PhpSpecToPHPUnitRector/#') - ->notPath('#/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Fixture/fixture3\.php\.inc$#') - ->notPath('#/SwapClassMethodArgumentsRector/Fixture/fixture\.php\.inc$#') - ->notPath('#bootstrap_names\.php\.inc$#') - ->notPath('#keep_anonymous_classes\.php\.inc$#') - ->notPath('#skip_different_order\.php\.inc$#') - ->notPath('#extended_parent\.php\.inc$#') - ->notPath('#trait_name\.php\.inc$#') - ->notPath('#normalize_file\.php\.inc$#') - ->notPath('#wrong_namespace\.php\.inc$#') - ->notPath('#stringy_calls\.php\.inc$#') - ->notPath('#delegating(_\d)?\.php\.inc$#') - ->notPath('#keep_annotated\.php\.inc$#') - ->notPath('#double_same_variable\.php\.inc$#') - ->notName('#_\.php\.inc$#') - ->notName('#Fixture/without_namespace.php.inc$#') - ->in(__DIR__ . '/../../../../tests') - ->in(__DIR__ . '/../../../../packages') - ->in(__DIR__ . '/../../../../rules'); - - return $this->finderSanitizer->sanitize($finder); - } -} diff --git a/utils/project-validator/src/Naming/ExpectedNameResolver.php b/utils/project-validator/src/Naming/ExpectedNameResolver.php deleted file mode 100644 index 5b32906e09ac..000000000000 --- a/utils/project-validator/src/Naming/ExpectedNameResolver.php +++ /dev/null @@ -1,38 +0,0 @@ - - */ - private $psr4autoloadPaths = []; - - public function __construct(PSR4AutoloadPathsProvider $psr4AutoloadPathsProvider) - { - $this->psr4autoloadPaths = $psr4AutoloadPathsProvider->provide(); - } - - public function resolve(string $path, string $relativePath): ?string - { - $directory = dirname($relativePath, PATHINFO_DIRNAME); - $relativePath = str_replace('/', '\\', $directory); - - foreach ($this->psr4autoloadPaths as $prefix => $psr4autoloadPath) { - if (! is_string($psr4autoloadPath)) { - continue; - } - - if ($psr4autoloadPath === $path) { - return $prefix . $relativePath; - } - } - - return null; - } -} diff --git a/utils/project-validator/src/Naming/NamespaceMatcher.php b/utils/project-validator/src/Naming/NamespaceMatcher.php deleted file mode 100644 index 297729865ea4..000000000000 --- a/utils/project-validator/src/Naming/NamespaceMatcher.php +++ /dev/null @@ -1,30 +0,0 @@ -> $matchAll - */ - public function isFoundCorrectNamespace(array $matchAll, string $expectedNamespace): bool - { - if ($matchAll === []) { - return true; - } - - $countMatchAll = count($matchAll); - if ($countMatchAll === 1 && $matchAll[0][1] === $expectedNamespace) { - return true; - } - if ($countMatchAll !== 2) { - return false; - } - if ($matchAll[0][1] !== $expectedNamespace) { - return false; - } - return $matchAll[1][1] === $expectedNamespace; - } -}