diff --git a/config/config.php b/config/config.php index acc97294359..8ccd0ab932c 100644 --- a/config/config.php +++ b/config/config.php @@ -42,8 +42,6 @@ use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\Dy 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 RectorPrefix202306\Symfony\Component\Console\Application; @@ -87,8 +85,6 @@ return static function (RectorConfig $rectorConfig) : void { __DIR__ . '/../packages/NodeTypeResolver/Reflection/BetterReflection/RectorBetterReflectionSourceLocatorFactory.php', __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/*', __DIR__ . '/../rules/*/Rector/*', __DIR__ . '/../rules/*/Contract/*', __DIR__ . '/../rules/*/Exception/*', __DIR__ . '/../rules/*/Enum/*']); $services->set(Filesystem::class); // use faster in-memory cache in CI. diff --git a/config/set/psr-4.php b/config/set/psr-4.php index 416c33fdfaf..50f72bde672 100644 --- a/config/set/psr-4.php +++ b/config/set/psr-4.php @@ -4,9 +4,7 @@ declare (strict_types=1); namespace RectorPrefix202306; 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/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 0c57e5f16f4..0a72ab18e9e 100644 --- a/docs/rector_rules_overview.md +++ b/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/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php b/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php deleted file mode 100644 index 2c0022118b2..00000000000 --- a/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php +++ /dev/null @@ -1,43 +0,0 @@ -> - */ - private $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 function (string $psr4Autoload) : bool { - return $psr4Autoload !== ''; - }, \ARRAY_FILTER_USE_KEY); - } -} diff --git a/rules/PSR4/Composer/PSR4NamespaceMatcher.php b/rules/PSR4/Composer/PSR4NamespaceMatcher.php deleted file mode 100644 index fc6dc9a49ae..00000000000 --- a/rules/PSR4/Composer/PSR4NamespaceMatcher.php +++ /dev/null @@ -1,63 +0,0 @@ -psr4AutoloadPathsProvider = $psr4AutoloadPathsProvider; - $this->filePathHelper = $filePathHelper; - } - public function getExpectedNamespace(File $file, Node $node) : ?string - { - $filePath = $file->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 (\strncmp($relativeDirectoryPath, $path, \strlen($path)) !== 0) { - continue; - } - $expectedNamespace = $namespace . $this->resolveExtraNamespace($relativeDirectoryPath, $path); - if (\strpos($expectedNamespace, '-') !== \false) { - 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 f15c6a7650a..00000000000 --- a/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -parameterProvider = $parameterProvider; - $this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser; - $this->nodeNameResolver = $nodeNameResolver; - $this->reflectionProvider = $reflectionProvider; - } - /** - * @param Stmt[] $stmts - */ - public function process(array $stmts, Scope $scope) : void - { - // no need to - if ($this->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 e15ece1c404..00000000000 --- a/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php +++ /dev/null @@ -1,130 +0,0 @@ -psr4AutoloadNamespaceMatcher = $psr4AutoloadNamespaceMatcher; - $this->fullyQualifyStmtsAnalyzer = $fullyQualifyStmtsAnalyzer; - } - public function getRuleDefinition() : RuleDefinition - { - $description = \sprintf('Adds namespace to namespace-less files or correct namespace to match PSR-4 in `composer.json` autoload section. Run with combination with "%s"', MultipleClassFileToPsr4ClassesRector::class); - return new RuleDefinition($description, [new ComposerJsonAwareCodeSample(<<<'CODE_SAMPLE' -// src/SomeClass.php - -class SomeClass -{ -} -CODE_SAMPLE -, <<<'CODE_SAMPLE' -// src/SomeClass.php - -namespace App\CustomNamespace; - -class SomeClass -{ -} -CODE_SAMPLE -, <<<'CODE_SAMPLE' -{ - "autoload": { - "psr-4": { - "App\\CustomNamespace\\": "src" - } - } -} -CODE_SAMPLE -)]); - } - /** - * @return array> - */ - public function getNodeTypes() : array - { - return [Namespace_::class, FileWithoutNamespace::class]; - } - /** - * @param FileWithoutNamespace|Namespace_ $node - * @return Node|null|Stmt[] - */ - public function refactorWithScope(Node $node, Scope $scope) - { - $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 function (Node $node) : bool { - return $node instanceof Namespace_; - }); - } - /** - * @return Namespace_|Stmt[] - */ - private function refactorFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace, string $expectedNamespace, Scope $scope) - { - $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; - } -} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 3b6fa9f737e..914b5f29d50 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '203be2b3076a4e7f2f80cee721aa20186868a604'; + public const PACKAGE_VERSION = '1d8b12dc022dc4ff9c9c26a2fc1ea409027d271b'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-06-03 18:23:04'; + public const RELEASE_DATE = '2023-06-03 16:32:43'; /** * @var int */ diff --git a/vendor/autoload.php b/vendor/autoload.php index 074e1fd48fa..cd55e7ad92a 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInitd84de2cf53039de1be8691008b648fd4::getLoader(); +return ComposerAutoloaderInit14084fdb3a7d29db8393e29bc7ca4896::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 27529d8df0c..47edb73317d 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2115,13 +2115,8 @@ return array( 'Rector\\PHPUnit\\ValueObject\\DataProviderClassMethodRecipe' => $vendorDir . '/rector/rector-phpunit/src/ValueObject/DataProviderClassMethodRecipe.php', 'Rector\\PHPUnit\\ValueObject\\FunctionNameWithAssertMethods' => $vendorDir . '/rector/rector-phpunit/src/ValueObject/FunctionNameWithAssertMethods.php', 'Rector\\PHPUnit\\ValueObject\\ParamAndArg' => $vendorDir . '/rector/rector-phpunit/src/ValueObject/ParamAndArg.php', - 'Rector\\PSR4\\Composer\\PSR4AutoloadPathsProvider' => $baseDir . '/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php', - 'Rector\\PSR4\\Composer\\PSR4NamespaceMatcher' => $baseDir . '/rules/PSR4/Composer/PSR4NamespaceMatcher.php', - 'Rector\\PSR4\\Contract\\PSR4AutoloadNamespaceMatcherInterface' => $baseDir . '/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php', 'Rector\\PSR4\\FileInfoAnalyzer\\FileInfoDeletionAnalyzer' => $baseDir . '/rules/PSR4/FileInfoAnalyzer/FileInfoDeletionAnalyzer.php', - 'Rector\\PSR4\\NodeManipulator\\FullyQualifyStmtsAnalyzer' => $baseDir . '/rules/PSR4/NodeManipulator/FullyQualifyStmtsAnalyzer.php', 'Rector\\PSR4\\NodeManipulator\\NamespaceManipulator' => $baseDir . '/rules/PSR4/NodeManipulator/NamespaceManipulator.php', - 'Rector\\PSR4\\Rector\\FileWithoutNamespace\\NormalizeNamespaceByPSR4ComposerAutoloadRector' => $baseDir . '/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php', 'Rector\\PSR4\\Rector\\Namespace_\\MultipleClassFileToPsr4ClassesRector' => $baseDir . '/rules/PSR4/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector.php', 'Rector\\Parallel\\Application\\ParallelFileProcessor' => $baseDir . '/packages/Parallel/Application/ParallelFileProcessor.php', 'Rector\\Parallel\\Command\\WorkerCommandLineFactory' => $baseDir . '/packages/Parallel/Command/WorkerCommandLineFactory.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 4fc11ac382a..ec4a2d879b8 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInitd84de2cf53039de1be8691008b648fd4 +class ComposerAutoloaderInit14084fdb3a7d29db8393e29bc7ca4896 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInitd84de2cf53039de1be8691008b648fd4 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInitd84de2cf53039de1be8691008b648fd4', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit14084fdb3a7d29db8393e29bc7ca4896', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInitd84de2cf53039de1be8691008b648fd4', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit14084fdb3a7d29db8393e29bc7ca4896', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInitd84de2cf53039de1be8691008b648fd4::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInitd84de2cf53039de1be8691008b648fd4::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 6d254501d4b..83204ccdce5 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInitd84de2cf53039de1be8691008b648fd4 +class ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -2357,13 +2357,8 @@ class ComposerStaticInitd84de2cf53039de1be8691008b648fd4 'Rector\\PHPUnit\\ValueObject\\DataProviderClassMethodRecipe' => __DIR__ . '/..' . '/rector/rector-phpunit/src/ValueObject/DataProviderClassMethodRecipe.php', 'Rector\\PHPUnit\\ValueObject\\FunctionNameWithAssertMethods' => __DIR__ . '/..' . '/rector/rector-phpunit/src/ValueObject/FunctionNameWithAssertMethods.php', 'Rector\\PHPUnit\\ValueObject\\ParamAndArg' => __DIR__ . '/..' . '/rector/rector-phpunit/src/ValueObject/ParamAndArg.php', - 'Rector\\PSR4\\Composer\\PSR4AutoloadPathsProvider' => __DIR__ . '/../..' . '/rules/PSR4/Composer/PSR4AutoloadPathsProvider.php', - 'Rector\\PSR4\\Composer\\PSR4NamespaceMatcher' => __DIR__ . '/../..' . '/rules/PSR4/Composer/PSR4NamespaceMatcher.php', - 'Rector\\PSR4\\Contract\\PSR4AutoloadNamespaceMatcherInterface' => __DIR__ . '/../..' . '/rules/PSR4/Contract/PSR4AutoloadNamespaceMatcherInterface.php', 'Rector\\PSR4\\FileInfoAnalyzer\\FileInfoDeletionAnalyzer' => __DIR__ . '/../..' . '/rules/PSR4/FileInfoAnalyzer/FileInfoDeletionAnalyzer.php', - 'Rector\\PSR4\\NodeManipulator\\FullyQualifyStmtsAnalyzer' => __DIR__ . '/../..' . '/rules/PSR4/NodeManipulator/FullyQualifyStmtsAnalyzer.php', 'Rector\\PSR4\\NodeManipulator\\NamespaceManipulator' => __DIR__ . '/../..' . '/rules/PSR4/NodeManipulator/NamespaceManipulator.php', - 'Rector\\PSR4\\Rector\\FileWithoutNamespace\\NormalizeNamespaceByPSR4ComposerAutoloadRector' => __DIR__ . '/../..' . '/rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php', 'Rector\\PSR4\\Rector\\Namespace_\\MultipleClassFileToPsr4ClassesRector' => __DIR__ . '/../..' . '/rules/PSR4/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector.php', 'Rector\\Parallel\\Application\\ParallelFileProcessor' => __DIR__ . '/../..' . '/packages/Parallel/Application/ParallelFileProcessor.php', 'Rector\\Parallel\\Command\\WorkerCommandLineFactory' => __DIR__ . '/../..' . '/packages/Parallel/Command/WorkerCommandLineFactory.php', @@ -3066,9 +3061,9 @@ class ComposerStaticInitd84de2cf53039de1be8691008b648fd4 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInitd84de2cf53039de1be8691008b648fd4::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInitd84de2cf53039de1be8691008b648fd4::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInitd84de2cf53039de1be8691008b648fd4::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit14084fdb3a7d29db8393e29bc7ca4896::$classMap; }, null, ClassLoader::class); }