-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use autowired method if exists in adding new dependency (#6652)
* Use autowired method if exists in adding new dependency * Add constructor fixture, just to verify alter behavior
- Loading branch information
1 parent
8bc80b4
commit fe99f21
Showing
8 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/Issues/AddClassDependency/AddClassDependencyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddClassDependencyTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency\Fixture; | ||
|
||
use Rector\Tests\Issues\AddClassDependency\Source\SomeAutowiredService; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
class PreferRequiredSetter extends Controller | ||
{ | ||
private SomeAutowiredService $someAutowiredService; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function autowire( | ||
SomeAutowiredService $someAutowiredService, | ||
) { | ||
$this->someAutowiredService = $someAutowiredService; | ||
} | ||
|
||
public function configure() | ||
{ | ||
$someType = $this->get('validator'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency\Fixture; | ||
|
||
use Rector\Tests\Issues\AddClassDependency\Source\SomeAutowiredService; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
class PreferRequiredSetter extends Controller | ||
{ | ||
private SomeAutowiredService $someAutowiredService; | ||
private \Symfony\Component\Validator\Validator\ValidatorInterface $validator; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function autowire( | ||
SomeAutowiredService $someAutowiredService, \Symfony\Component\Validator\Validator\ValidatorInterface $validator, | ||
) { | ||
$this->someAutowiredService = $someAutowiredService; | ||
$this->validator = $validator; | ||
} | ||
|
||
public function configure() | ||
{ | ||
$someType = $this->validator; | ||
} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
tests/Issues/AddClassDependency/Fixture/strick_with_constructor.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency\Fixture; | ||
|
||
use Rector\Tests\Issues\AddClassDependency\Source\SomeAutowiredService; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
final class StickWithConstructor extends Controller | ||
{ | ||
public function __construct( | ||
private SomeAutowiredService $someAutowiredService, | ||
) { | ||
} | ||
|
||
public function configure() | ||
{ | ||
$someType = $this->get('validator'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency\Fixture; | ||
|
||
use Rector\Tests\Issues\AddClassDependency\Source\SomeAutowiredService; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
final class StickWithConstructor extends Controller | ||
{ | ||
public function __construct( | ||
private SomeAutowiredService $someAutowiredService, private readonly \Symfony\Component\Validator\Validator\ValidatorInterface $validator, | ||
) { | ||
} | ||
|
||
public function configure() | ||
{ | ||
$someType = $this->validator; | ||
} | ||
} | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
tests/Issues/AddClassDependency/Source/SomeAutowiredService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Issues\AddClassDependency\Source; | ||
|
||
final class SomeAutowiredService | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([GetBySymfonyStringToConstructorInjectionRector::class]); |