-
-
Notifications
You must be signed in to change notification settings - Fork 733
[PSR4] Add UniteFileAndClassNameRector #1170
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
Closed
Closed
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
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,111 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Rector\Psr4; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use Rector\FileSystemRector\Contract\FileSystemRectorInterface; | ||
use Rector\PhpParser\Node\BetterNodeFinder; | ||
use Rector\PhpParser\Node\Resolver\NameResolver; | ||
use Rector\PhpParser\Parser\Parser; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symplify\PackageBuilder\FileSystem\SmartFileInfo; | ||
|
||
final class UniteFileAndClassNameRector implements FileSystemRectorInterface | ||
{ | ||
/** | ||
* @var BetterNodeFinder | ||
*/ | ||
private $betterNodeFinder; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @var Parser | ||
*/ | ||
private $parser; | ||
|
||
/** | ||
* @var NameResolver | ||
*/ | ||
private $nameResolver; | ||
|
||
public function __construct( | ||
BetterNodeFinder $betterNodeFinder, | ||
Filesystem $filesystem, | ||
Parser $parser, | ||
NameResolver $nameResolver | ||
) { | ||
$this->betterNodeFinder = $betterNodeFinder; | ||
$this->filesystem = $filesystem; | ||
$this->parser = $parser; | ||
$this->nameResolver = $nameResolver; | ||
} | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition( | ||
'Corrects file name to be in sync with class name in it and autoloadable by PSR-4 classes.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
// file: SomeClass.php | ||
final class SomeCllass | ||
{ | ||
|
||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
// file: SomeClass.php | ||
final class SomeClass | ||
{ | ||
|
||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function refactor(SmartFileInfo $smartFileInfo): void | ||
{ | ||
$nodes = $this->parser->parseFile($smartFileInfo->getRealPath()); | ||
|
||
// collect named class nodes | ||
/** @var ClassLike[] $classLikes */ | ||
$classLikes = $this->betterNodeFinder->find($nodes, function (Node $node) { | ||
if (! $node instanceof ClassLike) { | ||
return false; | ||
} | ||
|
||
return $node->name !== null; | ||
}); | ||
|
||
// process files with exactly 1 non-anonymous class like nodes | ||
if (count($classLikes) !== 1) { | ||
return; | ||
} | ||
|
||
$classLikeShortName = $this->nameResolver->resolve($classLikes[0]); | ||
if ($classLikeShortName === null) { | ||
return; | ||
} | ||
|
||
// is in sync with file? skip | ||
if ($classLikeShortName === $smartFileInfo->getBasenameWithoutSuffix()) { | ||
return; | ||
} | ||
|
||
// rename! | ||
$oldPath = $smartFileInfo->getRealPath(); | ||
$newPath = dirname($smartFileInfo->getRealPath()) . DIRECTORY_SEPARATOR . $classLikeShortName . '.php'; | ||
|
||
$this->filesystem->rename($oldPath, $newPath); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Rector/Psr4/UniteFileAndClassNameRector/Source/IncorrectClassNamee.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,8 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Rector\Psr4\UniteFileAndClassNameRector\Fxitures; | ||
|
||
final class IncorrectClassName | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
tests/Rector/Psr4/UniteFileAndClassNameRector/Source/IncorrectInterfaceNamee.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,8 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Rector\Psr4\UniteFileAndClassNameRector\Fxitures; | ||
|
||
interface IncorrectInterfaceName | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
tests/Rector/Psr4/UniteFileAndClassNameRector/Source/IncorrectTraitNamee.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,8 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Rector\Psr4\UniteFileAndClassNameRector\Fxitures; | ||
|
||
trait IncorrectTraitName | ||
{ | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
tests/Rector/Psr4/UniteFileAndClassNameRector/UniteFileAndClassNameRectorTest.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,58 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Rector\Psr4\UniteFileAndClassNameRector; | ||
|
||
use Rector\FileSystemRector\FileSystemFileProcessor; | ||
use Rector\HttpKernel\RectorKernel; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symplify\PackageBuilder\FileSystem\SmartFileInfo; | ||
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase; | ||
|
||
/** | ||
* @covers \Rector\Rector\Psr4\UniteFileAndClassNameRector | ||
*/ | ||
final class UniteFileAndClassNameRectorTest extends AbstractKernelTestCase | ||
{ | ||
/** | ||
* @var FileSystemFileProcessor | ||
*/ | ||
private $fileSystemFileProcessor; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->bootKernelWithConfigs(RectorKernel::class, [__DIR__ . '/config.yaml']); | ||
$this->fileSystemFileProcessor = self::$container->get(FileSystemFileProcessor::class); | ||
|
||
// prepare fixture file | ||
$this->filesystem = self::$container->get(Filesystem::class); | ||
$this->filesystem->mirror(__DIR__ . '/Source/', __DIR__ . '/Fixtures/'); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
// cleanup renamed file | ||
$this->filesystem->remove(__DIR__ . '/Fixtures'); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$this->assertFileNotExists(__DIR__ . '/Fixtures/IncorrectClassName.php'); | ||
|
||
$file = __DIR__ . '/Fixtures/IncorrectClassNamee.php'; | ||
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($file)); | ||
$this->assertFileExists(__DIR__ . '/Fixtures/IncorrectClassName.php'); | ||
|
||
$file = __DIR__ . '/Fixtures/IncorrectInterfaceNamee.php'; | ||
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($file)); | ||
$this->assertFileExists(__DIR__ . '/Fixtures/IncorrectInterfaceName.php'); | ||
|
||
$file = __DIR__ . '/Fixtures/IncorrectTraitNamee.php'; | ||
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($file)); | ||
$this->assertFileExists(__DIR__ . '/Fixtures/IncorrectTraitName.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,2 @@ | ||
services: | ||
Rector\Rector\Psr4\UniteFileAndClassNameRector: ~ |
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.
According to the code and test case, it renames the file not the class. This example shows that the file name stays the same but the class inside gets renamed.
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.
Maybe add an option to let the user decide in which direction they want to sync the name? From file to class or from class to file? I could imagine that this is a very hard decision though.
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 idea is to automate this for user. Any case for "asking" should be converted to algorithm or not handled by Rector at all but by a sniff.
I thought about this and there should be some function that detects which of file/class is typoed. Any idea for typo-detecting PHP package?
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.
Even if we'd add a spell checker, the user would still have to configure which language he used for his project, which would then again be a case of "asking" I think.
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.
Better done than perfect. Got any idea about the package?
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.
If you think that's the best option, got 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.
Hi guys! I agree that it would be awesome to let user choose, what way he wants to use it.
When i say let user choose there might be option/parameter in rector's config, maybe with default value? No prompts or ask dialogs :-).
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.
Rector's idea is not to bother user at all. Anything requiring user's attention is better done in PHPStorm, where you prefer control with slow work.
Rector should handle typos for users, like
SomeeeClass
inSomeClass.php
. Any user-opinionated moving is not Rector's job.It would be better to add more real-life tests, so I know what you need this for :)
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.
Real life example:
SomeClassPresenter
+SomeClass.php
- discussion with team, agreed to renameSomeClass.php
->SomeClassPresenter.php
this will be most common use case for us now, but i can imagine in some cases we want to rename class instead of file. Maybe create 2 rectors? One for renaming files, different for renaming classes? 😄Uh oh!
There was an error while loading. Please reload this page.
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'd do these manually, since it needs team energy anyway.
Rector would only add work here instead. Not a way to go :)