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