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
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"Rector\\Shopware\\": "packages/Shopware/src",
"Rector\\NetteTesterToPHPUnit\\": "packages/NetteTesterToPHPUnit/src",
"Rector\\Nette\\": "packages/Nette/src",
"Rector\\SOLID\\": "packages/SOLID/src"
"Rector\\SOLID\\": "packages/SOLID/src",
"Rector\\Legacy\\": "packages/Legacy/src"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -107,7 +108,8 @@
"Rector\\Shopware\\Tests\\": "packages/Shopware/tests",
"Rector\\NetteTesterToPHPUnit\\Tests\\": "packages/NetteTesterToPHPUnit/tests",
"Rector\\Nette\\Tests\\": "packages/Nette/tests",
"Rector\\SOLID\\Tests\\": "packages/SOLID/tests"
"Rector\\SOLID\\Tests\\": "packages/SOLID/tests",
"Rector\\Legacy\\Tests\\": "packages/Legacy/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",
Expand Down Expand Up @@ -159,4 +161,4 @@
"dev-master": "0.5-dev"
}
}
}
}
43 changes: 35 additions & 8 deletions docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 298 Rectors Overview
# All 299 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand All @@ -14,6 +14,7 @@
- [DomainDrivenDesign](#domaindrivendesign)
- [Guzzle](#guzzle)
- [Laravel](#laravel)
- [Legacy](#legacy)
- [MysqlToMysqli](#mysqltomysqli)
- [Nette](#nette)
- [NetteTesterToPHPUnit](#nettetestertophpunit)
Expand Down Expand Up @@ -1730,6 +1731,37 @@ Move help facade-like function calls to constructor injection

<br>

## Legacy

### `ChangeSingletonToServiceRector`

- class: `Rector\Legacy\Rector\ClassMethod\ChangeSingletonToServiceRector`

Change singleton class to normal class that can be registered as a service

```diff
class SomeClass
{
- private static $instance;
-
- private function __construct()
+ public function __construct()
{
- }
-
- public static function getInstance()
- {
- if (null === static::$instance) {
- static::$instance = new static();
- }
-
- return static::$instance;
}
}
```

<br>

## MysqlToMysqli

### `MysqlAssignToMysqliRector`
Expand Down Expand Up @@ -4376,9 +4408,9 @@ Finalize every class constant that is used only locally

<br>

### `AbstractChildlessUnusedClassesRector`
### `MakeUnusedClassesWithChildrenAbstractRector`

- class: `Rector\SOLID\Rector\Class_\AbstractChildlessUnusedClassesRector`
- class: `Rector\SOLID\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector`

Classes that have no children nor are used, should have abstract

Expand All @@ -4391,11 +4423,6 @@ Classes that have no children nor are used, should have abstract
+abstract class PossibleAbstractClass
{
}

function run()
{
return new SomeClass();
}
```

<br>
Expand Down
2 changes: 2 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ parameters:

Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
# tough logic
- 'packages/Legacy/src/Rector/ClassMethod/ChangeSingletonToServiceRector.php'
- 'src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php'
- 'src/PhpParser/Node/Resolver/NameResolver.php'
- 'src/Rector/MethodBody/NormalToFluentRector.php'
Expand All @@ -111,6 +112,7 @@ parameters:
- 'packages/Laravel/src/Rector/FuncCall/HelperFunctionToConstructorInjectionRector.php'
- 'packages/PhpSpecToPHPUnit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php'
- 'packages/NetteTesterToPHPUnit/src/AssertManipulator.php'
- 'packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php'
# aliases
- 'packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php'

Expand Down
15 changes: 15 additions & 0 deletions packages/ContributorTools/src/Command/CreateRectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@ private function saveJsonToFile(string $filePath, array $json): void
{
$content = Json::encode($json, Json::PRETTY);
$content = $this->inlineSections($content, ['keywords', 'bin']);
$content = $this->inlineAuthors($content);
FileSystem::write($filePath, $content);
}

private function inlineAuthors(string $jsonContent): string
{
$pattern = '#(?<start>"authors": \[\s+)(?<content>.*?)(?<end>\s+\](,))#ms';
$jsonContent = Strings::replace($jsonContent, $pattern, function (array $match): string {
$inlined = Strings::replace($match['content'], '#\s+#', ' ');
$inlined = trim($inlined);
$inlined = Strings::replace($inlined, '#},#', "},\n ");

return $match['start'] . $inlined . $match['end'];
});

return $jsonContent;
}
}
8 changes: 8 additions & 0 deletions packages/Legacy/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
public: true
autowire: true

Rector\Legacy\:
resource: '../src'
exclude: '../src/{Rector/**/*Rector.php}'
125 changes: 125 additions & 0 deletions packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php declare(strict_types=1);

namespace Rector\Legacy\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\Manipulator\ConstFetchManipulator;
use Rector\PhpParser\Printer\BetterStandardPrinter;

final class SingletonClassMethodAnalyzer
{
/**
* @var ConstFetchManipulator
*/
private $constFetchManipulator;

/**
* @var BetterStandardPrinter
*/
private $betterStandardPrinter;

/**
* @var NodeTypeResolver
*/
private $nodeTypeResolver;

public function __construct(
ConstFetchManipulator $constFetchManipulator,
BetterStandardPrinter $betterStandardPrinter,
NodeTypeResolver $nodeTypeResolver
) {
$this->constFetchManipulator = $constFetchManipulator;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->nodeTypeResolver = $nodeTypeResolver;
}

/**
* Match this code:
* if (null === static::$instance) {
* static::$instance = new static();
* }
* return static::$instance;
*
* Matches "static::$instance" on success
*/
public function matchStaticPropertyFetch(ClassMethod $classMethod): ?StaticPropertyFetch
{
if (count((array) $classMethod->stmts) !== 2) {
return null;
}

if (! $classMethod->stmts[0] instanceof If_) {
return null;
}

/** @var If_ $if */
$if = $classMethod->stmts[0];
$staticPropertyFetch = $this->matchStaticPropertyFetchInIfCond($if->cond);

if (count($if->stmts) !== 1) {
return null;
}

if (! $if->stmts[0] instanceof Expression) {
return null;
}

$stmt = $if->stmts[0]->expr;

// create self and assign to static property
if (! $stmt instanceof Assign) {
return null;
}

if (! $this->betterStandardPrinter->areNodesEqual($staticPropertyFetch, $stmt->var)) {
return null;
}

if (! $stmt->expr instanceof New_) {
return null;
}

$class = $classMethod->getAttribute(AttributeKey::CLASS_NAME);

// the "self" class is created
if ($this->nodeTypeResolver->getTypes($stmt->expr->class) !== [$class]) {
return null;
}

/** @var StaticPropertyFetch $staticPropertyFetch */
return $staticPropertyFetch;
}

private function matchStaticPropertyFetchInIfCond(Expr $expr): ?StaticPropertyFetch
{
// matching: "self::$static === null"
if ($expr instanceof Identical) {
if ($this->constFetchManipulator->isNull($expr->left) && $expr->right instanceof StaticPropertyFetch) {
return $expr->right;
}

if ($this->constFetchManipulator->isNull($expr->right) && $expr->left instanceof StaticPropertyFetch) {
return $expr->left;
}
}

// matching: "! self::$static"
if ($expr instanceof BooleanNot) {
if ($expr->expr instanceof StaticPropertyFetch) {
return $expr->expr;
}
}

return null;
}
}
Loading