blob: 1963868393cb705f44021da768079954c797c086 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?php
declare(strict_types=1);
namespace Webmozart\Assert;
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\PluginRegistrationSocket;
use SimpleXMLElement;
final class PsalmPlugin implements PluginEntryPointInterface, AfterMethodCallAnalysisInterface
{
public function __invoke(PluginRegistrationSocket $registration, ?SimpleXMLElement $config = null): void
{
$registration->registerHooksFromClass(self::class);
}
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
[$class, $method] = explode('::', $event->getAppearingMethodId());
if ($class !== Assert::class) {
return;
}
if (!isset(HasAssert::HAS_ASSERT[$method])) {
return;
}
$firstArg = $event->getExpr()->getArgs()[0] ?? null;
if ($firstArg === null) {
return;
}
$varId = ExpressionIdentifier::getExtendedVarId(
$firstArg->value,
$event->getContext()->self,
$event->getStatementsSource()
);
if ($varId === null || !isset($event->getContext()->vars_in_scope[$varId])) {
return;
}
$candidateType = $event->getContext()->vars_in_scope[$varId];
$event->setReturnTypeCandidate($candidateType);
}
}
|