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

70 lines
2.2 KiB
PHP
Raw Normal View History

2016-11-21 21:32:51 +01:00
<?php
namespace Psalm\Example\Plugin;
use PhpParser;
2016-11-21 21:32:51 +01:00
use Psalm\Checker;
use Psalm\Checker\StatementsChecker;
2016-12-12 22:07:45 +01:00
use Psalm\CodeLocation;
2018-01-18 23:41:14 +01:00
use Psalm\Context;
use Psalm\FileManipulation\FileManipulation;
2016-11-21 21:32:51 +01:00
2016-11-21 21:39:04 +01:00
/**
* Checks all strings to see if they contain references to classes
2016-12-12 22:07:45 +01:00
* and, if so, checks that those classes exist.
2016-11-21 21:39:04 +01:00
*/
2016-11-21 21:32:51 +01:00
class StringChecker extends \Psalm\Plugin
{
/**
* Called after an expression has been checked
*
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr $stmt
* @param Context $context
* @param CodeLocation $code_location
* @param string[] $suppressed_issues
* @param FileManipulation[] $file_replacements
2016-12-12 22:07:45 +01:00
*
2016-11-21 21:32:51 +01:00
* @return null|false
*/
public static function afterExpressionCheck(
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
Context $context,
CodeLocation $code_location,
array $suppressed_issues,
array &$file_replacements = []
) {
2016-11-21 21:32:51 +01:00
if ($stmt instanceof \PhpParser\Node\Scalar\String_) {
2016-12-12 22:07:45 +01:00
// Replace "Psalm" with your namespace
2016-11-21 21:32:51 +01:00
$class_or_class_method = '/^\\\?Psalm(\\\[A-Z][A-Za-z0-9]+)+(::[A-Za-z0-9]+)?$/';
if (preg_match($class_or_class_method, $stmt->value)) {
$fq_class_name = preg_split('/[:]/', $stmt->value)[0];
$project_checker = $statements_checker->getFileChecker()->project_checker;
2016-11-21 21:32:51 +01:00
if (Checker\ClassChecker::checkFullyQualifiedClassLikeName(
$statements_checker,
2016-11-21 21:32:51 +01:00
$fq_class_name,
$code_location,
$suppressed_issues
) === false
2016-11-21 21:32:51 +01:00
) {
return false;
}
if ($fq_class_name !== $stmt->value) {
if (Checker\MethodChecker::checkMethodExists(
$project_checker,
2016-11-21 21:32:51 +01:00
$stmt->value,
$code_location,
$suppressed_issues
)
2016-11-21 21:32:51 +01:00
) {
return false;
}
}
}
}
}
}