+
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/Rector/Psr4/UniteFileAndClassNameRector.php
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
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Member Author

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 👍

Copy link
Contributor

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.

  1. Fix filename
  2. Fix classname and all usages in app

When i say let user choose there might be option/parameter in rector's config, maybe with default value? No prompts or ask dialogs :-).

Copy link
Member Author

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 in SomeClass.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 :)

Copy link
Contributor

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 rename SomeClass.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? 😄

Copy link
Member Author

@TomasVotruba TomasVotruba Mar 11, 2019

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 :)

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);
}
}
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
{

}
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
{

}
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
{

}
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');
}
}
2 changes: 2 additions & 0 deletions tests/Rector/Psr4/UniteFileAndClassNameRector/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
Rector\Rector\Psr4\UniteFileAndClassNameRector: ~
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载