2018-01-21 18:44:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Examples\Template;
|
|
|
|
|
2021-12-27 20:18:33 +01:00
|
|
|
use InvalidArgumentException;
|
2018-01-21 18:44:46 +01:00
|
|
|
use PhpParser;
|
|
|
|
use Psalm;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
2018-01-21 19:38:51 +01:00
|
|
|
use Psalm\Codebase;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\DocComment;
|
2019-05-30 16:30:41 +02:00
|
|
|
use Psalm\Progress\Progress;
|
2018-01-21 18:44:46 +01:00
|
|
|
use Psalm\Storage\FileStorage;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
class TemplateScanner extends Psalm\Internal\Scanner\FileScanner
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
const VIEW_CLASS = 'Your\\View\\Class';
|
|
|
|
|
|
|
|
/**
|
2018-02-19 06:27:39 +01:00
|
|
|
* @param bool $storage_from_cache
|
2018-01-21 18:44:46 +01:00
|
|
|
*/
|
2018-02-19 17:53:30 +01:00
|
|
|
public function scan(
|
|
|
|
Codebase $codebase,
|
|
|
|
FileStorage $file_storage,
|
|
|
|
$storage_from_cache = false,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Progress $progress = null
|
2020-10-12 21:02:52 +02:00
|
|
|
): void {
|
2018-02-19 17:53:30 +01:00
|
|
|
$stmts = $codebase->statements_provider->getStatementsForFile(
|
|
|
|
$file_storage->file_path,
|
2021-11-12 02:29:17 +01:00
|
|
|
70400,
|
2019-05-30 16:30:41 +02:00
|
|
|
$progress
|
2018-02-19 17:53:30 +01:00
|
|
|
);
|
|
|
|
|
2020-09-20 00:26:51 +02:00
|
|
|
if ($stmts === []) {
|
2018-01-21 18:44:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$first_stmt = $stmts[0];
|
|
|
|
|
|
|
|
if (($first_stmt instanceof PhpParser\Node\Stmt\Nop) && ($doc_comment = $first_stmt->getDocComment())) {
|
2020-05-29 04:14:41 +02:00
|
|
|
$comment_block = DocComment::parsePreservingLength($doc_comment);
|
2018-01-21 18:44:46 +01:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($comment_block->tags['variablesfrom'])) {
|
|
|
|
$variables_from = trim($comment_block->tags['variablesfrom'][0]);
|
2018-01-21 18:44:46 +01:00
|
|
|
|
|
|
|
$first_line_regex = '/([A-Za-z\\\0-9]+::[a-z_A-Z]+)(\s+weak)?/';
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
if (!preg_match($first_line_regex, $variables_from, $matches)) {
|
2021-12-27 20:18:33 +01:00
|
|
|
throw new InvalidArgumentException('Could not interpret doc comment correctly');
|
2018-01-21 18:44:46 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 06:17:41 +02:00
|
|
|
[$fq_class_name] = explode('::', $matches[1]);
|
2018-01-21 18:44:46 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->scanner->queueClassLikeForScanning(
|
2018-01-21 18:44:46 +01:00
|
|
|
$fq_class_name,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 23:17:55 +02:00
|
|
|
$codebase->scanner->queueClassLikeForScanning(self::VIEW_CLASS);
|
2018-01-21 18:44:46 +01:00
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
parent::scan($codebase, $file_storage, $storage_from_cache, $progress);
|
2018-01-21 18:44:46 +01:00
|
|
|
}
|
|
|
|
}
|