Skip to content

Commit b1f0aac

Browse files
authored
fix tests for new psalm versions (#228)
1 parent 6558660 commit b1f0aac

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": "^7.1 || ^8.0",
1414
"ext-simplexml": "*",
1515
"symfony/framework-bundle": "^4.0 || ^5.0",
16-
"vimeo/psalm": "^4.9"
16+
"vimeo/psalm": "^4.11"
1717
},
1818
"require-dev": {
1919
"symfony/form": "^4.0 || ^5.0",

src/Handler/ConsoleHandler.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
4646
$expr = $event->getExpr();
4747
$declaring_method_id = $event->getDeclaringMethodId();
4848

49+
$args = [];
50+
foreach ($expr->args as $arg) {
51+
if ($arg instanceof Arg) {
52+
$args[] = $arg;
53+
}
54+
}
55+
4956
switch ($declaring_method_id) {
5057
case 'Symfony\Component\Console\Command\Command::addargument':
51-
self::analyseArgument($expr->args, $statements_source);
58+
self::analyseArgument($args, $statements_source);
5259
break;
5360
case 'Symfony\Component\Console\Input\InputInterface::getargument':
54-
$identifier = self::getNodeIdentifier($expr->args[0]->value);
61+
$identifier = self::getNodeIdentifier($args[0]->value);
5562
if (!$identifier) {
5663
break;
5764
}
@@ -61,10 +68,10 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
6168
}
6269
break;
6370
case 'Symfony\Component\Console\Command\Command::addoption':
64-
self::analyseOption($expr->args, $statements_source);
71+
self::analyseOption($args, $statements_source);
6572
break;
6673
case 'Symfony\Component\Console\Input\InputInterface::getoption':
67-
$identifier = self::getNodeIdentifier($expr->args[0]->value);
74+
$identifier = self::getNodeIdentifier($args[0]->value);
6875
if (!$identifier) {
6976
break;
7077
}
@@ -75,10 +82,10 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
7582
break;
7683
case 'Symfony\Component\Console\Command\Command::setdefinition':
7784
$inputItems = [];
78-
$definition = $expr->args[0]->value;
85+
$definition = $args[0]->value;
7986
if ($definition instanceof Expr\Array_) {
8087
$inputItems = $definition->items;
81-
} elseif ($definition instanceof Expr\New_) {
88+
} elseif ($definition instanceof Expr\New_ && isset($definition->args[0]->value)) {
8289
$inputDefinition = $definition->args[0]->value;
8390
if ($inputDefinition instanceof Expr\Array_) {
8491
$inputItems = $inputDefinition->items;
@@ -91,12 +98,19 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
9198

9299
foreach ($inputItems as $inputItem) {
93100
if ($inputItem instanceof Expr\ArrayItem && $inputItem->value instanceof Expr\New_) {
101+
$args = [];
102+
foreach ($inputItem->value->args as $arg) {
103+
if ($arg instanceof Arg) {
104+
$args[] = $arg;
105+
}
106+
}
107+
94108
switch ($inputItem->value->class->getAttribute('resolvedName')) {
95109
case InputArgument::class:
96-
self::analyseArgument($inputItem->value->args, $statements_source);
110+
self::analyseArgument($args, $statements_source);
97111
break;
98112
case InputOption::class:
99-
self::analyseOption($inputItem->value->args, $statements_source);
113+
self::analyseOption($args, $statements_source);
100114
break;
101115
}
102116
}

src/Handler/ContainerHandler.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Psalm\SymfonyPsalmPlugin\Handler;
44

55
use function constant;
6+
use PhpParser\Node\Arg;
67
use PhpParser\Node\Expr\ClassConstFetch;
78
use PhpParser\Node\Identifier;
89
use PhpParser\Node\Scalar\String_;
@@ -50,9 +51,18 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
5051
$codebase = $event->getCodebase();
5152
$context = $event->getContext();
5253

54+
if (!isset($expr->args[0])) {
55+
return;
56+
}
57+
58+
$firstArg = $expr->args[0];
59+
if (!$firstArg instanceof Arg) {
60+
return;
61+
}
62+
5363
if (!self::isContainerMethod($declaring_method_id, 'get')) {
5464
if (self::isContainerMethod($declaring_method_id, 'getparameter')) {
55-
$argument = $expr->args[0]->value;
65+
$argument = $firstArg->value;
5666
if ($argument instanceof String_ && !self::followsNamingConvention($argument->value) && false === strpos($argument->value, '\\')) {
5767
IssueBuffer::accepts(
5868
new NamingConventionViolation(new CodeLocation($statements_source, $argument)),
@@ -65,8 +75,8 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
6575
}
6676

6777
if (!self::$containerMeta) {
68-
if ($event->getReturnTypeCandidate() && $expr->args[0]->value instanceof ClassConstFetch) {
69-
$className = (string) $expr->args[0]->value->class->getAttribute('resolvedName');
78+
if ($event->getReturnTypeCandidate() && $firstArg->value instanceof ClassConstFetch) {
79+
$className = (string) $firstArg->value->class->getAttribute('resolvedName');
7080
if (!in_array($className, ['self', 'parent', 'static'])) {
7181
$event->setReturnTypeCandidate(new Union([new TNamedObject($className)]));
7282
}
@@ -75,7 +85,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
7585
return;
7686
}
7787

78-
$idArgument = $expr->args[0]->value;
88+
$idArgument = $firstArg->value;
7989

8090
if ($idArgument instanceof String_) {
8191
$serviceId = $idArgument->value;
@@ -106,7 +116,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
106116

107117
if (!self::followsNamingConvention($serviceId) && false === strpos($serviceId, '\\')) {
108118
IssueBuffer::accepts(
109-
new NamingConventionViolation(new CodeLocation($statements_source, $expr->args[0]->value)),
119+
new NamingConventionViolation(new CodeLocation($statements_source, $firstArg->value)),
110120
$statements_source->getSuppressedIssues()
111121
);
112122
}
@@ -126,14 +136,14 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
126136
);
127137
if (!$isTestContainer) {
128138
IssueBuffer::accepts(
129-
new PrivateService($serviceId, new CodeLocation($statements_source, $expr->args[0]->value)),
139+
new PrivateService($serviceId, new CodeLocation($statements_source, $firstArg->value)),
130140
$statements_source->getSuppressedIssues()
131141
);
132142
}
133143
}
134144
} catch (ServiceNotFoundException $e) {
135145
IssueBuffer::accepts(
136-
new ServiceNotFound($serviceId, new CodeLocation($statements_source, $expr->args[0]->value)),
146+
new ServiceNotFound($serviceId, new CodeLocation($statements_source, $firstArg->value)),
137147
$statements_source->getSuppressedIssues()
138148
);
139149
}

src/Handler/DoctrineQueryBuilderHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
2727
return;
2828
}
2929

30+
if (!isset($expr->args[1]->value)) {
31+
return;
32+
}
3033
$value = $expr->args[1]->value;
3134

3235
if (self::isValueObject($value, $context)) {

src/Handler/DoctrineRepositoryHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
3030
$statements_source = $event->getStatementsSource();
3131

3232
if (in_array($declaring_method_id, ['Doctrine\ORM\EntityManagerInterface::getrepository', 'Doctrine\Persistence\ObjectManager::getrepository'])) {
33+
if (!isset($expr->args[0]->value)) {
34+
return;
35+
}
36+
3337
$entityName = $expr->args[0]->value;
3438
if ($entityName instanceof String_) {
3539
IssueBuffer::accepts(

tests/acceptance/acceptance/Envelope.feature

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,14 @@ Feature: Messenger Envelope
162162
$stamps = $envelope->all();
163163
foreach ($stamps as $className => $classStamps) {
164164
foreach ($classStamps as $stamp) {
165-
if ($stamp instanceof StampInterface) {
166-
echo 'always true';
167-
}
165+
/** @psalm-trace $stamp */
168166
}
169167
}
170168
"""
171169
When I run Psalm
172170
Then I see these errors
173-
| Type | Message |
174-
| RedundantConditionGivenDocblockType | Docblock-defined type Symfony\Component\Messenger\Stamp\StampInterface for $stamp is always Symfony\Component\Messenger\Stamp\StampInterface |
171+
| Type | Message |
172+
| Trace | $stamp: Symfony\Component\Messenger\Stamp\StampInterface |
175173
And I see no other errors
176174

177175
Scenario: Envelope::getMessage returns a message of a valid class

0 commit comments

Comments
 (0)