Skip to content

Commit

Permalink
Merge pull request #6 from kafkiansky/feature/psalm-5
Browse files Browse the repository at this point in the history
Add support for psalm 5
  • Loading branch information
kafkiansky authored May 26, 2022
2 parents 937b4f3 + 65b04d9 commit 150f5eb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 55 deletions.
46 changes: 18 additions & 28 deletions src/Hooks/PreventContainerUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,27 @@
use Kafkiansky\ServiceLocatorInterrupter\Issues\ContainerUsed;
use PhpParser\Node;
use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\Plugin\Hook\AfterFunctionLikeAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;

final class PreventContainerUsage implements AfterFunctionLikeAnalysisInterface, AfterExpressionAnalysisInterface
{
/**
* @var array of container classes that laravel has.
*/
private static $containerClasses = [
private static array $containerClasses = [
'Illuminate\Container\Container',
'Illuminate\Foundation\Application',
];

/**
* @var array of container interfaces that laravel use.
*/
private static $containerInterfaces = [
private static array $containerInterfaces = [
'Psr\Container\ContainerInterface',
'Illuminate\Contracts\Container\Container',
'Illuminate\Contracts\Foundation\Application',
Expand All @@ -45,13 +43,10 @@ final class PreventContainerUsage 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();

if ($stmt instanceof Node\Stmt\ClassMethod) {
/** @var Node\Param $param */
foreach ($stmt->params as $param) {
Expand All @@ -62,9 +57,9 @@ public static function afterStatementAnalysis(
) {
IssueBuffer::accepts(
new ContainerUsed(
new CodeLocation($statements_source, $param)
new CodeLocation($event->getStatementsSource(), $param)
),
$statements_source->getSuppressedIssues()
$event->getStatementsSource()->getSuppressedIssues()
);
}
}
Expand All @@ -76,23 +71,20 @@ public static function afterStatementAnalysis(
/**
* {@inheritdoc}
*/
public static function afterExpressionAnalysis(
Expr $expr,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
): ?bool {
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
$expr = $event->getExpr();

if ($expr instanceof Expr\StaticCall) {
if ($expr->class->hasAttribute('resolvedName')) {
$classOrInterface = $expr->class->getAttribute('resolvedName');

if (self::isServiceLocatorCall($classOrInterface)) {
IssueBuffer::accepts(
new ContainerUsed(
new CodeLocation($statements_source, $expr)
new CodeLocation($event->getStatementsSource(), $expr)
),
$statements_source->getSuppressedIssues()
$event->getStatementsSource()->getSuppressedIssues()
);
}
}
Expand Down Expand Up @@ -150,8 +142,6 @@ private static function instanceOfContainer(array $parents, array $declaringCont
$isContainer = true;
break;
}

continue;
}

return $isContainer;
Expand Down
21 changes: 8 additions & 13 deletions src/Hooks/PreventFacadeCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@

use Kafkiansky\ServiceLocatorInterrupter\Issues\FacadeCalled;
use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;

final class PreventFacadeCall implements AfterExpressionAnalysisInterface
{
/**
* {@inheritdoc}
*/
public static function afterExpressionAnalysis(
Expr $expr,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
): ?bool {
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
$expr = $event->getExpr();

if ($expr instanceof Expr\StaticCall) {
if (self::isFacadeCall($expr->class->getAttribute('resolvedName'))) {
IssueBuffer::accepts(
new FacadeCalled(
new CodeLocation($statements_source, $expr)
new CodeLocation($event->getStatementsSource(), $expr)
),
$statements_source->getSuppressedIssues()
$event->getStatementsSource()->getSuppressedIssues()
);
}
}
Expand Down
22 changes: 8 additions & 14 deletions src/Hooks/PreventHelpersUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@
namespace Kafkiansky\ServiceLocatorInterrupter\Hooks;

use Kafkiansky\ServiceLocatorInterrupter\Issues\HelperUsed;
use PhpParser\Node\Expr\FuncCall;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterEveryFunctionCallAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Plugin\EventHandler\AfterEveryFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent;

final class PreventHelpersUsage implements AfterEveryFunctionCallAnalysisInterface
{
public static function afterEveryFunctionCallAnalysis(
FuncCall $expr,
string $function_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase
): void {
public static function afterEveryFunctionCallAnalysis(AfterEveryFunctionCallAnalysisEvent $event): void
{
$expr = $event->getExpr();

if (self::isServiceLocatorHelperCall($expr->name->toString())) {
IssueBuffer::accepts(
new HelperUsed(
new CodeLocation($statements_source, $expr)
new CodeLocation($event->getStatementsSource(), $expr)
),
$statements_source->getSuppressedIssues()
$event->getStatementsSource()->getSuppressedIssues()
);
}
}
Expand Down

0 comments on commit 150f5eb

Please sign in to comment.