1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/examples/plugins/StringChecker.php

80 lines
2.9 KiB
PHP
Raw Normal View History

2018-11-06 03:57:36 +01:00
<?php
2022-12-18 17:15:15 +01:00
2018-11-06 03:57:36 +01:00
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
2018-11-06 03:57:36 +01:00
2022-12-18 17:15:15 +01:00
use function in_array;
use function preg_match;
use function preg_split;
use function strpos;
use function strtolower;
class StringChecker implements AfterExpressionAnalysisInterface
2018-11-06 03:57:36 +01:00
{
/**
* Called after an expression has been checked
*
* @return null|false
*/
2022-12-18 17:15:15 +01:00
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
$expr = $event->getExpr();
$statements_source = $event->getStatementsSource();
$codebase = $event->getCodebase();
2018-11-06 03:57:36 +01:00
if ($expr instanceof PhpParser\Node\Scalar\String_) {
$class_or_class_method = '/^\\\?Psalm(\\\[A-Z][A-Za-z0-9]+)+(::[A-Za-z0-9]+)?$/';
if (strpos($statements_source->getFileName(), 'base/DefinitionManager.php') === false
&& strpos($expr->value, 'TestController') === false
&& preg_match($class_or_class_method, $expr->value)
) {
$absolute_class = preg_split('/[:]/', $expr->value)[0];
2022-12-17 05:00:34 +01:00
IssueBuffer::maybeAdd(
new InvalidClass(
2018-11-06 03:57:36 +01:00
'Use ::class constants when representing class names',
new CodeLocation($statements_source, $expr),
2022-12-18 17:15:15 +01:00
$absolute_class,
2018-11-06 03:57:36 +01:00
),
2022-12-18 17:15:15 +01:00
$statements_source->getSuppressedIssues(),
2022-12-17 05:00:34 +01:00
);
2018-11-06 03:57:36 +01:00
}
} elseif ($expr instanceof PhpParser\Node\Expr\BinaryOp\Concat
&& $expr->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $expr->left->class instanceof PhpParser\Node\Name
&& $expr->left->name instanceof PhpParser\Node\Identifier
&& strtolower($expr->left->name->name) === 'class'
&& !in_array(strtolower($expr->left->class->parts[0]), ['self', 'static', 'parent'])
&& $expr->right instanceof PhpParser\Node\Scalar\String_
&& preg_match('/^::[A-Za-z0-9]+$/', $expr->right->value)
) {
$method_id = ((string) $expr->left->class->getAttribute('resolvedName')) . $expr->right->value;
$appearing_method_id = $codebase->getAppearingMethodId($method_id);
if (!$appearing_method_id) {
if (IssueBuffer::accepts(
new UndefinedMethod(
2018-11-06 03:57:36 +01:00
'Method ' . $method_id . ' does not exist',
new CodeLocation($statements_source, $expr),
2022-12-18 17:15:15 +01:00
$method_id,
2018-11-06 03:57:36 +01:00
),
2022-12-18 17:15:15 +01:00
$statements_source->getSuppressedIssues(),
2018-11-06 03:57:36 +01:00
)) {
return false;
}
return null;
2018-11-06 03:57:36 +01:00
}
}
return null;
2018-11-06 03:57:36 +01:00
}
}