Merge pull request #3519 from rectorphp/filesytem-name

[Rector] Add UpdateFileNameByClassNameFileSystemRector
This commit is contained in:
kodiakhq[bot] 2020-06-14 22:54:41 +00:00 committed by GitHub
commit 7329e4c25d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 131 additions and 3 deletions

View File

@ -1,4 +1,4 @@
# All 508 Rectors Overview
# All 510 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -8,7 +8,7 @@
- [Architecture](#architecture) (4)
- [Autodiscovery](#autodiscovery) (4)
- [CakePHP](#cakephp) (5)
- [CakePHP](#cakephp) (6)
- [Celebrity](#celebrity) (3)
- [CodeQuality](#codequality) (54)
- [CodingStyle](#codingstyle) (32)
@ -56,7 +56,7 @@
- [Refactoring](#refactoring) (2)
- [RemovingStatic](#removingstatic) (4)
- [Renaming](#renaming) (10)
- [Restoration](#restoration) (6)
- [Restoration](#restoration) (7)
- [SOLID](#solid) (12)
- [Sensio](#sensio) (3)
- [StrictCodeQuality](#strictcodequality) (1)
@ -280,6 +280,31 @@ Change App::uses() to use imports
<br>
### `ArrayToFluentCallRector`
- class: [`Rector\CakePHP\Rector\MethodCall\ArrayToFluentCallRector`](/../master/rules/cakephp/src/Rector/MethodCall/ArrayToFluentCallRector.php)
- [test fixtures](/../master/rules/cakephp/tests/Rector/MethodCall/ArrayToFluentCallRector/Fixture)
Moves array options to fluent setter method calls.
```diff
class ArticlesTable extends \Cake\ORM\Table
{
public function initialize(array $config)
{
- $this->belongsTo('Authors', [
- 'foreignKey' => 'author_id',
- 'propertyName' => 'person'
- ]);
+ $this->belongsTo('Authors')
+ ->setForeignKey('author_id')
+ ->setProperty('person');
}
}
```
<br>
### `ChangeSnakedFixtureNameToCamelRector`
- class: [`Rector\CakePHP\Rector\Name\ChangeSnakedFixtureNameToCamelRector`](/../master/rules/cakephp/src/Rector/Name/ChangeSnakedFixtureNameToCamelRector.php)
@ -9373,6 +9398,15 @@ Remove interface, that are added just for its sake, but nowhere useful
<br>
### `UpdateFileNameByClassNameFileSystemRector`
- class: [`Rector\Restoration\Rector\FileSystem\UpdateFileNameByClassNameFileSystemRector`](/../master/rules/restoration/src/Rector/FileSystem/UpdateFileNameByClassNameFileSystemRector.php)
- [test fixtures](/../master/rules/restoration/tests/Rector/FileSystem/UpdateFileNameByClassNameFileSystemRector/Fixture)
Rename file to respect class name
<br>
## SOLID
### `AddFalseDefaultToBoolPropertyRector`

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Rector\Restoration\Rector\FileSystem;
use PhpParser\Node\Stmt\ClassLike;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @see \Rector\Restoration\Tests\Rector\FileSystem\UpdateFileNameByClassNameFileSystemRector\UpdateFileNameByClassNameFileSystemRectorTest
*/
final class UpdateFileNameByClassNameFileSystemRector extends AbstractFileSystemRector
{
/**
* @var ClassNaming
*/
private $classNaming;
public function __construct(BetterNodeFinder $betterNodeFinder, ClassNaming $classNaming)
{
$this->betterNodeFinder = $betterNodeFinder;
$this->classNaming = $classNaming;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Rename file to respect class name');
}
public function refactor(SmartFileInfo $smartFileInfo): void
{
$nodes = $this->parseFileInfoToNodes($smartFileInfo);
$class = $this->betterNodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
if ($class === null) {
return;
}
$className = $this->getName($class);
if ($className === null) {
return;
}
$classShortName = $this->classNaming->getShortName($className);
if ($classShortName === null) {
return;
}
// matches
if ($classShortName === $smartFileInfo->getBasenameWithoutSuffix()) {
return;
}
// no match → rename file
$newFileLocation = $smartFileInfo->getPath() . DIRECTORY_SEPARATOR . $classShortName . '.php';
$this->moveFile($smartFileInfo, $newFileLocation);
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Restoration\Tests\Rector\FileSystem\UpdateFileNameByClassNameFileSystemRector\Fixture;
final class CorrectClassName
{
}

View File

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Rector\Restoration\Tests\Rector\FileSystem\UpdateFileNameByClassNameFileSystemRector;
use Rector\Core\Testing\PHPUnit\AbstractFileSystemRectorTestCase;
use Rector\Restoration\Rector\FileSystem\UpdateFileNameByClassNameFileSystemRector;
final class UpdateFileNameByClassNameFileSystemRectorTest extends AbstractFileSystemRectorTestCase
{
public function test(): void
{
$this->doTestFile(__DIR__ . '/Fixture/different_class_name.php.inc');
$this->assertFileExists($this->getFixtureTempDirectory() . '/Fixture/CorrectClassName.php');
}
protected function getRectorClass(): string
{
return UpdateFileNameByClassNameFileSystemRector::class;
}
}