2016-11-21 21:32:51 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Example\Plugin;
|
|
|
|
|
|
|
|
use Psalm\Checker;
|
2017-02-09 22:08:53 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
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.
|
|
|
|
*
|
2017-01-17 01:06:29 +01:00
|
|
|
* You will need to add `"nikic/PHP-Parser": ">=3.0.2"` to your
|
2016-12-12 22:07:45 +01:00
|
|
|
* 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
|
|
|
|
*
|
2017-02-09 22:08:53 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
2016-12-12 22:07:45 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2016-12-04 01:11:30 +01:00
|
|
|
public function checkExpression(
|
2017-02-09 22:08:53 +01:00
|
|
|
StatementsChecker $statements_checker,
|
2016-12-12 22:07:45 +01:00
|
|
|
\PhpParser\Node\Expr $stmt,
|
2016-12-04 01:11:30 +01:00
|
|
|
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];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2016-11-21 21:32:51 +01:00
|
|
|
if (Checker\ClassChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-11-21 21:32:51 +01:00
|
|
|
$fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
$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(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-11-21 21:32:51 +01:00
|
|
|
$stmt->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location,
|
|
|
|
$suppressed_issues
|
|
|
|
)
|
2016-11-21 21:32:51 +01:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new StringChecker;
|