Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce BeforeExpressionAnalysisEvent #9725

Merged
merged 2 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 62 additions & 15 deletions src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Psalm\Issue\UnsupportedReferenceUsage;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeExpressionAnalysisEvent;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
use Psalm\Type\TaintKind;
Expand Down Expand Up @@ -80,6 +81,10 @@ public static function analyze(
?TemplateResult $template_result = null,
bool $assigned_to_reference = false
): bool {
if (self::dispatchBeforeExpressionAnalysis($stmt, $context, $statements_analyzer) === false) {
return false;
}

$codebase = $statements_analyzer->getCodebase();

if (self::handleExpression(
Expand Down Expand Up @@ -126,24 +131,10 @@ public static function analyze(
}
}

$event = new AfterExpressionAnalysisEvent(
$stmt,
$context,
$statements_analyzer,
$codebase,
[],
);

if ($codebase->config->eventDispatcher->dispatchAfterExpressionAnalysis($event) === false) {
if (self::dispatchAfterExpressionAnalysis($stmt, $context, $statements_analyzer) === false) {
return false;
}

$file_manipulations = $event->getFileReplacements();

if ($file_manipulations) {
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
}

return true;
}

Expand Down Expand Up @@ -554,4 +545,60 @@ private static function analyzeAssignment(

return true;
}

private static function dispatchBeforeExpressionAnalysis(
PhpParser\Node\Expr $expr,
Context $context,
StatementsAnalyzer $statements_analyzer
): ?bool {
$codebase = $statements_analyzer->getCodebase();

$event = new BeforeExpressionAnalysisEvent(
$expr,
$context,
$statements_analyzer,
$codebase,
[],
);

if ($codebase->config->eventDispatcher->dispatchBeforeExpressionAnalysis($event) === false) {
return false;
}

$file_manipulations = $event->getFileReplacements();

if (!empty($file_manipulations)) {
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
}

return null;
}

private static function dispatchAfterExpressionAnalysis(
PhpParser\Node\Expr $expr,
Context $context,
StatementsAnalyzer $statements_analyzer
): ?bool {
$codebase = $statements_analyzer->getCodebase();

$event = new AfterExpressionAnalysisEvent(
$expr,
$context,
$statements_analyzer,
$codebase,
[],
);

if ($codebase->config->eventDispatcher->dispatchAfterExpressionAnalysis($event) === false) {
return false;
}

$file_manipulations = $event->getFileReplacements();

if (!empty($file_manipulations)) {
tuqqu marked this conversation as resolved.
Show resolved Hide resolved
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
}

return null;
}
}
24 changes: 24 additions & 0 deletions src/Psalm/Internal/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\BeforeAddIssueInterface;
use Psalm\Plugin\EventHandler\BeforeExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\BeforeFileAnalysisInterface;
use Psalm\Plugin\EventHandler\BeforeStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
Expand All @@ -32,6 +33,7 @@
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterStatementAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeAddIssueEvent;
use Psalm\Plugin\EventHandler\Event\BeforeExpressionAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeFileAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeStatementAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\StringInterpreterEvent;
Expand Down Expand Up @@ -77,6 +79,13 @@ class EventDispatcher
*/
public array $after_every_function_checks = [];

/**
* Static methods to be called before expression checks are completed
*
* @var list<class-string<BeforeExpressionAnalysisInterface>>
*/
public array $before_expression_checks = [];

/**
* Static methods to be called after expression checks have completed
*
Expand Down Expand Up @@ -197,6 +206,10 @@ public function registerClass(string $class): void
$this->after_every_function_checks[] = $class;
}

if (is_subclass_of($class, BeforeExpressionAnalysisInterface::class)) {
$this->before_expression_checks[] = $class;
}

if (is_subclass_of($class, AfterExpressionAnalysisInterface::class)) {
$this->after_expression_checks[] = $class;
}
Expand Down Expand Up @@ -284,6 +297,17 @@ public function dispatchAfterEveryFunctionCallAnalysis(AfterEveryFunctionCallAna
}
}

public function dispatchBeforeExpressionAnalysis(BeforeExpressionAnalysisEvent $event): ?bool
{
foreach ($this->before_expression_checks as $handler) {
if ($handler::beforeExpressionAnalysis($event) === false) {
return false;
}
}

return null;
}

public function dispatchAfterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
foreach ($this->after_expression_checks as $handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Psalm\Plugin\EventHandler;

use Psalm\Plugin\EventHandler\Event\BeforeExpressionAnalysisEvent;

interface BeforeExpressionAnalysisInterface
{
/**
* Called before an expression is checked
*/
public static function beforeExpressionAnalysis(BeforeExpressionAnalysisEvent $event): ?bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Psalm\Plugin\EventHandler\Event;

use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\StatementsSource;

final class BeforeExpressionAnalysisEvent
{
private Expr $expr;
private Context $context;
private StatementsSource $statements_source;
private Codebase $codebase;
/**
* @var list<FileManipulation>
*/
private array $file_replacements;

/**
* Called before an expression is checked
*
* @param list<FileManipulation> $file_replacements
* @internal
*/
public function __construct(
Expr $expr,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array $file_replacements = []
) {
$this->expr = $expr;
$this->context = $context;
$this->statements_source = $statements_source;
$this->codebase = $codebase;
$this->file_replacements = $file_replacements;
}

public function getExpr(): Expr
{
return $this->expr;
}

public function getContext(): Context
{
return $this->context;
}

public function getStatementsSource(): StatementsSource
{
return $this->statements_source;
}

public function getCodebase(): Codebase
{
return $this->codebase;
}

/**
* @return list<FileManipulation>
*/
public function getFileReplacements(): array
{
return $this->file_replacements;
}

/**
* @param list<FileManipulation> $file_replacements
*/
public function setFileReplacements(array $file_replacements): void
{
$this->file_replacements = $file_replacements;
}
}