Updated Rector to commit 0315a82d57

0315a82d57 Add new option for auto importing names (#764)
This commit is contained in:
Tomas Votruba 2021-09-16 21:56:43 +00:00
parent cc2b048092
commit 4728cc7a69
9 changed files with 52 additions and 21 deletions

View File

@ -1,6 +1,6 @@
# Auto Import Names
Rector works with all class names as fully qualified by default, so it know the exact types. In most coding standard, that's not desired behavior, because short version with `use` statement is preferred:
Rector works with all class names as fully qualified by default, so it knows the exact types. In most coding standard, that's not desired behavior, because short version with `use` statement is preferred:
```diff
-$object = new \App\Some\Namespace\SomeClass();
@ -48,6 +48,16 @@ Do you want to keep those?
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);
```
<br>
If you have set Option::AUTO_IMPORT_NAMES to true, rector is applying this to every analyzed file, even if no real change by a rector was applied to the file.
The reason is that a so-called post rector is responsible for this, namely the NameImportingPostRector.
If you like to apply the Option::AUTO_IMPORT_NAMES only for real changed files, you can configure this.
```php
$parameters->set(Option::APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY, true);
```
## How to Remove Unused Imports?
To remove imports, use [ECS](github.com/symplify/easy-coding-standard) with [`NoUnusedImportsFixer`](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.18/doc/rules/import/no_unused_imports.rst) rule:

View File

@ -68,16 +68,22 @@ final class NameImportingPostRector extends \Rector\PostRector\Rector\AbstractPo
if (!$this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::AUTO_IMPORT_NAMES)) {
return null;
}
$file = $this->currentFileProvider->getFile();
if ($node instanceof \PhpParser\Node\Name) {
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
if (!$this->shouldApply($file)) {
return null;
}
return $this->processNodeName($node, $file);
}
if (!$this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::IMPORT_DOC_BLOCKS)) {
return null;
}
if ($file instanceof \Rector\Core\ValueObject\Application\File && !$this->shouldApply($file)) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
return $node;
@ -138,4 +144,11 @@ CODE_SAMPLE
}
return $this->reflectionProvider->hasFunction(new \PhpParser\Node\Name($name->getLast()), null);
}
private function shouldApply(\Rector\Core\ValueObject\Application\File $file) : bool
{
if (!$this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY)) {
return \true;
}
return $file->hasContentChanged();
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'e22645de4fdc07736bd25896ee98858d3a46bfda';
public const PACKAGE_VERSION = '0315a82d57436e6c7ae4b73766708ad814059b74';
/**
* @var string
*/
public const RELEASE_DATE = '2021-09-17 00:49:58';
public const RELEASE_DATE = '2021-09-17 04:45:44';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210916\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

View File

@ -138,4 +138,8 @@ final class Option
* @var string
*/
public const OUTPUT_FORMAT_SHORT = 'o';
/**
* @var string
*/
public const APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY = 'apply_auto_import_names_on_changed_files_only';
}

View File

@ -75,6 +75,10 @@ final class File
$this->fileContent = $newFileContent;
$this->hasChanged = \true;
}
public function hasContentChanged() : bool
{
return $this->fileContent !== $this->originalFileContent;
}
public function getOriginalFileContent() : string
{
return $this->originalFileContent;

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c::getLoader();
return ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c
class ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit54ab096d7c8e47b134624f3b4065586c::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit54ab096d7c8e47b134624f3b4065586c::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire54ab096d7c8e47b134624f3b4065586c($fileIdentifier, $file);
composerRequire670ccafa4b3c6d48d6b501e5bc540f1c($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire54ab096d7c8e47b134624f3b4065586c($fileIdentifier, $file)
function composerRequire670ccafa4b3c6d48d6b501e5bc540f1c($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit54ab096d7c8e47b134624f3b4065586c
class ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3858,9 +3858,9 @@ class ComposerStaticInit54ab096d7c8e47b134624f3b4065586c
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit54ab096d7c8e47b134624f3b4065586c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit54ab096d7c8e47b134624f3b4065586c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit54ab096d7c8e47b134624f3b4065586c::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit670ccafa4b3c6d48d6b501e5bc540f1c::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20210916\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c', false) && !interface_exists('ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c', false) && !trait_exists('ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c', false)) {
spl_autoload_call('RectorPrefix20210916\ComposerAutoloaderInit54ab096d7c8e47b134624f3b4065586c');
if (!class_exists('ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c', false) && !interface_exists('ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c', false) && !trait_exists('ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c', false)) {
spl_autoload_call('RectorPrefix20210916\ComposerAutoloaderInit670ccafa4b3c6d48d6b501e5bc540f1c');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20210916\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -3311,9 +3311,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210916\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire54ab096d7c8e47b134624f3b4065586c')) {
function composerRequire54ab096d7c8e47b134624f3b4065586c() {
return \RectorPrefix20210916\composerRequire54ab096d7c8e47b134624f3b4065586c(...func_get_args());
if (!function_exists('composerRequire670ccafa4b3c6d48d6b501e5bc540f1c')) {
function composerRequire670ccafa4b3c6d48d6b501e5bc540f1c() {
return \RectorPrefix20210916\composerRequire670ccafa4b3c6d48d6b501e5bc540f1c(...func_get_args());
}
}
if (!function_exists('parseArgs')) {