1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/examples/StringChecker.php

59 lines
1.7 KiB
PHP
Raw Normal View History

2016-11-21 21:32:51 +01:00
<?php
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\Checker;
2016-11-21 21:51:38 +01:00
use Psalm\Context;
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
* and, if so, checks that those classes exist
*/
2016-11-21 21:32:51 +01:00
class StringChecker extends \Psalm\Plugin
{
/**
* checks an expression
* @param PhpParser\Node\Expr $stmt
2016-11-21 21:51:38 +01:00
* @param Context $context
* @param CodeLocation $file_name
2016-11-21 21:51:38 +01:00
* @param array<string> $suppressed_issues
2016-11-21 21:32:51 +01:00
* @return null|false
*/
public function checkExpression(
PhpParser\Node\Expr $stmt,
Context $context,
CodeLocation $code_location,
array $suppressed_issues
) {
2016-11-21 21:32:51 +01:00
if ($stmt instanceof \PhpParser\Node\Scalar\String_) {
$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];
if (Checker\ClassChecker::checkFullyQualifiedClassLikeName(
$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(
$stmt->value,
$code_location,
$suppressed_issues
)
2016-11-21 21:32:51 +01:00
) {
return false;
}
}
}
}
}
}
return new StringChecker;