Skip to content

Commit a9dfc5d

Browse files
authored
[config] Add RemoveConstructorAutowireServiceRector (#855)
* [config] Add RemoveConstructorAutowireServiceRector * decouple class to strings
1 parent 6337a1b commit a9dfc5d

21 files changed

+493
-29
lines changed

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@
1212
"phpstan/extension-installer": "^1.4",
1313
"phpstan/phpstan": "^2.1.31",
1414
"phpstan/phpstan-webmozart-assert": "^2.0",
15-
"phpunit/phpunit": "^11.4",
15+
"phpunit/phpunit": "^11.5",
16+
"rector/jack": "^0.2.9",
1617
"rector/rector-src": "dev-main",
17-
"rector/type-perfect": "^2.0",
18+
"rector/type-perfect": "^2.1",
1819
"symfony/config": "^6.4",
1920
"symfony/dependency-injection": "^6.4",
20-
"symfony/http-kernel": "^6.4",
21+
"symfony/http-kernel": "^7.0",
2122
"symfony/routing": "^6.4",
2223
"symfony/security-core": "^6.4",
2324
"symfony/security-http": "^6.4",
2425
"symfony/validator": "^6.4",
2526
"symfony/web-link": "^6.4",
2627
"symplify/phpstan-extensions": "^12.0",
27-
"symplify/phpstan-rules": "^14.6",
28-
"symplify/vendor-patches": "^11.3",
28+
"symplify/phpstan-rules": "^14.8",
29+
"symplify/vendor-patches": "^11.5",
2930
"tomasvotruba/class-leak": "^2.0",
3031
"tomasvotruba/type-coverage": "^2.0",
3132
"tomasvotruba/unused-public": "^2.0",

config/sets/symfony/configs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\Symfony\Configs\Rector\Closure\MergeServiceNameTypeRector;
7+
use Rector\Symfony\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector;
78
use Rector\Symfony\Configs\Rector\Closure\ServiceArgsToServiceNamedArgRector;
89
use Rector\Symfony\Configs\Rector\Closure\ServiceSetStringNameToClassNameRector;
910
use Rector\Symfony\Configs\Rector\Closure\ServiceSettersToSettersAutodiscoveryRector;
@@ -16,5 +17,6 @@
1617
ServiceSetStringNameToClassNameRector::class,
1718
ServiceSettersToSettersAutodiscoveryRector::class,
1819
ServiceTagsToDefaultsAutoconfigureRector::class,
20+
RemoveConstructorAutowireServiceRector::class,
1921
]);
2022
};

phpstan.neon

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,10 @@ parameters:
3939
# pointless check
4040
- '#expects class-string, string given#'
4141

42-
- '#Calling PHPStan\\Reflection\\Php\\PhpParameterReflection\:\:getName\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version#'
43-
4442
# false positive
4543
- '#but class PhpParser\\Node\\Stmt\\Expression is not generic#'
4644
- '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
4745

48-
# false positive
49-
- '#Parameters should have "PhpParser\\Node\\Expr\\Closure" types as the only types passed to this method#'
50-
- '#Parameter 1 should use "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionMethod" type as the only type passed to this method#'
51-
5246
# more advanced usage, but not always working
5347
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
5448
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'
@@ -59,3 +53,9 @@ parameters:
5953

6054
# avoid notice on run on php 8.3
6155
- identifier: typeCoverage.constantTypeCoverage
56+
57+
# false postitive
58+
-
59+
identifier: typePerfect.narrowPublicClassMethodParamType
60+
path: src/NodeAnalyzer/SymfonyPhpClosureDetector.php
61+
- '#Parameter 1 should use "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionMethod" type as the only type passed to this method#'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Fixture;
6+
7+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\AnotherClassWithoutConstructor;
8+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\DifferentType;
9+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\PassedAsDependency;
10+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
11+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
12+
13+
return static function (ContainerConfigurator $containerConfigurator): void {
14+
$services = $containerConfigurator->services();
15+
16+
$services->defaults()->autowire();
17+
18+
$services->set(AnotherClassWithoutConstructor::class)
19+
->arg('$passedAsDependency', service(DifferentType::class));
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Fixture;
6+
7+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\AnotherClassWithoutConstructor;
8+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\PassedAsDependency;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
11+
12+
return static function (ContainerConfigurator $containerConfigurator): void {
13+
$services = $containerConfigurator->services();
14+
15+
$services->set(AnotherClassWithoutConstructor::class)
16+
->arg('$passedAsDependency', service(PassedAsDependency::class));
17+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Fixture;
6+
7+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\AnotherClassWithoutConstructor;
8+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\PassedAsDependency;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
11+
12+
return static function (ContainerConfigurator $containerConfigurator): void {
13+
$services = $containerConfigurator->services();
14+
15+
$services->defaults()->autowire();
16+
17+
$services->set(AnotherClassWithoutConstructor::class)
18+
->arg('$passedAsDependency', 123);
19+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Fixture;
6+
7+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\AnotherClassWithoutConstructor;
8+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\PassedAsDependency;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
11+
12+
return static function (ContainerConfigurator $containerConfigurator): void {
13+
$services = $containerConfigurator->services();
14+
15+
$services->defaults()->autowire();
16+
17+
$services->set(AnotherClassWithoutConstructor::class)
18+
->arg('$passedAsDependency', service(PassedAsDependency::class));
19+
};
20+
21+
?>
22+
-----
23+
<?php
24+
25+
declare(strict_types=1);
26+
27+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Fixture;
28+
29+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\AnotherClassWithoutConstructor;
30+
use Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source\PassedAsDependency;
31+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
32+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
33+
34+
return static function (ContainerConfigurator $containerConfigurator): void {
35+
$services = $containerConfigurator->services();
36+
37+
$services->defaults()->autowire();
38+
39+
$services->set(AnotherClassWithoutConstructor::class);
40+
};
41+
42+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveConstructorAutowireServiceRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source;
4+
5+
final class AnotherClassWithoutConstructor
6+
{
7+
public function __construct(
8+
PassedAsDependency $passedAsDependency
9+
) {
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\RemoveConstructorAutowireServiceRector\Source;
4+
5+
final class DifferentType
6+
{
7+
}

0 commit comments

Comments
 (0)