diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 0c57e5f16f4..0a72ab18e9e 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 391 Rules Overview +# 390 Rules Overview
@@ -22,7 +22,7 @@ - [Naming](#naming) (6) -- [PSR4](#psr4) (2) +- [PSR4](#psr4) (1) - [Php52](#php52) (2) @@ -4110,38 +4110,6 @@ Change multiple classes in one file to standalone PSR-4 classes.
-### NormalizeNamespaceByPSR4ComposerAutoloadRector - -Adds namespace to namespace-less files or correct namespace to match PSR-4 in `composer.json` autoload section. Run with combination with "Rector\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector" - -- class: [`Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector`](../rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php) - -- with `composer.json`: - -```json -{ - "autoload": { - "psr-4": { - "App\\CustomNamespace\\": "src" - } - } -} -``` - -↓ - -```diff - // src/SomeClass.php - -+namespace App\CustomNamespace; -+ - class SomeClass - { - } -``` - -
- ## Php52 ### ContinueToBreakInSwitchRector diff --git a/config/config.php b/config/config.php index 34b9b58a044..ba628b70c6e 100644 --- a/config/config.php +++ b/config/config.php @@ -41,8 +41,6 @@ use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PhpDocParser\PhpParser\SmartPhpParser; use Rector\PhpDocParser\PhpParser\SmartPhpParserFactory; -use Rector\PSR4\Composer\PSR4NamespaceMatcher; -use Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface; use Rector\Utils\Command\MissingInSetCommand; use Rector\Utils\Command\OutsideAnySetCommand; use Symfony\Component\Console\Application; @@ -103,9 +101,6 @@ __DIR__ . '/../packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php', ]); - // psr-4 - $services->alias(PSR4AutoloadNamespaceMatcherInterface::class, PSR4NamespaceMatcher::class); - $services->load('Rector\\', __DIR__ . '/../rules') ->exclude([ __DIR__ . '/../rules/*/ValueObject/*', diff --git a/config/set/psr-4.php b/config/set/psr-4.php index a5ef722a5b2..a2109fa5cd2 100644 --- a/config/set/psr-4.php +++ b/config/set/psr-4.php @@ -3,10 +3,8 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector; use Rector\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class); $rectorConfig->rule(MultipleClassFileToPsr4ClassesRector::class); }; diff --git a/phpstan.neon b/phpstan.neon index 485118c23d9..d393385a6c5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -267,7 +267,6 @@ parameters: - packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php - rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php - rules/Php70/EregToPcreTransformer.php - - rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php - rules/Renaming/NodeManipulator/ClassRenamer.php - '#Method Rector\\Core\\Application\\ApplicationFileProcessor\:\:runParallel\(\) should return array\{system_errors\: array, file_diffs\: array\} but returns array#' diff --git a/rules-tests/PSR4/Composer/Fixture-dashed/Config.php b/rules-tests/PSR4/Composer/Fixture-dashed/Config.php deleted file mode 100644 index eaafffd69ff..00000000000 --- a/rules-tests/PSR4/Composer/Fixture-dashed/Config.php +++ /dev/null @@ -1,7 +0,0 @@ -boot(); - $this->psr4NamespaceMatcher = $this->getService(PSR4NamespaceMatcher::class); - } - - public function test(): void - { - $filePath = __DIR__ . '/Fixture-dashed/Config.php'; - $file = new File($filePath, FileSystem::read($filePath)); - - $expectedNamespace = $this->psr4NamespaceMatcher->getExpectedNamespace($file, new FileWithoutNamespace([])); - $this->assertNull($expectedNamespace); - } -} diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/case_insensitive.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/case_insensitive.php.inc deleted file mode 100644 index 06223ddc8cc..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/case_insensitive.php.inc +++ /dev/null @@ -1,23 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/do_not_change_next_namespace.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/do_not_change_next_namespace.php.inc deleted file mode 100644 index 2bffe2c2551..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/do_not_change_next_namespace.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/group_use_class.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/group_use_class.php.inc deleted file mode 100644 index 7ae34985c08..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/group_use_class.php.inc +++ /dev/null @@ -1,42 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/namespace_less_no_declare_strict_types.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/namespace_less_no_declare_strict_types.php.inc deleted file mode 100644 index c96d5a41406..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/namespace_less_no_declare_strict_types.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/no_prefix_consts.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/no_prefix_consts.php.inc deleted file mode 100644 index c0514e3b3cd..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/no_prefix_consts.php.inc +++ /dev/null @@ -1,45 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/normalize_file.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/normalize_file.php.inc deleted file mode 100644 index 41a1c2103d4..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/normalize_file.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_already_defined_namespace.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_already_defined_namespace.php.inc deleted file mode 100644 index 1af56349bc4..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_already_defined_namespace.php.inc +++ /dev/null @@ -1,18 +0,0 @@ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_inline_html.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_inline_html.php.inc deleted file mode 100644 index 50ebb59f064..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_inline_html.php.inc +++ /dev/null @@ -1 +0,0 @@ -

{{ content }}

diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/wrong_namespace.php.inc b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/wrong_namespace.php.inc deleted file mode 100644 index a1caad7146b..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/wrong_namespace.php.inc +++ /dev/null @@ -1,19 +0,0 @@ - ------ - diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/NormalizeNamespaceByPSR4ComposerAutoloadRectorTest.php b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/NormalizeNamespaceByPSR4ComposerAutoloadRectorTest.php deleted file mode 100644 index c99fe8dca1a..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/NormalizeNamespaceByPSR4ComposerAutoloadRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Source/DummyPSR4AutoloadWithoutNamespaceMatcher.php b/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Source/DummyPSR4AutoloadWithoutNamespaceMatcher.php deleted file mode 100644 index ad7d4af2e26..00000000000 --- a/rules-tests/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Source/DummyPSR4AutoloadWithoutNamespaceMatcher.php +++ /dev/null @@ -1,17 +0,0 @@ -rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class); - - $services = $rectorConfig->services(); - $services->set(DummyPSR4AutoloadWithoutNamespaceMatcher::class); - $services->alias(PSR4AutoloadNamespaceMatcherInterface::class, DummyPSR4AutoloadWithoutNamespaceMatcher::class); -}; diff --git a/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php b/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php deleted file mode 100644 index 215832a08a0..00000000000 --- a/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php +++ /dev/null @@ -1,56 +0,0 @@ -> - */ - private array $cachedComposerJsonPSR4AutoloadPaths = []; - - /** - * @return array - */ - public function provide(): array - { - if ($this->cachedComposerJsonPSR4AutoloadPaths !== []) { - return $this->cachedComposerJsonPSR4AutoloadPaths; - } - - $fileContents = FileSystem::read($this->getComposerJsonPath()); - $composerJson = Json::decode($fileContents, Json::FORCE_ARRAY); - $psr4Autoloads = array_merge( - $composerJson['autoload']['psr-4'] ?? [], - $composerJson['autoload-dev']['psr-4'] ?? [] - ); - - $this->cachedComposerJsonPSR4AutoloadPaths = $this->removeEmptyNamespaces($psr4Autoloads); - - return $this->cachedComposerJsonPSR4AutoloadPaths; - } - - private function getComposerJsonPath(): string - { - // assume the project has "composer.json" in root directory - return getcwd() . '/composer.json'; - } - - /** - * @param array> $psr4Autoloads - * @return array> - */ - private function removeEmptyNamespaces(array $psr4Autoloads): array - { - return array_filter( - $psr4Autoloads, - static fn (string $psr4Autoload): bool => $psr4Autoload !== '', - ARRAY_FILTER_USE_KEY - ); - } -} diff --git a/rules/PSR4/Composer/PSR4NamespaceMatcher.php b/rules/PSR4/Composer/PSR4NamespaceMatcher.php deleted file mode 100644 index baf818b0059..00000000000 --- a/rules/PSR4/Composer/PSR4NamespaceMatcher.php +++ /dev/null @@ -1,65 +0,0 @@ -getFilePath(); - $psr4Autoloads = $this->psr4AutoloadPathsProvider->provide(); - - foreach ($psr4Autoloads as $namespace => $path) { - // remove extra slash - $paths = is_array($path) ? $path : [$path]; - - foreach ($paths as $path) { - $relativeFilePath = $this->filePathHelper->relativePath($filePath); - $relativeDirectoryPath = dirname($relativeFilePath); - - $path = rtrim($path, '/'); - if (! \str_starts_with($relativeDirectoryPath, $path)) { - continue; - } - - $expectedNamespace = $namespace . $this->resolveExtraNamespace($relativeDirectoryPath, $path); - - if (str_contains($expectedNamespace, '-')) { - return null; - } - - return rtrim($expectedNamespace, '\\'); - } - } - - return null; - } - - /** - * Get the extra path that is not included in root PSR-4 namespace - */ - private function resolveExtraNamespace(string $relativeDirectoryPath, string $path): string - { - $extraNamespace = Strings::substring($relativeDirectoryPath, Strings::length($path) + 1); - $extraNamespace = Strings::replace($extraNamespace, '#/#', '\\'); - - return trim($extraNamespace); - } -} diff --git a/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php b/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php deleted file mode 100644 index d1d78496342..00000000000 --- a/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php +++ /dev/null @@ -1,13 +0,0 @@ -parameterProvider->provideBoolParameter(Option::AUTO_IMPORT_NAMES)) { - return; - } - - // FQNize all class names - $this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use ( - $scope - ): ?FullyQualified { - if (! $node instanceof Name) { - return null; - } - - $name = $this->nodeNameResolver->getName($node); - if (in_array($name, [ObjectReference::STATIC, ObjectReference::PARENT, ObjectReference::SELF], true)) { - return null; - } - - if ($this->isNativeConstant($node, $scope)) { - return null; - } - - $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof GroupUse) { - $parentNode->setAttribute(AttributeKey::ORIGINAL_NODE, null); - return null; - } - - if ($parentNode instanceof UseUse) { - return null; - } - - return new FullyQualified($name); - }); - } - - private function isNativeConstant(Name $name, Scope $scope): bool - { - $parentNode = $name->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentNode instanceof ConstFetch) { - return false; - } - - if (! $this->reflectionProvider->hasConstant($name, $scope)) { - return false; - } - - $globalConstantReflection = $this->reflectionProvider->getConstant($name, $scope); - return $globalConstantReflection instanceof RuntimeConstantReflection; - } -} diff --git a/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php b/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php deleted file mode 100644 index f63b6a25fbc..00000000000 --- a/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php +++ /dev/null @@ -1,155 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [Namespace_::class, FileWithoutNamespace::class]; - } - - /** - * @param FileWithoutNamespace|Namespace_ $node - * @return Node|null|Stmt[] - */ - public function refactorWithScope(Node $node, Scope $scope): Node|null|array - { - $expectedNamespace = $this->psr4AutoloadNamespaceMatcher->getExpectedNamespace($this->file, $node); - if ($expectedNamespace === null) { - return null; - } - - // is namespace and already correctly named? - if ($node instanceof Namespace_ && $this->nodeNameResolver->isCaseSensitiveName( - $node, - $expectedNamespace - )) { - return null; - } - - if ($node instanceof Namespace_ && $this->hasNamespaceInPreviousNamespace($node)) { - return null; - } - - // to put declare_strict types on correct place - if ($node instanceof FileWithoutNamespace) { - return $this->refactorFileWithoutNamespace($node, $expectedNamespace, $scope); - } - - $node->name = new Name($expectedNamespace); - $this->fullyQualifyStmtsAnalyzer->process($node->stmts, $scope); - - return $node; - } - - private function hasNamespaceInPreviousNamespace(Namespace_ $namespace): bool - { - return (bool) $this->betterNodeFinder->findFirstPrevious( - $namespace, - static fn (Node $node): bool => $node instanceof Namespace_ - ); - } - - /** - * @return Namespace_|Stmt[] - */ - private function refactorFileWithoutNamespace( - FileWithoutNamespace $fileWithoutNamespace, - string $expectedNamespace, - Scope $scope - ): Namespace_|array { - $nodes = $fileWithoutNamespace->stmts; - - $declare = null; - - foreach ($nodes as $key => $fileWithoutNamespace) { - if ($key > 0) { - break; - } - - if ($fileWithoutNamespace instanceof Declare_) { - $declare = $fileWithoutNamespace; - unset($nodes[$key]); - } - } - - $namespace = new Namespace_(new Name($expectedNamespace), $nodes); - - $this->fullyQualifyStmtsAnalyzer->process($nodes, $scope); - - if ($declare instanceof Declare_) { - return [$declare, $namespace]; - } - - return $namespace; - } -}