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

64 lines
1.8 KiB
PHP
Raw Normal View History

2016-11-21 21:32:51 +01:00
<?php
namespace Psalm\Example\Plugin;
use Psalm\Checker;
2016-11-21 21:51:38 +01:00
use Psalm\Context;
2016-12-12 22:07:45 +01:00
use Psalm\CodeLocation;
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.
*
* You will need to add `"nikic/PHP-Parser": ">=3.0.2"` to your
* composer.json.
2016-11-21 21:39:04 +01:00
*/
2016-11-21 21:32:51 +01:00
class StringChecker extends \Psalm\Plugin
{
/**
2016-12-12 22:07:45 +01:00
* Checks an expression
*
* @param \PhpParser\Node\Expr $stmt
* @param Context $context
* @param CodeLocation $code_location
* @param array<string> $suppressed_issues
2016-11-21 21:32:51 +01:00
* @return null|false
*/
public function checkExpression(
2016-12-12 22:07:45 +01:00
\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_) {
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];
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;