Skip to content

Commit

Permalink
Remove auto import on changed files only to avoid too detailed setup …
Browse files Browse the repository at this point in the history
…and making ambiguous code (#3132)
  • Loading branch information
TomasVotruba committed Nov 30, 2022
1 parent 7331d04 commit 7a65218
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 131 deletions.
11 changes: 0 additions & 11 deletions build/target-repository/docs/auto_import_names.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ Do you want to keep those?
$rectorConfig->importShortClasses(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](https://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:
Expand Down
13 changes: 0 additions & 13 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ public function enterNode(Node $node): ?Node
return null;
}

if (! $this->shouldApply($file)) {
return null;
}

if ($node instanceof Name) {
return $this->processNodeName($node, $file);
}
Expand Down Expand Up @@ -228,13 +224,4 @@ private function shouldImportName(Name $name, array $currentUses): bool

return $this->reflectionProvider->hasFunction(new Name($name->getLast()), null);
}

private function shouldApply(File $file): bool
{
if (! $this->parameterProvider->provideBoolParameter(Option::APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY)) {
return true;
}

return $file->hasContentChanged();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ final class Option

/**
* @var string
* @deprecated
* @api
*/
public const APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY = 'apply_auto_import_names_on_changed_files_only';

Expand Down

0 comments on commit 7a65218

Please sign in to comment.