Skip to content

Commit 2992656

Browse files
authored
Upgrade Psalm interfaces (#186)
1 parent 74cfb5f commit 2992656

18 files changed

+159
-194
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": "^3.0 || ^4.0 || ^5.0",
16-
"vimeo/psalm": "^4.6.1"
16+
"vimeo/psalm": "^4.8"
1717
},
1818
"require-dev": {
1919
"symfony/form": "^4.0 || ^5.0",

src/Handler/AnnotationHandler.php

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

55
use PhpParser\Comment\Doc;
66
use PhpParser\Node\Stmt\Class_;
7-
use PhpParser\Node\Stmt\ClassLike;
8-
use Psalm\Codebase;
9-
use Psalm\FileSource;
10-
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
11-
use Psalm\Storage\ClassLikeStorage;
7+
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
8+
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
129

1310
class AnnotationHandler implements AfterClassLikeVisitInterface
1411
{
1512
/**
1613
* {@inheritdoc}
1714
*/
18-
public static function afterClassLikeVisit(ClassLike $stmt, ClassLikeStorage $storage, FileSource $statements_source, Codebase $codebase, array &$file_replacements = [])
15+
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
1916
{
17+
$stmt = $event->getStmt();
18+
$storage = $event->getStorage();
19+
2020
if (!$stmt instanceof Class_) {
2121
return;
2222
}

src/Handler/ConsoleHandler.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Identifier;
1010
use PhpParser\Node\Scalar\String_;
11-
use Psalm\Codebase;
1211
use Psalm\CodeLocation;
13-
use Psalm\Context;
1412
use Psalm\IssueBuffer;
15-
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
13+
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
14+
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
1615
use Psalm\StatementsSource;
1716
use Psalm\SymfonyPsalmPlugin\Exception\InvalidConsoleModeException;
1817
use Psalm\SymfonyPsalmPlugin\Issue\InvalidConsoleArgumentValue;
@@ -41,17 +40,12 @@ class ConsoleHandler implements AfterMethodCallAnalysisInterface
4140
/**
4241
* {@inheritdoc}
4342
*/
44-
public static function afterMethodCallAnalysis(
45-
Expr $expr,
46-
string $method_id,
47-
string $appearing_method_id,
48-
string $declaring_method_id,
49-
Context $context,
50-
StatementsSource $statements_source,
51-
Codebase $codebase,
52-
array &$file_replacements = [],
53-
Union &$return_type_candidate = null
54-
): void {
43+
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
44+
{
45+
$statements_source = $event->getStatementsSource();
46+
$expr = $event->getExpr();
47+
$declaring_method_id = $event->getDeclaringMethodId();
48+
5549
switch ($declaring_method_id) {
5650
case 'Symfony\Component\Console\Command\Command::addargument':
5751
self::analyseArgument($expr->args, $statements_source);
@@ -63,7 +57,7 @@ public static function afterMethodCallAnalysis(
6357
}
6458

6559
if (isset(self::$arguments[$identifier])) {
66-
$return_type_candidate = self::$arguments[$identifier];
60+
$event->setReturnTypeCandidate(self::$arguments[$identifier]);
6761
}
6862
break;
6963
case 'Symfony\Component\Console\Command\Command::addoption':
@@ -76,7 +70,7 @@ public static function afterMethodCallAnalysis(
7670
}
7771

7872
if (isset(self::$options[$identifier])) {
79-
$return_type_candidate = self::$options[$identifier];
73+
$event->setReturnTypeCandidate(self::$options[$identifier]);
8074
}
8175
break;
8276
case 'Symfony\Component\Console\Command\Command::setdefinition':

src/Handler/ContainerDependencyHandler.php

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

55
use PhpParser\Node;
6-
use Psalm\Codebase;
76
use Psalm\CodeLocation;
87
use Psalm\IssueBuffer;
9-
use Psalm\Plugin\Hook\AfterFunctionLikeAnalysisInterface;
10-
use Psalm\StatementsSource;
11-
use Psalm\Storage\FunctionLikeStorage;
8+
use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
9+
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
1210
use Psalm\SymfonyPsalmPlugin\Issue\ContainerDependency;
1311
use Symfony\Component\DependencyInjection\ContainerInterface;
1412

@@ -17,13 +15,11 @@ class ContainerDependencyHandler implements AfterFunctionLikeAnalysisInterface
1715
/**
1816
* {@inheritdoc}
1917
*/
20-
public static function afterStatementAnalysis(
21-
Node\FunctionLike $stmt,
22-
FunctionLikeStorage $classlike_storage,
23-
StatementsSource $statements_source,
24-
Codebase $codebase,
25-
array &$file_replacements = []
26-
): ?bool {
18+
public static function afterStatementAnalysis(AfterFunctionLikeAnalysisEvent $event): ?bool
19+
{
20+
$stmt = $event->getStmt();
21+
$statements_source = $event->getStatementsSource();
22+
2723
if ($stmt instanceof Node\Stmt\ClassMethod && '__construct' === $stmt->name->name) {
2824
foreach ($stmt->params as $param) {
2925
if ($param->type instanceof Node\Name && ContainerInterface::class === $param->type->getAttribute('resolvedName')) {

src/Handler/ContainerHandler.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
use PhpParser\Node\Name;
88
use PhpParser\Node\Scalar\String_;
99
use PhpParser\Node\Stmt\Class_;
10-
use PhpParser\Node\Stmt\ClassLike;
1110
use PhpParser\Node\Stmt\ClassMethod;
1211
use PhpParser\Node\Stmt\Return_;
1312
use Psalm\Codebase;
1413
use Psalm\CodeLocation;
15-
use Psalm\Context;
16-
use Psalm\FileSource;
1714
use Psalm\IssueBuffer;
18-
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
19-
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
20-
use Psalm\StatementsSource;
21-
use Psalm\Storage\ClassLikeStorage;
15+
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
16+
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
17+
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
18+
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
2219
use Psalm\Storage\FileStorage;
2320
use Psalm\SymfonyPsalmPlugin\Issue\NamingConventionViolation;
2421
use Psalm\SymfonyPsalmPlugin\Issue\PrivateService;
@@ -50,17 +47,14 @@ public static function init(ContainerMeta $containerMeta): void
5047
/**
5148
* {@inheritdoc}
5249
*/
53-
public static function afterMethodCallAnalysis(
54-
Expr $expr,
55-
string $method_id,
56-
string $appearing_method_id,
57-
string $declaring_method_id,
58-
Context $context,
59-
StatementsSource $statements_source,
60-
Codebase $codebase,
61-
array &$file_replacements = [],
62-
Union &$return_type_candidate = null
63-
): void {
50+
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
51+
{
52+
$declaring_method_id = $event->getDeclaringMethodId();
53+
$statements_source = $event->getStatementsSource();
54+
$expr = $event->getExpr();
55+
$codebase = $event->getCodebase();
56+
$context = $event->getContext();
57+
6458
if (!self::isContainerMethod($declaring_method_id, 'get')) {
6559
if (self::isContainerMethod($declaring_method_id, 'getparameter')) {
6660
$argument = $expr->args[0]->value;
@@ -76,10 +70,10 @@ public static function afterMethodCallAnalysis(
7670
}
7771

7872
if (!self::$containerMeta) {
79-
if ($return_type_candidate && $expr->args[0]->value instanceof ClassConstFetch) {
73+
if ($event->getReturnTypeCandidate() && $expr->args[0]->value instanceof ClassConstFetch) {
8074
$className = (string) $expr->args[0]->value->class->getAttribute('resolvedName');
8175
if (!in_array($className, ['self', 'parent', 'static'])) {
82-
$return_type_candidate = new Union([new TNamedObject($className)]);
76+
$event->setReturnTypeCandidate(new Union([new TNamedObject($className)]));
8377
}
8478
}
8579

@@ -106,7 +100,7 @@ public static function afterMethodCallAnalysis(
106100
$class = $service->getClassName();
107101
if ($class) {
108102
$codebase->classlikes->addFullyQualifiedClassName($class);
109-
$return_type_candidate = new Union([new TNamedObject($class)]);
103+
$event->setReturnTypeCandidate(new Union([new TNamedObject($class)]));
110104
}
111105

112106
if (!$service->isPublic()) {
@@ -129,13 +123,13 @@ public static function afterMethodCallAnalysis(
129123
/**
130124
* {@inheritdoc}
131125
*/
132-
public static function afterClassLikeVisit(
133-
ClassLike $stmt,
134-
ClassLikeStorage $storage,
135-
FileSource $statements_source,
136-
Codebase $codebase,
137-
array &$file_replacements = []
138-
) {
126+
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
127+
{
128+
$codebase = $event->getCodebase();
129+
$statements_source = $event->getStatementsSource();
130+
$storage = $event->getStorage();
131+
$stmt = $event->getStmt();
132+
139133
$fileStorage = $codebase->file_storage_provider->get($statements_source->getFilePath());
140134

141135
if (\in_array($storage->name, ContainerHandler::GET_CLASSLIKES)) {

src/Handler/DoctrineQueryBuilderHandler.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@
33
namespace Psalm\SymfonyPsalmPlugin\Handler;
44

55
use PhpParser\Node\Expr;
6-
use Psalm\Codebase;
76
use Psalm\CodeLocation;
87
use Psalm\Context;
98
use Psalm\IssueBuffer;
10-
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
11-
use Psalm\StatementsSource;
9+
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
10+
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
1211
use Psalm\SymfonyPsalmPlugin\Issue\QueryBuilderSetParameter;
13-
use Psalm\Type\Union;
1412

1513
class DoctrineQueryBuilderHandler implements AfterMethodCallAnalysisInterface
1614
{
1715
/**
1816
* {@inheritdoc}
1917
*/
20-
public static function afterMethodCallAnalysis(
21-
Expr $expr,
22-
string $method_id,
23-
string $appearing_method_id,
24-
string $declaring_method_id,
25-
Context $context,
26-
StatementsSource $statements_source,
27-
Codebase $codebase,
28-
array &$file_replacements = [],
29-
Union &$return_type_candidate = null
30-
): void {
18+
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
19+
{
20+
$expr = $event->getExpr();
21+
$declaring_method_id = $event->getDeclaringMethodId();
22+
$statements_source = $event->getStatementsSource();
23+
$context = $event->getContext();
24+
3125
if ('Doctrine\ORM\QueryBuilder::setparameter' === $declaring_method_id) {
3226
if (isset($expr->args[2])) {
3327
return;

src/Handler/DoctrineRepositoryHandler.php

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
use Doctrine\ORM\Mapping\Entity as EntityAnnotation;
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Scalar\String_;
9-
use PhpParser\Node\Stmt\ClassLike;
10-
use Psalm\Codebase;
119
use Psalm\CodeLocation;
12-
use Psalm\Context;
1310
use Psalm\DocComment;
1411
use Psalm\Exception\DocblockParseException;
15-
use Psalm\FileSource;
1612
use Psalm\IssueBuffer;
17-
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
18-
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
19-
use Psalm\StatementsSource;
20-
use Psalm\Storage\ClassLikeStorage;
13+
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
14+
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
15+
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
16+
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
2117
use Psalm\SymfonyPsalmPlugin\Issue\RepositoryStringShortcut;
2218
use Psalm\Type\Atomic\TNamedObject;
2319
use Psalm\Type\Union;
@@ -27,17 +23,12 @@ class DoctrineRepositoryHandler implements AfterMethodCallAnalysisInterface, Aft
2723
/**
2824
* {@inheritdoc}
2925
*/
30-
public static function afterMethodCallAnalysis(
31-
Expr $expr,
32-
string $method_id,
33-
string $appearing_method_id,
34-
string $declaring_method_id,
35-
Context $context,
36-
StatementsSource $statements_source,
37-
Codebase $codebase,
38-
array &$file_replacements = [],
39-
Union &$return_type_candidate = null
40-
): void {
26+
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
27+
{
28+
$expr = $event->getExpr();
29+
$declaring_method_id = $event->getDeclaringMethodId();
30+
$statements_source = $event->getStatementsSource();
31+
4132
if (in_array($declaring_method_id, ['Doctrine\ORM\EntityManagerInterface::getrepository', 'Doctrine\Persistence\ObjectManager::getrepository'])) {
4233
$entityName = $expr->args[0]->value;
4334
if ($entityName instanceof String_) {
@@ -56,21 +47,20 @@ public static function afterMethodCallAnalysis(
5647
EntityAnnotation::class
5748
);
5849
if ($entityAnnotation instanceof EntityAnnotation && $entityAnnotation->repositoryClass) {
59-
$return_type_candidate = new Union([new TNamedObject($entityAnnotation->repositoryClass)]);
50+
$event->setReturnTypeCandidate(new Union([new TNamedObject($entityAnnotation->repositoryClass)]));
6051
}
6152
} catch (\ReflectionException $e) {
6253
}
6354
}
6455
}
6556
}
6657

67-
public static function afterClassLikeVisit(
68-
ClassLike $stmt,
69-
ClassLikeStorage $storage,
70-
FileSource $statements_source,
71-
Codebase $codebase,
72-
array &$file_replacements = []
73-
) {
58+
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
59+
{
60+
$stmt = $event->getStmt();
61+
$statements_source = $event->getStatementsSource();
62+
$codebase = $event->getCodebase();
63+
7464
$docblock = $stmt->getDocComment();
7565
if ($docblock && false !== strpos((string) $docblock, 'repositoryClass')) {
7666
try {

src/Handler/HeaderBagHandler.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use PhpParser\Node\Expr\ConstFetch;
66
use PhpParser\Node\Scalar\String_;
7-
use Psalm\CodeLocation;
8-
use Psalm\Context;
9-
use Psalm\Plugin\Hook\MethodReturnTypeProviderInterface;
10-
use Psalm\StatementsSource;
7+
use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
8+
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
119
use Psalm\Type;
1210
use Psalm\Type\Atomic\TArray;
1311
use Psalm\Type\Atomic\TInt;
@@ -26,17 +24,14 @@ public static function getClassLikeNames(): array
2624
];
2725
}
2826

29-
public static function getMethodReturnType(
30-
StatementsSource $source,
31-
string $fq_classlike_name,
32-
string $method_name_lowercase,
33-
array $call_args,
34-
Context $context,
35-
CodeLocation $code_location,
36-
?array $template_type_parameters = null,
37-
?string $called_fq_classlike_name = null,
38-
?string $called_method_name_lowercase = null
39-
): ?Type\Union {
27+
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
28+
{
29+
$fq_classlike_name = $event->getFqClasslikeName();
30+
$method_name_lowercase = $event->getMethodNameLowercase();
31+
$call_args = $event->getCallArgs();
32+
$source = $event->getSource();
33+
$code_location = $event->getCodeLocation();
34+
4035
if (HeaderBag::class !== $fq_classlike_name) {
4136
return null;
4237
}

0 commit comments

Comments
 (0)