Skip to content
Merged
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ parameters:
- '*/packages/ContributorTools/templates/*'
# part of composer
- 'tests/Composer/AutoloadWrongCasesEventSubscriber.php'
- '*/tests/Rector/Psr4/MultipleClassFileToPsr4ClassesRector/Expected/Just*ExceptionWithoutNamespace.php'

ignoreErrors:
# false positive
Expand Down
2 changes: 1 addition & 1 deletion rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ parameters:
php_version_features: '7.1'

services:
Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
# Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
105 changes: 71 additions & 34 deletions src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -66,44 +67,43 @@ public function refactor(SmartFileInfo $smartFileInfo): void
{
$nodes = $this->parseFileInfoToNodes($smartFileInfo);

/** @var Namespace_[] $namespaceNodes */
$namespaceNodes = $this->betterNodeFinder->findInstanceOf($nodes, Namespace_::class);

if ($this->shouldSkip($smartFileInfo, $nodes, $namespaceNodes)) {
if ($this->shouldSkip($smartFileInfo, $nodes)) {
return;
}

$shouldDelete = true;

foreach ($namespaceNodes as $namespaceNode) {
$newStmtsSet = $this->removeAllOtherNamespaces($nodes, $namespaceNode);

foreach ($newStmtsSet as $newStmt) {
if (! $newStmt instanceof Namespace_) {
continue;
}

/** @var ClassLike[] $namespacedClassLikeNodes */
$namespacedClassLikeNodes = $this->betterNodeFinder->findInstanceOf($newStmt->stmts, ClassLike::class);
/** @var Namespace_[] $namespaceNodes */
$namespaceNodes = $this->betterNodeFinder->findInstanceOf($nodes, Namespace_::class);

foreach ($namespacedClassLikeNodes as $classLikeNode) {
if ($classLikeNode instanceof Class_ && $classLikeNode->isAnonymous()) {
continue;
}
if (count($namespaceNodes)) {
$shouldDelete = $this->processNamespaceNodes($smartFileInfo, $namespaceNodes, $nodes);
} else {
$declareNode = null;

$this->removeAllClassLikesFromNamespaceNode($newStmt);
$newStmt->stmts[] = $classLikeNode;
foreach ($nodes as $node) {
if ($node instanceof Declare_) {
$declareNode = $node;
}

$fileDestination = $this->createClassLikeFileDestination($classLikeNode, $smartFileInfo);
if (! $node instanceof Class_ || $node->isAnonymous()) {
continue;
}

if ($smartFileInfo->getRealPath() === $fileDestination) {
$shouldDelete = false;
}
$fileDestination = $this->createClassLikeFileDestination($node, $smartFileInfo);
if ($smartFileInfo->getRealPath() === $fileDestination) {
$shouldDelete = false;
}

// has file changed?
// has file changed?

$this->printNodesToFilePath($newStmtsSet, $fileDestination);
if ($declareNode) {
$nodes = array_merge([$declareNode], [$node]);
} else {
$nodes = [$node];
}

$this->printNodesToFilePath($nodes, $fileDestination);
}
}

Expand All @@ -114,15 +114,9 @@ public function refactor(SmartFileInfo $smartFileInfo): void

/**
* @param Node[] $nodes
* @param Namespace_[] $namespaceNodes
*/
private function shouldSkip(SmartFileInfo $smartFileInfo, array $nodes, array $namespaceNodes): bool
private function shouldSkip(SmartFileInfo $smartFileInfo, array $nodes): bool
{
// process only namespaced file
if ($namespaceNodes === []) {
return true;
}

/** @var ClassLike[] $classLikeNodes */
$classLikeNodes = $this->betterNodeFinder->findInstanceOf($nodes, ClassLike::class);

Expand Down Expand Up @@ -173,6 +167,49 @@ private function createClassLikeFileDestination(ClassLike $classLike, SmartFileI
{
$currentDirectory = dirname($smartFileInfo->getRealPath());

return $currentDirectory . DIRECTORY_SEPARATOR . (string) $classLike->name . '.php';
return $currentDirectory . DIRECTORY_SEPARATOR . $classLike->name . '.php';
}

/**
* @param Namespace_[] $namespaceNodes
* @param Node\Stmt[] $nodes
*/
private function processNamespaceNodes(SmartFileInfo $smartFileInfo, array $namespaceNodes, array $nodes): bool
{
$shouldDelete = true;

foreach ($namespaceNodes as $namespaceNode) {
$newStmtsSet = $this->removeAllOtherNamespaces($nodes, $namespaceNode);

foreach ($newStmtsSet as $newStmt) {
if (! $newStmt instanceof Namespace_) {
continue;
}

/** @var ClassLike[] $namespacedClassLikeNodes */
$namespacedClassLikeNodes = $this->betterNodeFinder->findInstanceOf($newStmt->stmts, ClassLike::class);

foreach ($namespacedClassLikeNodes as $classLikeNode) {
if ($classLikeNode instanceof Class_ && $classLikeNode->isAnonymous()) {
continue;
}

$this->removeAllClassLikesFromNamespaceNode($newStmt);
$newStmt->stmts[] = $classLikeNode;

$fileDestination = $this->createClassLikeFileDestination($classLikeNode, $smartFileInfo);

if ($smartFileInfo->getRealPath() === $fileDestination) {
$shouldDelete = false;
}

// has file changed?

$this->printNodesToFilePath($newStmtsSet, $fileDestination);
}
}
}

return $shouldDelete;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types=1);

final class JustOneExceptionWithoutNamespace
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types=1);

final class JustTwoExceptionWithoutNamespace
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ protected function tearDown(): void

/**
* @param string[] $expectedExceptions
* @dataProvider provideWithoutNamespace
* @dataProvider provideExceptionsData
* @dataProvider provideMissNamed
* @dataProvider provideClassLike
* @dataProvider provideExceptionsData
*/
public function test(string $file, array $expectedExceptions): void
{
Expand Down Expand Up @@ -97,6 +98,18 @@ public function provideExceptionsData(): Iterator
];
}

public function provideWithoutNamespace(): Iterator
{
// non namespaced PSR-4 file with one class
yield [
__DIR__ . '/Source/exceptions-without-namespace.php',
[
__DIR__ . '/Fixture/JustOneExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustOneExceptionWithoutNamespace.php',
__DIR__ . '/Fixture/JustTwoExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustTwoExceptionWithoutNamespace.php',
],
];
}

public function provideMissNamed(): Iterator
{
yield [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

final class JustOneExceptionWithoutNamespace
{
}

final class JustTwoExceptionWithoutNamespace
{
}