File tree Expand file tree Collapse file tree 3 files changed +42
-4
lines changed Expand file tree Collapse file tree 3 files changed +42
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Psalm \SymfonyPsalmPlugin \Handler ;
4
4
5
+ use function constant ;
5
6
use PhpParser \Node \Expr \ClassConstFetch ;
7
+ use PhpParser \Node \Identifier ;
6
8
use PhpParser \Node \Scalar \String_ ;
7
9
use Psalm \CodeLocation ;
8
10
use Psalm \IssueBuffer ;
@@ -73,10 +75,28 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
73
75
return ;
74
76
}
75
77
76
- if ($ expr ->args [0 ]->value instanceof String_) {
77
- $ serviceId = $ expr ->args [0 ]->value ->value ;
78
- } elseif ($ expr ->args [0 ]->value instanceof ClassConstFetch) {
79
- $ serviceId = (string ) $ expr ->args [0 ]->value ->class ->getAttribute ('resolvedName ' );
78
+ $ idArgument = $ expr ->args [0 ]->value ;
79
+
80
+ if ($ idArgument instanceof String_) {
81
+ $ serviceId = $ idArgument ->value ;
82
+ } elseif ($ idArgument instanceof ClassConstFetch) {
83
+ $ className = (string ) $ idArgument ->class ->getAttribute ('resolvedName ' );
84
+ if ('self ' === $ className ) {
85
+ $ className = $ event ->getStatementsSource ()->getSource ()->getFQCLN ();
86
+ }
87
+ if (!$ idArgument ->name instanceof Identifier || !$ className ) {
88
+ return ;
89
+ }
90
+
91
+ if ('class ' === $ idArgument ->name ->name ) {
92
+ $ serviceId = $ className ;
93
+ } else {
94
+ try {
95
+ $ serviceId = constant ($ className .':: ' .$ idArgument ->name ->name );
96
+ } catch (\Exception $ e ) {
97
+ return ;
98
+ }
99
+ }
80
100
} else {
81
101
return ;
82
102
}
Original file line number Diff line number Diff line change @@ -57,3 +57,19 @@ Feature: Service Subscriber
57
57
| Trace | $service2 : Psalm \SymfonyPsalmPlugin \Tests \Fixture \DummyPrivateService |
58
58
| Trace | $service3 : Psalm \SymfonyPsalmPlugin \Tests \Fixture \DummyPrivateService |
59
59
And I see no other errors
60
+
61
+ Scenario : Asserting psalm recognizes return type of services fetched name by PHP constants.
62
+ Given I have the following code
63
+ """
64
+ public function __invoke()
65
+ {
66
+ /** @psalm-trace $service1 */
67
+ $service1 = $this->container->get(\Psalm\SymfonyPsalmPlugin\Tests\Fixture\DummyPrivateService::CUSTOM_SERVICE_NAME);
68
+ }
69
+ }
70
+ """
71
+ When I run Psalm
72
+ Then I see these errors
73
+ | Type | Message |
74
+ | Trace | $service1 : Psalm \SymfonyPsalmPlugin \Tests \Fixture \DummyPrivateService |
75
+ And I see no other errors
Original file line number Diff line number Diff line change 4
4
5
5
class DummyPrivateService
6
6
{
7
+ const CUSTOM_SERVICE_NAME = 'dummy_service_with_locator ' ;
8
+
7
9
public function foo (): string
8
10
{
9
11
return 'foo ' ;
You can’t perform that action at this time.
0 commit comments