File tree 4 files changed +124
-0
lines changed
4 files changed +124
-0
lines changed Original file line number Diff line number Diff line change 8
8
use PHPStan \Testing \RuleTestCase ;
9
9
use function class_exists ;
10
10
use function interface_exists ;
11
+ use const PHP_VERSION_ID ;
11
12
12
13
/**
13
14
* @extends RuleTestCase<ContainerInterfacePrivateServiceRule>
@@ -78,4 +79,37 @@ public function testGetPrivateServiceInServiceSubscriber(): void
78
79
);
79
80
}
80
81
82
+ public function testGetPrivateServiceWithoutAutowireLocatorAttribute (): void
83
+ {
84
+ if (PHP_VERSION_ID < 80000 ) {
85
+ self ::markTestSkipped ('The test uses PHP Attributes which are available since PHP 8.0. ' );
86
+ }
87
+
88
+ $ this ->analyse (
89
+ [
90
+ __DIR__ . '/ExampleAutowireLocatorEmptyService.php ' ,
91
+ ],
92
+ [
93
+ [
94
+ 'Service "private" is private. ' ,
95
+ 22 ,
96
+ ],
97
+ ]
98
+ );
99
+ }
100
+
101
+ public function testGetPrivateServiceViaAutowireLocatorAttribute (): void
102
+ {
103
+ if (PHP_VERSION_ID < 80000 ) {
104
+ self ::markTestSkipped ('The test uses PHP Attributes which are available since PHP 8.0. ' );
105
+ }
106
+
107
+ $ this ->analyse (
108
+ [
109
+ __DIR__ . '/ExampleAutowireLocatorService.php ' ,
110
+ ],
111
+ []
112
+ );
113
+ }
114
+
81
115
}
Original file line number Diff line number Diff line change 9
9
use PHPStan \Testing \RuleTestCase ;
10
10
use function class_exists ;
11
11
use function interface_exists ;
12
+ use const PHP_VERSION_ID ;
12
13
13
14
/**
14
15
* @extends RuleTestCase<ContainerInterfaceUnknownServiceRule>
@@ -72,6 +73,43 @@ public function testGetPrivateServiceInLegacyServiceSubscriber(): void
72
73
);
73
74
}
74
75
76
+ public function testGetPrivateServiceWithoutAutowireLocatorAttribute (): void
77
+ {
78
+ if (PHP_VERSION_ID < 80000 ) {
79
+ self ::markTestSkipped ('The test uses PHP Attributes which are available since PHP 8.0. ' );
80
+ }
81
+
82
+ $ this ->analyse (
83
+ [
84
+ __DIR__ . '/ExampleAutowireLocatorEmptyService.php ' ,
85
+ ],
86
+ [
87
+ [
88
+ 'Service "Foo" is not registered in the AutowireLocator. ' ,
89
+ 21 ,
90
+ ],
91
+ [
92
+ 'Service "private" is not registered in the AutowireLocator. ' ,
93
+ 22 ,
94
+ ],
95
+ ]
96
+ );
97
+ }
98
+
99
+ public function testGetPrivateServiceViaAutowireLocatorAttribute (): void
100
+ {
101
+ if (PHP_VERSION_ID < 80000 ) {
102
+ self ::markTestSkipped ('The test uses PHP Attributes which are available since PHP 8.0. ' );
103
+ }
104
+
105
+ $ this ->analyse (
106
+ [
107
+ __DIR__ . '/ExampleAutowireLocatorService.php ' ,
108
+ ],
109
+ []
110
+ );
111
+ }
112
+
75
113
public static function getAdditionalConfigFiles (): array
76
114
{
77
115
return [
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \Symfony ;
4
+
5
+ use Psr \Container \ContainerInterface ;
6
+ use Symfony \Component \DependencyInjection \Attribute \AutowireLocator ;
7
+
8
+ final class ExampleAutowireLocatorEmptyService
9
+ {
10
+
11
+ public function __construct (
12
+ #[AutowireLocator([])]
13
+ private ContainerInterface $ locator
14
+ )
15
+ {
16
+ $ this ->locator = $ locator ;
17
+ }
18
+
19
+ public function privateServiceInLocator (): void
20
+ {
21
+ $ this ->locator ->get ('Foo ' );
22
+ $ this ->locator ->get ('private ' );
23
+ }
24
+
25
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \Symfony ;
4
+
5
+ use Psr \Container \ContainerInterface ;
6
+ use Symfony \Component \DependencyInjection \Attribute \AutowireLocator ;
7
+
8
+ final class ExampleAutowireLocatorService
9
+ {
10
+
11
+ public function __construct (
12
+ #[AutowireLocator([
13
+ 'Foo ' => 'Foo ' ,
14
+ 'private ' => 'Foo ' ,
15
+ ])]
16
+ private ContainerInterface $ locator
17
+ )
18
+ {
19
+ }
20
+
21
+ public function privateServiceInLocator (): void
22
+ {
23
+ $ this ->locator ->get ('Foo ' );
24
+ $ this ->locator ->get ('private ' );
25
+ }
26
+
27
+ }
You can’t perform that action at this time.
0 commit comments