-
-
Notifications
You must be signed in to change notification settings - Fork 441
[skip] mark class/path skip used only when rule would change the file #8076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ng/Rector/Name/RenameClassRector/FixtureSkipUsedTracking/skip_unused_no_old_class.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureSkipUsedTracking; | ||
|
|
||
| class SkipUnusedNoOldClass | ||
| { | ||
| } |
7 changes: 7 additions & 0 deletions
7
...Rector/Name/RenameClassRector/FixtureSkipUsedTracking/skip_used_renames_old_class.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureSkipUsedTracking; | ||
|
|
||
| class SkipUsedRenamesOldClass extends \Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClass | ||
| { | ||
| } |
46 changes: 46 additions & 0 deletions
46
rules-tests/Renaming/Rector/Name/RenameClassRector/SkipUsedTrackingTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector; | ||
|
|
||
| use Nette\Utils\FileSystem; | ||
| use Rector\Renaming\Rector\Name\RenameClassRector; | ||
| use Rector\Skipper\Skipper\UsedSkipCollector; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| /** | ||
| * A class/path skip is only "used" when the rule would actually have changed the skipped file. | ||
| * | ||
| * @see RenameClassRector | ||
| */ | ||
| final class SkipUsedTrackingTest extends AbstractRectorTestCase | ||
| { | ||
| public function testMarksSkipUsedOnlyWhenRuleWouldChangeFile(): void | ||
| { | ||
| // file uses OldClass → rule would rename it → its skip is what prevents the change | ||
| $this->doTestFile(__DIR__ . '/FixtureSkipUsedTracking/skip_used_renames_old_class.php.inc'); | ||
|
|
||
| // doTestFile() only cleans up the last processed temp file, so remove this one explicitly | ||
| FileSystem::delete(__DIR__ . '/FixtureSkipUsedTracking/skip_used_renames_old_class.php'); | ||
|
|
||
| // file does not use OldClass → rule would not change it → its skip is unnecessary | ||
| $this->doTestFile(__DIR__ . '/FixtureSkipUsedTracking/skip_unused_no_old_class.php.inc'); | ||
|
|
||
| $usedSkipCollector = $this->make(UsedSkipCollector::class); | ||
| $usedSkips = $usedSkipCollector->provide(); | ||
|
|
||
| $usedPaths = $usedSkips[RenameClassRector::class] ?? []; | ||
|
|
||
| // the skip that actually prevented a rename is marked used | ||
| $this->assertContains('*skip_used_renames_old_class*', $usedPaths); | ||
|
|
||
| // the skip on a file the rule would not have touched is never marked used | ||
| $this->assertNotContains('*skip_unused_no_old_class*', $usedPaths); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/skip_used_tracking_configured_rule.php'; | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...ests/Renaming/Rector/Name/RenameClassRector/config/skip_used_tracking_configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Renaming\Rector\Name\RenameClassRector; | ||
| use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClass; | ||
| use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClass; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [ | ||
| OldClass::class => NewClass::class, | ||
| ]); | ||
|
|
||
| // both paths are skipped, but only the first file actually uses OldClass, so only its skip | ||
| // prevents a real change - the second one would not have changed anyway | ||
| $rectorConfig->skip([ | ||
| RenameClassRector::class => ['*skip_used_renames_old_class*', '*skip_unused_no_old_class*'], | ||
| ]); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Skipper\ValueObject; | ||
|
|
||
| /** | ||
| * Result of a matched class/path skip: the skipped class and the concrete path pattern | ||
| * that matched, if the skip is scoped to specific paths. | ||
| */ | ||
| final readonly class SkipMatch | ||
| { | ||
| public function __construct( | ||
| private string $skippedClass, | ||
| private ?string $matchedPath | ||
| ) { | ||
| } | ||
|
|
||
| public function getSkippedClass(): string | ||
| { | ||
| return $this->skippedClass; | ||
| } | ||
|
|
||
| public function getMatchedPath(): ?string | ||
| { | ||
| return $this->matchedPath; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
refactor()now called twice, I think this can be improved, I will look into it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created PR
for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is
return null;inif ($skipMatch instanceof SkipMatch) {, so its called only once.