Skip to content

Commit

Permalink
Upgrade Psalm interfaces (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jul 4, 2021
1 parent 74cfb5f commit 2992656
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 194 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^7.1 || ^8.0",
"ext-simplexml": "*",
"symfony/framework-bundle": "^3.0 || ^4.0 || ^5.0",
"vimeo/psalm": "^4.6.1"
"vimeo/psalm": "^4.8"
},
"require-dev": {
"symfony/form": "^4.0 || ^5.0",
Expand Down
12 changes: 6 additions & 6 deletions src/Handler/AnnotationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

use PhpParser\Comment\Doc;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use Psalm\Codebase;
use Psalm\FileSource;
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;

class AnnotationHandler implements AfterClassLikeVisitInterface
{
/**
* {@inheritdoc}
*/
public static function afterClassLikeVisit(ClassLike $stmt, ClassLikeStorage $storage, FileSource $statements_source, Codebase $codebase, array &$file_replacements = [])
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
{
$stmt = $event->getStmt();
$storage = $event->getStorage();

if (!$stmt instanceof Class_) {
return;
}
Expand Down
26 changes: 10 additions & 16 deletions src/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\StatementsSource;
use Psalm\SymfonyPsalmPlugin\Exception\InvalidConsoleModeException;
use Psalm\SymfonyPsalmPlugin\Issue\InvalidConsoleArgumentValue;
Expand Down Expand Up @@ -41,17 +40,12 @@ class ConsoleHandler implements AfterMethodCallAnalysisInterface
/**
* {@inheritdoc}
*/
public static function afterMethodCallAnalysis(
Expr $expr,
string $method_id,
string $appearing_method_id,
string $declaring_method_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
): void {
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
$statements_source = $event->getStatementsSource();
$expr = $event->getExpr();
$declaring_method_id = $event->getDeclaringMethodId();

switch ($declaring_method_id) {
case 'Symfony\Component\Console\Command\Command::addargument':
self::analyseArgument($expr->args, $statements_source);
Expand All @@ -63,7 +57,7 @@ public static function afterMethodCallAnalysis(
}

if (isset(self::$arguments[$identifier])) {
$return_type_candidate = self::$arguments[$identifier];
$event->setReturnTypeCandidate(self::$arguments[$identifier]);
}
break;
case 'Symfony\Component\Console\Command\Command::addoption':
Expand All @@ -76,7 +70,7 @@ public static function afterMethodCallAnalysis(
}

if (isset(self::$options[$identifier])) {
$return_type_candidate = self::$options[$identifier];
$event->setReturnTypeCandidate(self::$options[$identifier]);
}
break;
case 'Symfony\Component\Console\Command\Command::setdefinition':
Expand Down
18 changes: 7 additions & 11 deletions src/Handler/ContainerDependencyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace Psalm\SymfonyPsalmPlugin\Handler;

use PhpParser\Node;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterFunctionLikeAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
use Psalm\SymfonyPsalmPlugin\Issue\ContainerDependency;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -17,13 +15,11 @@ class ContainerDependencyHandler implements AfterFunctionLikeAnalysisInterface
/**
* {@inheritdoc}
*/
public static function afterStatementAnalysis(
Node\FunctionLike $stmt,
FunctionLikeStorage $classlike_storage,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
): ?bool {
public static function afterStatementAnalysis(AfterFunctionLikeAnalysisEvent $event): ?bool
{
$stmt = $event->getStmt();
$statements_source = $event->getStatementsSource();

if ($stmt instanceof Node\Stmt\ClassMethod && '__construct' === $stmt->name->name) {
foreach ($stmt->params as $param) {
if ($param->type instanceof Node\Name && ContainerInterface::class === $param->type->getAttribute('resolvedName')) {
Expand Down
50 changes: 22 additions & 28 deletions src/Handler/ContainerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileSource;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Storage\FileStorage;
use Psalm\SymfonyPsalmPlugin\Issue\NamingConventionViolation;
use Psalm\SymfonyPsalmPlugin\Issue\PrivateService;
Expand Down Expand Up @@ -50,17 +47,14 @@ public static function init(ContainerMeta $containerMeta): void
/**
* {@inheritdoc}
*/
public static function afterMethodCallAnalysis(
Expr $expr,
string $method_id,
string $appearing_method_id,
string $declaring_method_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
): void {
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
$declaring_method_id = $event->getDeclaringMethodId();
$statements_source = $event->getStatementsSource();
$expr = $event->getExpr();
$codebase = $event->getCodebase();
$context = $event->getContext();

if (!self::isContainerMethod($declaring_method_id, 'get')) {
if (self::isContainerMethod($declaring_method_id, 'getparameter')) {
$argument = $expr->args[0]->value;
Expand All @@ -76,10 +70,10 @@ public static function afterMethodCallAnalysis(
}

if (!self::$containerMeta) {
if ($return_type_candidate && $expr->args[0]->value instanceof ClassConstFetch) {
if ($event->getReturnTypeCandidate() && $expr->args[0]->value instanceof ClassConstFetch) {
$className = (string) $expr->args[0]->value->class->getAttribute('resolvedName');
if (!in_array($className, ['self', 'parent', 'static'])) {
$return_type_candidate = new Union([new TNamedObject($className)]);
$event->setReturnTypeCandidate(new Union([new TNamedObject($className)]));
}
}

Expand All @@ -106,7 +100,7 @@ public static function afterMethodCallAnalysis(
$class = $service->getClassName();
if ($class) {
$codebase->classlikes->addFullyQualifiedClassName($class);
$return_type_candidate = new Union([new TNamedObject($class)]);
$event->setReturnTypeCandidate(new Union([new TNamedObject($class)]));
}

if (!$service->isPublic()) {
Expand All @@ -129,13 +123,13 @@ public static function afterMethodCallAnalysis(
/**
* {@inheritdoc}
*/
public static function afterClassLikeVisit(
ClassLike $stmt,
ClassLikeStorage $storage,
FileSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
{
$codebase = $event->getCodebase();
$statements_source = $event->getStatementsSource();
$storage = $event->getStorage();
$stmt = $event->getStmt();

$fileStorage = $codebase->file_storage_provider->get($statements_source->getFilePath());

if (\in_array($storage->name, ContainerHandler::GET_CLASSLIKES)) {
Expand Down
24 changes: 9 additions & 15 deletions src/Handler/DoctrineQueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,25 @@
namespace Psalm\SymfonyPsalmPlugin\Handler;

use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\SymfonyPsalmPlugin\Issue\QueryBuilderSetParameter;
use Psalm\Type\Union;

class DoctrineQueryBuilderHandler implements AfterMethodCallAnalysisInterface
{
/**
* {@inheritdoc}
*/
public static function afterMethodCallAnalysis(
Expr $expr,
string $method_id,
string $appearing_method_id,
string $declaring_method_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
): void {
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
$expr = $event->getExpr();
$declaring_method_id = $event->getDeclaringMethodId();
$statements_source = $event->getStatementsSource();
$context = $event->getContext();

if ('Doctrine\ORM\QueryBuilder::setparameter' === $declaring_method_id) {
if (isset($expr->args[2])) {
return;
Expand Down
44 changes: 17 additions & 27 deletions src/Handler/DoctrineRepositoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
use Doctrine\ORM\Mapping\Entity as EntityAnnotation;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassLike;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\DocComment;
use Psalm\Exception\DocblockParseException;
use Psalm\FileSource;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\SymfonyPsalmPlugin\Issue\RepositoryStringShortcut;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
Expand All @@ -27,17 +23,12 @@ class DoctrineRepositoryHandler implements AfterMethodCallAnalysisInterface, Aft
/**
* {@inheritdoc}
*/
public static function afterMethodCallAnalysis(
Expr $expr,
string $method_id,
string $appearing_method_id,
string $declaring_method_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
): void {
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
$expr = $event->getExpr();
$declaring_method_id = $event->getDeclaringMethodId();
$statements_source = $event->getStatementsSource();

if (in_array($declaring_method_id, ['Doctrine\ORM\EntityManagerInterface::getrepository', 'Doctrine\Persistence\ObjectManager::getrepository'])) {
$entityName = $expr->args[0]->value;
if ($entityName instanceof String_) {
Expand All @@ -56,21 +47,20 @@ public static function afterMethodCallAnalysis(
EntityAnnotation::class
);
if ($entityAnnotation instanceof EntityAnnotation && $entityAnnotation->repositoryClass) {
$return_type_candidate = new Union([new TNamedObject($entityAnnotation->repositoryClass)]);
$event->setReturnTypeCandidate(new Union([new TNamedObject($entityAnnotation->repositoryClass)]));
}
} catch (\ReflectionException $e) {
}
}
}
}

public static function afterClassLikeVisit(
ClassLike $stmt,
ClassLikeStorage $storage,
FileSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
{
$stmt = $event->getStmt();
$statements_source = $event->getStatementsSource();
$codebase = $event->getCodebase();

$docblock = $stmt->getDocComment();
if ($docblock && false !== strpos((string) $docblock, 'repositoryClass')) {
try {
Expand Down
25 changes: 10 additions & 15 deletions src/Handler/HeaderBagHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\String_;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Plugin\Hook\MethodReturnTypeProviderInterface;
use Psalm\StatementsSource;
use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TInt;
Expand All @@ -26,17 +24,14 @@ public static function getClassLikeNames(): array
];
}

public static function getMethodReturnType(
StatementsSource $source,
string $fq_classlike_name,
string $method_name_lowercase,
array $call_args,
Context $context,
CodeLocation $code_location,
?array $template_type_parameters = null,
?string $called_fq_classlike_name = null,
?string $called_method_name_lowercase = null
): ?Type\Union {
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
{
$fq_classlike_name = $event->getFqClasslikeName();
$method_name_lowercase = $event->getMethodNameLowercase();
$call_args = $event->getCallArgs();
$source = $event->getSource();
$code_location = $event->getCodeLocation();

if (HeaderBag::class !== $fq_classlike_name) {
return null;
}
Expand Down
Loading

0 comments on commit 2992656

Please sign in to comment.