Skip to content

Commit 3e49d06

Browse files
author
Tomáš Votruba
authored
Merge 50ac029 into 09762bd
2 parents 09762bd + 50ac029 commit 3e49d06

File tree

9 files changed

+73
-24
lines changed

9 files changed

+73
-24
lines changed

src/NodeAnalyzer/Contrib/SymfonyContainerCallsAnalyzer.php renamed to src/NodeAnalyzer/Contrib/Symfony/ContainerCallAnalyzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php declare(strict_types=1);
22

3-
namespace Rector\NodeAnalyzer\Contrib;
3+
namespace Rector\NodeAnalyzer\Contrib\Symfony;
44

55
use PhpParser\Node\Expr\MethodCall;
66
use PhpParser\Node\Expr\Variable;
77
use PhpParser\Node\Identifier;
88
use PhpParser\Node\Scalar\String_;
99

10-
final class SymfonyContainerCallsAnalyzer
10+
final class ContainerCallAnalyzer
1111
{
1212
/**
1313
* Finds $this->get(...);

src/NodeAnalyzer/Contrib/ControllerMethodAnalyzer.php renamed to src/NodeAnalyzer/Contrib/Symfony/ControllerMethodAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace Rector\NodeAnalyzer\Contrib;
3+
namespace Rector\NodeAnalyzer\Contrib\Symfony;
44

55
use Nette\Utils\Strings;
66
use PhpParser\Node;

src/Rector/Contrib/Symfony/Console/CommandToConstructorInjectionRector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Rector\Naming\PropertyNaming;
1212
use Rector\Node\Attribute;
1313
use Rector\Node\NodeFactory;
14-
use Rector\NodeAnalyzer\Contrib\SymfonyContainerCallsAnalyzer;
14+
use Rector\NodeAnalyzer\Contrib\Symfony\ContainerCallAnalyzer;
1515
use Rector\Rector\AbstractRector;
1616

1717
/**
@@ -57,9 +57,9 @@ final class CommandToConstructorInjectionRector extends AbstractRector
5757
private $nodeFactory;
5858

5959
/**
60-
* @var SymfonyContainerCallsAnalyzer
60+
* @var ContainerCallAnalyzer
6161
*/
62-
private $symfonyContainerCallsAnalyzer;
62+
private $containerCallAnalyzer;
6363

6464
/**
6565
* @var ServiceTypeForNameProviderInterface
@@ -70,13 +70,13 @@ public function __construct(
7070
ClassPropertyCollector $classPropertyCollector,
7171
PropertyNaming $propertyNaming,
7272
NodeFactory $nodeFactory,
73-
SymfonyContainerCallsAnalyzer $symfonyContainerCallsAnalyzer,
73+
ContainerCallAnalyzer $containerCallAnalyzer,
7474
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider
7575
) {
7676
$this->classPropertyCollector = $classPropertyCollector;
7777
$this->propertyNaming = $propertyNaming;
7878
$this->nodeFactory = $nodeFactory;
79-
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
79+
$this->containerCallAnalyzer = $containerCallAnalyzer;
8080
$this->serviceTypeForNameProvider = $serviceTypeForNameProvider;
8181
}
8282

@@ -92,7 +92,7 @@ public function isCandidate(Node $node): bool
9292
return false;
9393
}
9494

95-
return $this->symfonyContainerCallsAnalyzer->isGetContainerCall($node);
95+
return $this->containerCallAnalyzer->isGetContainerCall($node);
9696
}
9797

9898
/**

src/Rector/Contrib/Symfony/Form/FormTypeGetParentRector.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@
1111
/**
1212
* Converts all:
1313
* - getParent() {
14-
* return 'some_string';
14+
* return 'collection';
15+
* }
16+
* - getExtendedType() {
17+
* return 'collection';
1518
* }
1619
*
1720
* into:
1821
* - getParent() {
1922
* return CollectionType::class;
2023
* }
24+
* - getExtendedType() {
25+
* return CollectionType::class;
26+
* }
2127
*/
2228
final class FormTypeGetParentRector extends AbstractRector
2329
{
2430
/**
2531
* @var string[]
2632
*/
2733
private $nameToClassMap = [
34+
'form' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
2835
'birthday' => 'Symfony\Component\Form\Extension\Core\Type\BirthdayType',
2936
'checkbox' => 'Symfony\Component\Form\Extension\Core\Type\CheckboxType',
3037
'collection' => 'Symfony\Component\Form\Extension\Core\Type\CollectionType',
@@ -54,6 +61,7 @@ final class FormTypeGetParentRector extends AbstractRector
5461
'button' => 'Symfony\Component\Form\Extension\Core\Type\ButtonType',
5562
'submit' => 'Symfony\Component\Form\Extension\Core\Type\SubmitType',
5663
'reset' => 'Symfony\Component\Form\Extension\Core\Type\ResetType',
64+
'entity' => 'Symfony\Bridge\Doctrine\Form\Type\EntityType',
5765
];
5866

5967
/**
@@ -68,21 +76,23 @@ public function __construct(NodeFactory $nodeFactory)
6876

6977
public function isCandidate(Node $node): bool
7078
{
71-
if (! $node instanceof String_ || ! isset($this->nameToClassMap[$node->value])) {
79+
if (! $node instanceof String_) {
7280
return false;
7381
}
7482

75-
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
76-
if ($parentClassName !== 'Symfony\Component\Form\AbstractType') {
83+
if (! isset($this->nameToClassMap[$node->value])) {
7784
return false;
7885
}
7986

80-
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
81-
if ($methodName !== 'getParent') {
82-
return false;
87+
if ($this->isParentTypeAndMethod($node, 'Symfony\Component\Form\AbstractType', 'getParent')) {
88+
return true;
89+
}
90+
91+
if ($this->isParentTypeAndMethod($node, 'Symfony\Component\Form\AbstractTypeExtension', 'getExtendedType')) {
92+
return true;
8393
}
8494

85-
return true;
95+
return false;
8696
}
8797

8898
/**
@@ -94,4 +104,16 @@ public function refactor(Node $stringNode): ?Node
94104

95105
return $this->nodeFactory->createClassConstantReference($class);
96106
}
107+
108+
private function isParentTypeAndMethod(Node $node, string $type, string $method): bool
109+
{
110+
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
111+
if ($parentClassName !== $type) {
112+
return false;
113+
}
114+
115+
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
116+
117+
return $methodName === $method;
118+
}
97119
}

src/Rector/Contrib/Symfony/HttpKernel/GetRequestRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpParser\Node\Stmt\ClassMethod;
88
use Rector\Node\Attribute;
99
use Rector\Node\NodeFactory;
10-
use Rector\NodeAnalyzer\Contrib\ControllerMethodAnalyzer;
10+
use Rector\NodeAnalyzer\Contrib\Symfony\ControllerMethodAnalyzer;
1111
use Rector\NodeAnalyzer\MethodCallAnalyzer;
1212
use Rector\Rector\AbstractRector;
1313

src/Rector/Contrib/Symfony/HttpKernel/GetterToPropertyRector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Rector\Naming\PropertyNaming;
1010
use Rector\Node\Attribute;
1111
use Rector\Node\NodeFactory;
12-
use Rector\NodeAnalyzer\Contrib\SymfonyContainerCallsAnalyzer;
12+
use Rector\NodeAnalyzer\Contrib\Symfony\ContainerCallAnalyzer;
1313
use Rector\Rector\AbstractRector;
1414

1515
/**
@@ -37,9 +37,9 @@ final class GetterToPropertyRector extends AbstractRector
3737
private $nodeFactory;
3838

3939
/**
40-
* @var SymfonyContainerCallsAnalyzer
40+
* @var ContainerCallAnalyzer
4141
*/
42-
private $symfonyContainerCallsAnalyzer;
42+
private $containerCallAnalyzer;
4343

4444
/**
4545
* @var ServiceTypeForNameProviderInterface
@@ -50,13 +50,13 @@ public function __construct(
5050
PropertyNaming $propertyNaming,
5151
ClassPropertyCollector $classPropertyCollector,
5252
NodeFactory $nodeFactory,
53-
SymfonyContainerCallsAnalyzer $symfonyContainerCallsAnalyzer,
53+
ContainerCallAnalyzer $containerCallAnalyzer,
5454
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider
5555
) {
5656
$this->propertyNaming = $propertyNaming;
5757
$this->classPropertyCollector = $classPropertyCollector;
5858
$this->nodeFactory = $nodeFactory;
59-
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
59+
$this->containerCallAnalyzer = $containerCallAnalyzer;
6060
$this->serviceTypeForNameProvider = $serviceTypeForNameProvider;
6161
}
6262

@@ -66,7 +66,7 @@ public function isCandidate(Node $node): bool
6666
return false;
6767
}
6868

69-
return $this->symfonyContainerCallsAnalyzer->isThisCall($node);
69+
return $this->containerCallAnalyzer->isThisCall($node);
7070
}
7171

7272
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare (strict_types=1);
2+
3+
use Symfony\Component\Form\AbstractTypeExtension;
4+
5+
class TypeExtension extends AbstractTypeExtension
6+
{
7+
public function getExtendedType()
8+
{
9+
return \Symfony\Component\Form\Extension\Core\Type\FormType::class;
10+
}
11+
}

tests/Rector/Contrib/Symfony/Form/FormTypeGetParentRector/Test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public function test(): void
1313
__DIR__ . '/Wrong/wrong.php.inc',
1414
__DIR__ . '/Correct/correct.php.inc'
1515
);
16+
17+
$this->doTestFileMatchesExpectedContent(
18+
__DIR__ . '/Wrong/wrong2.php.inc',
19+
__DIR__ . '/Correct/correct2.php.inc'
20+
);
1621
}
1722

1823
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare (strict_types=1);
2+
3+
use Symfony\Component\Form\AbstractTypeExtension;
4+
5+
class TypeExtension extends AbstractTypeExtension
6+
{
7+
public function getExtendedType()
8+
{
9+
return 'form';
10+
}
11+
}

0 commit comments

Comments
 (0)