-
-
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
- Loading branch information
1 parent
8bc80b4
commit 56afd54
Showing
7 changed files
with
157 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; | ||
} | ||
} | ||
|
||
?> |
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 | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Issues/AddClassDependency/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return RectorConfig::configure() | ||
->withRules([ | ||
GetBySymfonyStringToConstructorInjectionRector::class, | ||
]); |