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
91 changes: 88 additions & 3 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 485 Rectors Overview
# All 487 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand All @@ -25,7 +25,7 @@
- [MockistaToMockery](#mockistatomockery) (2)
- [MysqlToMysqli](#mysqltomysqli) (4)
- [Naming](#naming) (1)
- [Nette](#nette) (11)
- [Nette](#nette) (12)
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
- [NetteToSymfony](#nettetosymfony) (9)
- [Order](#order) (3)
Expand Down Expand Up @@ -55,7 +55,7 @@
- [RemovingStatic](#removingstatic) (4)
- [Renaming](#renaming) (10)
- [Restoration](#restoration) (3)
- [SOLID](#solid) (11)
- [SOLID](#solid) (12)
- [Sensio](#sensio) (1)
- [StrictCodeQuality](#strictcodequality) (1)
- [Symfony](#symfony) (29)
Expand Down Expand Up @@ -4312,6 +4312,41 @@ Nextras/Form upgrade of addDatePicker method call to DateControl assign

<br>

### `ContextGetByTypeToConstructorInjectionRector`

- class: [`Rector\Nette\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector`](/../master/rules/nette/src/Rector/MethodCall/ContextGetByTypeToConstructorInjectionRector.php)
- [test fixtures](/../master/rules/nette/tests/Rector/MethodCall/ContextGetByTypeToConstructorInjectionRector/Fixture)

Move dependency passed to all children to parent as @inject/@required dependency

```diff
class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;

+ /**
+ * @var SomeTypeToInject
+ */
+ private $someTypeToInject;
+
+ public function __construct(SomeTypeToInject $someTypeToInject)
+ {
+ $this->someTypeToInject = $someTypeToInject;
+ }
+
public function run()
{
- $someTypeToInject = $this->context->getByType(SomeTypeToInject::class);
+ $someTypeToInject = $this->someTypeToInject;
}
}
```

<br>

### `EndsWithFunctionToNetteUtilsStringsRector`

- class: [`Rector\Nette\Rector\Identical\EndsWithFunctionToNetteUtilsStringsRector`](/../master/rules/nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php)
Expand Down Expand Up @@ -9127,6 +9162,56 @@ Classes that have no children nor are used, should have abstract

<br>

### `MultiParentingToAbstractDependencyRector`

- class: [`Rector\SOLID\Rector\Class_\MultiParentingToAbstractDependencyRector`](/../master/rules/solid/src/Rector/Class_/MultiParentingToAbstractDependencyRector.php)
- [test fixtures](/../master/rules/solid/tests/Rector/Class_/MultiParentingToAbstractDependencyRector/Fixture)

Move dependency passed to all children to parent as @inject/@required dependency

```yaml
services:
Rector\SOLID\Rector\Class_\MultiParentingToAbstractDependencyRector:
$framework: nette
```


```diff
abstract class AbstractParentClass
{
- private $someDependency;
-
- public function __construct(SomeDependency $someDependency)
- {
- $this->someDependency = $someDependency;
- }
+ /**
+ * @inject
+ * @var SomeDependency
+ */
+ public $someDependency;
}

class FirstChild extends AbstractParentClass
{
- public function __construct(SomeDependency $someDependency)
- {
- parent::__construct($someDependency);
- }
}

class SecondChild extends AbstractParentClass
{
- public function __construct(SomeDependency $someDependency)
- {
- parent::__construct($someDependency);
- }
}
```

<br>

### `RemoveAlwaysElseRector`

- class: [`Rector\SOLID\Rector\If_\RemoveAlwaysElseRector`](/../master/rules/solid/src/Rector/If_/RemoveAlwaysElseRector.php)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ private function getConstantsDefinedInClass(string $className): array
{
$reflectionClass = new ReflectionClass($className);

$currentClassConstants = $reflectionClass->getConstants();
$currentClassConstants = array_keys($reflectionClass->getConstants());
$parentClassReflection = $reflectionClass->getParentClass();

if (! $parentClassReflection) {
return array_keys($currentClassConstants);
return $currentClassConstants;
}

$currentClassConstants = array_diff($currentClassConstants, $parentClassReflection->getConstants());
$parentClassConstants = array_keys($parentClassReflection->getConstants());

return array_keys($currentClassConstants);
return array_diff($currentClassConstants, $parentClassConstants);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Rector\Nette\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Symfony\Rector\FrameworkBundle\AbstractToConstructorInjectionRector;

/**
* @see \Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\ContextGetByTypeToConstructorInjectionRectorTest
*/
final class ContextGetByTypeToConstructorInjectionRector extends AbstractToConstructorInjectionRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Move dependency passed to all children to parent as @inject/@required dependency',
[
new CodeSample(
<<<'PHP'
class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;

public function run()
{
$someTypeToInject = $this->context->getByType(SomeTypeToInject::class);
}
}
PHP
,
<<<'PHP'
class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;

/**
* @var SomeTypeToInject
*/
private $someTypeToInject;

public function __construct(SomeTypeToInject $someTypeToInject)
{
$this->someTypeToInject = $someTypeToInject;
}

public function run()
{
$someTypeToInject = $this->someTypeToInject;
}
}
PHP

),
]
);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof PropertyFetch) {
return null;
}

if (! $this->isObjectType($node->var, 'Nette\DI\Container')) {
return null;
}

if (! $this->isName($node->name, 'getByType')) {
return null;
}

return $this->processMethodCallNode($node);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Nette\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector;

final class ContextGetByTypeToConstructorInjectionRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return ContextGetByTypeToConstructorInjectionRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source;

final class SomeTypeToInject
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\SomeTypeToInject;

class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;

public function run()
{
$someTypeToInject = $this->context->getByType(SomeTypeToInject::class);
}
}

?>
-----
<?php

namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\SomeTypeToInject;

class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;
/**
* @var \Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\SomeTypeToInject
*/
private $someTypeToInject;
public function __construct(\Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\SomeTypeToInject $someTypeToInject)
{
$this->someTypeToInject = $someTypeToInject;
}

public function run()
{
$someTypeToInject = $this->someTypeToInject;
}
}

?>