Skip to content

Commit

Permalink
Updated Rector to commit bcb51e81bc9cbc667017eea9240d7f8564848768
Browse files Browse the repository at this point in the history
rectorphp/rector-src@bcb51e8 [Renaming] Deprecate PseudoNamespaceToNamespaceRector as too dynamic and unreliable, use the RenameClassRector instead (#4755)
  • Loading branch information
TomasVotruba committed Aug 10, 2023
1 parent dde9963 commit 50e22e8
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 293 deletions.
1 change: 0 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
__DIR__ . '/../packages/PhpDocParser/PhpDocParser/PhpDocNodeVisitor/CallablePhpDocNodeVisitor.php',
__DIR__ . '/../packages/PHPStanStaticTypeMapper/Enum',
__DIR__ . '/../packages/Caching/Cache.php',
__DIR__ . '/../packages/NodeTypeResolver/PhpDocNodeVisitor/UnderscoreRenamePhpDocNodeVisitor.php',
__DIR__ . '/../packages/NodeTypeResolver/PHPStan/ObjectWithoutClassTypeWithParentTypes.php',
// used in PHPStan
__DIR__ . '/../packages/NodeTypeResolver/Reflection/BetterReflection/RectorBetterReflectionSourceLocatorFactory.php',
Expand Down
33 changes: 0 additions & 33 deletions packages/NodeTypeResolver/PhpDoc/PhpDocTypeRenamer.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,19 @@
declare (strict_types=1);
namespace Rector\Renaming\Rector\FileWithoutNamespace;

use RectorPrefix202308\Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\PhpDoc\PhpDocTypeRenamer;
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202308\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector\PseudoNamespaceToNamespaceRectorTest
* @api deprecated and soon to be removed
*/
final class PseudoNamespaceToNamespaceRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\PhpDoc\PhpDocTypeRenamer
*/
private $phpDocTypeRenamer;
/**
* @see https://regex101.com/r/chvLgs/1/
* @var string
*/
private const SPLIT_BY_UNDERSCORE_REGEX = '#([a-zA-Z])(_)?(_)([a-zA-Z])#';
/**
* @var PseudoNamespaceToNamespace[]
*/
private $pseudoNamespacesToNamespaces = [];
/**
* @var string|null
*/
private $newNamespace;
public function __construct(PhpDocTypeRenamer $phpDocTypeRenamer)
{
$this->phpDocTypeRenamer = $phpDocTypeRenamer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Replaces defined Pseudo_Namespaces by Namespace\\Ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
Expand Down Expand Up @@ -75,132 +43,16 @@ public function getNodeTypes() : array
*/
public function refactor(Node $node) : ?Node
{
$this->newNamespace = null;
if ($node instanceof FileWithoutNamespace) {
return $this->refactorFileWithoutNamespace($node);
}
return $this->refactorNamespace($node);
$errorMessage = \sprintf('Rule "%s" is deprecated, as unreliable and too dynamic. Use more robuts RenameClassRector instead.', self::class);
\trigger_error($errorMessage, \E_USER_WARNING);
\sleep(3);
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, PseudoNamespaceToNamespace::class);
$this->pseudoNamespacesToNamespaces = $configuration;
}
/**
* @param Stmt[] $stmts
* @return Stmt[]|null
*/
private function refactorStmts(array $stmts) : ?array
{
$hasChanged = \false;
$this->traverseNodesWithCallable($stmts, function (Node $node) use(&$hasChanged) : ?Node {
if (!$node instanceof Name && !$node instanceof Identifier && !$node instanceof Property && !$node instanceof FunctionLike) {
return null;
}
if ($this->refactorPhpDoc($node)) {
$hasChanged = \true;
}
// @todo - update rule to allow for bool instanceof check
if ($node instanceof Name || $node instanceof Identifier) {
$changedNode = $this->processNameOrIdentifier($node);
if ($changedNode instanceof Node) {
$hasChanged = \true;
return $changedNode;
}
}
return null;
});
if ($hasChanged) {
return $stmts;
}
return null;
}
/**
* @return Identifier|Name|null
* @param \PhpParser\Node\Name|\PhpParser\Node\Identifier $node
*/
private function processNameOrIdentifier($node) : ?Node
{
// no name → skip
if ($node->toString() === '') {
return null;
}
foreach ($this->pseudoNamespacesToNamespaces as $pseudoNamespaceToNamespace) {
if (!$this->isName($node, $pseudoNamespaceToNamespace->getNamespacePrefix() . '*')) {
continue;
}
$excludedClasses = $pseudoNamespaceToNamespace->getExcludedClasses();
if ($excludedClasses !== [] && $this->isNames($node, $excludedClasses)) {
return null;
}
if ($node instanceof Name) {
return $this->processName($node);
}
return $this->processIdentifier($node);
}
return null;
}
private function processName(Name $name) : Name
{
$nodeName = $this->getName($name);
return $name instanceof FullyQualified ? new FullyQualified(\explode('_', $nodeName), $name->getAttributes()) : new Name(\explode('_', $nodeName), $name->getAttributes());
}
private function processIdentifier(Identifier $identifier) : ?Identifier
{
$name = $this->getName($identifier);
if ($name === null) {
return null;
}
/** @var string $namespaceName */
$namespaceName = Strings::before($name, '_', -1);
/** @var string $lastNewNamePart */
$lastNewNamePart = Strings::after($name, '_', -1);
$newNamespace = Strings::replace($namespaceName, self::SPLIT_BY_UNDERSCORE_REGEX, '$1$2\\\\$4');
if ($this->newNamespace !== null && $this->newNamespace !== $newNamespace) {
throw new ShouldNotHappenException('There cannot be 2 different namespaces in one file');
}
$this->newNamespace = $newNamespace;
$identifier->name = $lastNewNamePart;
return $identifier;
}
private function refactorNamespace(Namespace_ $namespace) : ?Namespace_
{
$changedStmts = $this->refactorStmts($namespace->stmts);
if ($changedStmts === null) {
return null;
}
return $namespace;
}
/**
* @param \PhpParser\Node\Name|\PhpParser\Node\FunctionLike|\PhpParser\Node\Identifier|\PhpParser\Node\Stmt\Property $node
*/
private function refactorPhpDoc($node) : bool
{
$hasChanged = \false;
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
// replace on @var/@param/@return/@throws
foreach ($this->pseudoNamespacesToNamespaces as $pseudoNamespaceToNamespace) {
$hasDocTypeChanged = $this->phpDocTypeRenamer->changeUnderscoreType($phpDocInfo, $node, $pseudoNamespaceToNamespace);
if ($hasDocTypeChanged) {
$hasChanged = \true;
}
}
return $hasChanged;
}
private function refactorFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace) : ?Namespace_
{
$changedStmts = $this->refactorStmts($fileWithoutNamespace->stmts);
if ($changedStmts === null) {
return null;
}
$fileWithoutNamespace->stmts = $changedStmts;
// add a new namespace?
if ($this->newNamespace !== null) {
return new Namespace_(new Name($this->newNamespace), $changedStmts);
}
return null;
// for BC
}
}
3 changes: 3 additions & 0 deletions rules/Renaming/ValueObject/PseudoNamespaceToNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
declare (strict_types=1);
namespace Rector\Renaming\ValueObject;

