1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00
psalm/examples/TemplateScanner.php

69 lines
2.0 KiB
PHP
Raw Normal View History

2018-01-21 12:44:46 -05:00
<?php
namespace Psalm\Examples\Template;
use PhpParser;
use Psalm;
use Psalm\Checker\CommentChecker;
use Psalm\Codebase;
2018-11-05 21:57:36 -05:00
use Psalm\DocComment;
2018-01-21 12:44:46 -05:00
use Psalm\Storage\FileStorage;
2018-11-05 21:57:36 -05:00
class TemplateScanner extends Psalm\Internal\Scanner\FileScanner
2018-01-21 12:44:46 -05:00
{
const VIEW_CLASS = 'Your\\View\\Class';
/**
* @param array<mixed, PhpParser\Node> $stmts
2018-02-19 00:27:39 -05:00
* @param bool $storage_from_cache
* @param bool $debug_output
2018-01-21 12:44:46 -05:00
*
* @return void
*/
public function scan(
Codebase $codebase,
FileStorage $file_storage,
$storage_from_cache = false,
$debug_output = false
) {
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
$debug_output
);
2018-01-21 12:44:46 -05:00
if (empty($stmts)) {
return;
}
$first_stmt = $stmts[0];
if (($first_stmt instanceof PhpParser\Node\Stmt\Nop) && ($doc_comment = $first_stmt->getDocComment())) {
2018-11-05 21:57:36 -05:00
$comment_block = DocComment::parse(trim($doc_comment->getText()));
2018-01-21 12:44:46 -05:00
if (isset($comment_block['specials']['variablesfrom'])) {
$variables_from = trim($comment_block['specials']['variablesfrom'][0]);
$first_line_regex = '/([A-Za-z\\\0-9]+::[a-z_A-Z]+)(\s+weak)?/';
$matches = [];
if (!preg_match($first_line_regex, $variables_from, $matches)) {
throw new \InvalidArgumentException('Could not interpret doc comment correctly');
}
/** @psalm-suppress MixedArgument */
2018-01-28 12:29:14 -05:00
list($fq_class_name) = explode('::', $matches[1]);
2018-01-21 12:44:46 -05:00
2018-02-03 18:52:35 -05:00
$codebase->scanner->queueClassLikeForScanning(
2018-01-21 12:44:46 -05:00
$fq_class_name,
$this->file_path,
true
);
}
}
2018-02-03 18:52:35 -05:00
$codebase->scanner->queueClassLikeForScanning(self::VIEW_CLASS, $this->file_path);
2018-01-21 12:44:46 -05:00
parent::scan($codebase, $file_storage, $storage_from_cache, $debug_output);
2018-01-21 12:44:46 -05:00
}
}