Skip to content

Commit

Permalink
Fixed Hook Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
marartner committed Mar 11, 2021
1 parent 54b8fe5 commit fee0f32
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
29 changes: 29 additions & 0 deletions src/Hooks/UsedEmptyHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Marartner\PsalmNoEmpty\Hooks;

use Marartner\PsalmNoEmpty\Issue\UsedEmptyConstruct;
use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\StatementsSource;

final class UsedEmptyHook implements AfterExpressionAnalysisInterface
{
public static function afterExpressionAnalysis(Expr $expr, Context $context, StatementsSource $statements_source, Codebase $codebase, array &$file_replacements = []): ?bool
{
if ($expr instanceof Expr\Empty_) {
IssueBuffer::accepts(
new UsedEmptyConstruct(
'Do not use the empty() construct',
new CodeLocation($statements_source->getSource(), $expr)
),
$statements_source->getSuppressedIssues()
);
}
return null;
}
}
30 changes: 7 additions & 23 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
<?php
<?php declare(strict_types=1);

namespace Marartner\PsalmNoEmpty;

use Marartner\PsalmNoEmpty\Issue\UsedEmptyConstruct;
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 Marartner\PsalmNoEmpty\Hooks\UsedEmptyHook;
use SimpleXMLElement;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use function class_exists;

class Plugin implements PluginEntryPointInterface, AfterExpressionAnalysisInterface
class Plugin implements PluginEntryPointInterface
{
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null): void
{
// TODO: Implement __invoke() method.
}

public static function afterExpressionAnalysis(Expr $expr, Context $context, StatementsSource $statements_source, Codebase $codebase, array &$file_replacements = []): ?bool
{
if ($expr instanceof Expr\Empty_) {
IssueBuffer::accepts(
new UsedEmptyConstruct(
'Do not use the empty() construct',
new CodeLocation($statements_source->getSource(), $expr)
),
$statements_source->getSuppressedIssues()
);
if(class_exists(UsedEmptyHook::class)){
$registration->registerHooksFromClass(UsedEmptyHook::class);
}
}
}

0 comments on commit fee0f32

Please sign in to comment.