/**
* @api deprecated, soon to be removed
*/
final class PseudoNamespaceToNamespace
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'f294e6df400814dbd8125ba141b16d96930716c5';
public const PACKAGE_VERSION = 'bcb51e81bc9cbc667017eea9240d7f8564848768';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-08-10 11:10:16';
public const RELEASE_DATE = '2023-08-10 12:44:08';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit6d3b3aba1b36e46f7d925f0c660f452e::getLoader();
return ComposerAutoloaderInit474f93b1535aa0fa14eb116db0880ba2::getLoader();
2 changes: 0 additions & 2 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1982,11 +1982,9 @@
'Rector\\NodeTypeResolver\\PHPStan\\Type\\TypeFactory' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php',
'Rector\\NodeTypeResolver\\PhpDocNodeVisitor\\ClassRenamePhpDocNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PhpDocNodeVisitor/ClassRenamePhpDocNodeVisitor.php',
'Rector\\NodeTypeResolver\\PhpDocNodeVisitor\\NameImportingPhpDocNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PhpDocNodeVisitor/NameImportingPhpDocNodeVisitor.php',
'Rector\\NodeTypeResolver\\PhpDocNodeVisitor\\UnderscoreRenamePhpDocNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PhpDocNodeVisitor/UnderscoreRenamePhpDocNodeVisitor.php',
'Rector\\NodeTypeResolver\\PhpDoc\\NodeAnalyzer\\DocBlockClassRenamer' => $baseDir . '/packages/NodeTypeResolver/PhpDoc/NodeAnalyzer/DocBlockClassRenamer.php',
'Rector\\NodeTypeResolver\\PhpDoc\\NodeAnalyzer\\DocBlockNameImporter' => $baseDir . '/packages/NodeTypeResolver/PhpDoc/NodeAnalyzer/DocBlockNameImporter.php',
'Rector\\NodeTypeResolver\\PhpDoc\\NodeAnalyzer\\DocBlockTagReplacer' => $baseDir . '/packages/NodeTypeResolver/PhpDoc/NodeAnalyzer/DocBlockTagReplacer.php',
'Rector\\NodeTypeResolver\\PhpDoc\\PhpDocTypeRenamer' => $baseDir . '/packages/NodeTypeResolver/PhpDoc/PhpDocTypeRenamer.php',
'Rector\\NodeTypeResolver\\Reflection\\BetterReflection\\RectorBetterReflectionSourceLocatorFactory' => $baseDir . '/packages/NodeTypeResolver/Reflection/BetterReflection/RectorBetterReflectionSourceLocatorFactory.php',
'Rector\\NodeTypeResolver\\Reflection\\BetterReflection\\SourceLocatorProvider\\DynamicSourceLocatorProvider' => $baseDir . '/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php',
'Rector\\NodeTypeResolver\\Reflection\\BetterReflection\\SourceLocator\\IntermediateSourceLocator' => $baseDir . '/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocator/IntermediateSourceLocator.php',
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit6d3b3aba1b36e46f7d925f0c660f452e
class ComposerAutoloaderInit474f93b1535aa0fa14eb116db0880ba2
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInit6d3b3aba1b36e46f7d925f0c660f452e', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit474f93b1535aa0fa14eb116db0880ba2', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit6d3b3aba1b36e46f7d925f0c660f452e', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit474f93b1535aa0fa14eb116db0880ba2', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6d3b3aba1b36e46f7d925f0c660f452e::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit474f93b1535aa0fa14eb116db0880ba2::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit6d3b3aba1b36e46f7d925f0c660f452e::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit474f93b1535aa0fa14eb116db0880ba2::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down

0 comments on commit 50e22e8

Please sign in to comment.