From b2ccf2487e79df50c6b43bcf69e882789befa2dc Mon Sep 17 00:00:00 2001 From: Jack Worman Date: Tue, 20 Dec 2022 17:18:50 -0600 Subject: [PATCH] Add Codebase to remaining events --- src/Psalm/IssueBuffer.php | 10 +++++----- .../Plugin/EventHandler/Event/BeforeAddIssueEvent.php | 11 +++++++++-- .../EventHandler/Event/StringInterpreterEvent.php | 11 ++++++++++- src/Psalm/Type.php | 3 ++- tests/StubTest.php | 4 ++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Psalm/IssueBuffer.php b/src/Psalm/IssueBuffer.php index 02c99b0885f..4d55976650f 100644 --- a/src/Psalm/IssueBuffer.php +++ b/src/Psalm/IssueBuffer.php @@ -254,24 +254,24 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = []) public static function add(CodeIssue $e, bool $is_fixable = false): bool { $config = Config::getInstance(); + $project_analyzer = ProjectAnalyzer::getInstance(); + $codebase = $project_analyzer->getCodebase(); - $event = new BeforeAddIssueEvent($e, $is_fixable); + $event = new BeforeAddIssueEvent($e, $is_fixable, $codebase); if ($config->eventDispatcher->dispatchBeforeAddIssue($event) === false) { return false; - }; + } $fqcn_parts = explode('\\', get_class($e)); $issue_type = array_pop($fqcn_parts); - $project_analyzer = ProjectAnalyzer::getInstance(); - if (!$project_analyzer->show_issues) { return false; } $is_tainted = strpos($issue_type, 'Tainted') === 0; - if ($project_analyzer->getCodebase()->taint_flow_graph && !$is_tainted) { + if ($codebase->taint_flow_graph && !$is_tainted) { return false; } diff --git a/src/Psalm/Plugin/EventHandler/Event/BeforeAddIssueEvent.php b/src/Psalm/Plugin/EventHandler/Event/BeforeAddIssueEvent.php index 190e3907fd7..9e354ed900d 100644 --- a/src/Psalm/Plugin/EventHandler/Event/BeforeAddIssueEvent.php +++ b/src/Psalm/Plugin/EventHandler/Event/BeforeAddIssueEvent.php @@ -4,19 +4,21 @@ namespace Psalm\Plugin\EventHandler\Event; +use Psalm\Codebase; use Psalm\Issue\CodeIssue; final class BeforeAddIssueEvent { private CodeIssue $issue; - private bool $fixable; + private Codebase $codebase; /** @internal */ - public function __construct(CodeIssue $issue, bool $fixable) + public function __construct(CodeIssue $issue, bool $fixable, Codebase $codebase) { $this->issue = $issue; $this->fixable = $fixable; + $this->codebase = $codebase; } public function getIssue(): CodeIssue @@ -28,4 +30,9 @@ public function isFixable(): bool { return $this->fixable; } + + public function getCodebase(): Codebase + { + return $this->codebase; + } } diff --git a/src/Psalm/Plugin/EventHandler/Event/StringInterpreterEvent.php b/src/Psalm/Plugin/EventHandler/Event/StringInterpreterEvent.php index d027fdc4547..8d612bf721c 100644 --- a/src/Psalm/Plugin/EventHandler/Event/StringInterpreterEvent.php +++ b/src/Psalm/Plugin/EventHandler/Event/StringInterpreterEvent.php @@ -2,9 +2,12 @@ namespace Psalm\Plugin\EventHandler\Event; +use Psalm\Codebase; + final class StringInterpreterEvent { private string $value; + private Codebase $codebase; /** * Called after a statement has been checked @@ -12,13 +15,19 @@ final class StringInterpreterEvent * @psalm-external-mutation-free * @internal */ - public function __construct(string $value) + public function __construct(string $value, Codebase $codebase) { $this->value = $value; + $this->codebase = $codebase; } public function getValue(): string { return $this->value; } + + public function getCodebase(): Codebase + { + return $this->codebase; + } } diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index 74d6ae45a90..3fdced8774f 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use LogicException; +use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\Type\Comparator\AtomicTypeComparator; use Psalm\Internal\Type\Comparator\UnionTypeComparator; use Psalm\Internal\Type\TypeCombiner; @@ -256,7 +257,7 @@ public static function getString(?string $value = null): Union if ($value !== null) { $config = Config::getInstance(); - $event = new StringInterpreterEvent($value); + $event = new StringInterpreterEvent($value, ProjectAnalyzer::getInstance()->getCodebase()); $type = $config->eventDispatcher->dispatchStringInterpreter($event); diff --git a/tests/StubTest.php b/tests/StubTest.php index 14fb4dad1b7..2c040b357af 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -418,9 +418,9 @@ function bar(array $a) {} '$c2===' => "'hello'", '$d1===' => "5", - '$d2===' => "'hello'" + '$d2===' => "'hello'", ], - $context + $context, ); }