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

158 lines
4.9 KiB
PHP
Raw Normal View History

2016-10-30 04:07:13 +01:00
<?php
2017-12-06 07:05:51 +01:00
namespace Psalm\Examples\Template;
use PhpParser;
2016-10-30 04:07:13 +01:00
use Psalm;
2018-11-06 03:57:36 +01:00
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2017-12-06 07:05:51 +01:00
use Psalm\CodeLocation;
2016-10-30 04:07:13 +01:00
use Psalm\Context;
2018-11-06 03:57:36 +01:00
use Psalm\DocComment;
2016-10-30 04:07:13 +01:00
use Psalm\Type;
2018-11-06 03:57:36 +01:00
class TemplateAnalyzer extends Psalm\Internal\Analyzer\FileAnalyzer
2016-10-30 04:07:13 +01:00
{
2017-12-06 07:05:51 +01:00
const VIEW_CLASS = 'Your\\View\\Class';
2016-10-30 04:07:13 +01:00
public function analyze(Context $context = null, $update_docblocks = false, Context $global_context = null)
2017-12-06 07:05:51 +01:00
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$stmts = $codebase->getStatementsForFile($this->file_path);
2017-12-06 07:05:51 +01:00
if (empty($stmts)) {
return;
}
$first_stmt = $stmts[0];
$this_params = null;
if (($first_stmt instanceof PhpParser\Node\Stmt\Nop) && ($doc_comment = $first_stmt->getDocComment())) {
2018-11-06 03:57:36 +01:00
$comment_block = DocComment::parse(trim($doc_comment->getText()));
2017-12-06 07:05:51 +01: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-11-06 03:57:36 +01:00
$this_params = $this->checkMethod($matches[1], $first_stmt, $codebase);
2016-10-30 04:07:13 +01:00
if ($this_params === false) {
2017-12-06 07:05:51 +01:00
return;
2016-10-30 04:07:13 +01:00
}
2017-12-06 07:05:51 +01:00
$this_params->vars_in_scope['$this'] = new Type\Union([
2018-01-18 23:41:14 +01:00
new Type\Atomic\TNamedObject(self::VIEW_CLASS),
2017-12-06 07:05:51 +01:00
]);
2016-10-30 04:07:13 +01:00
}
}
if (!$this_params) {
2017-12-06 07:05:51 +01:00
$this_params = new Context();
2016-10-30 04:07:13 +01:00
$this_params->check_variables = false;
2017-12-06 07:05:51 +01:00
$this_params->self = self::VIEW_CLASS;
$this_params->vars_in_scope['$this'] = new Type\Union([
2018-01-18 23:41:14 +01:00
new Type\Atomic\TNamedObject(self::VIEW_CLASS),
2017-12-06 07:05:51 +01:00
]);
2016-10-30 04:07:13 +01:00
}
2018-01-21 18:44:46 +01:00
$this->checkWithViewClass($this_params, $stmts);
2016-10-30 04:07:13 +01:00
}
2016-11-02 07:29:00 +01:00
/**
2017-12-06 07:05:51 +01:00
* @param string $method_id
* @param PhpParser\Node $stmt
2018-01-18 23:41:14 +01:00
*
2017-12-06 07:05:51 +01:00
* @return Context|false
2016-11-02 07:29:00 +01:00
*/
2018-11-06 03:57:36 +01:00
private function checkMethod($method_id, PhpParser\Node $stmt, Codebase $codebase)
2016-10-30 04:07:13 +01:00
{
$class = explode('::', $method_id)[0];
2018-11-06 03:57:36 +01:00
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$this,
2017-12-06 07:05:51 +01:00
$class,
new CodeLocation($this, $stmt),
[],
true
) === false
) {
2016-10-30 04:07:13 +01:00
return false;
}
2017-12-06 07:05:51 +01:00
$this_context = new Context();
2016-10-30 04:07:13 +01:00
$this_context->self = $class;
2017-12-06 07:05:51 +01:00
$this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class)]);
2016-10-30 04:07:13 +01:00
$constructor_id = $class . '::__construct';
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getMethodMutations($constructor_id, $this_context);
2016-10-30 04:07:13 +01:00
2017-12-06 07:05:51 +01:00
$this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class)]);
2016-10-30 04:07:13 +01:00
// check the actual method
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getMethodMutations($method_id, $this_context);
2016-10-30 04:07:13 +01:00
2017-12-06 07:05:51 +01:00
$view_context = new Context();
$view_context->self = self::VIEW_CLASS;
2016-10-30 04:07:13 +01:00
// add all $this-> vars to scope
foreach ($this_context->vars_possibly_in_scope as $var => $_) {
2016-10-30 04:07:13 +01:00
$view_context->vars_in_scope[str_replace('$this->', '$', $var)] = Type::getMixed();
}
foreach ($this_context->vars_in_scope as $var => $type) {
$view_context->vars_in_scope[str_replace('$this->', '$', $var)] = $type;
}
return $view_context;
}
2016-11-02 07:29:00 +01:00
/**
2017-12-06 07:05:51 +01:00
* @param Context $context
2018-01-21 18:44:46 +01:00
* @param array<PhpParser\Node\Stmt> $stmts
2018-01-18 23:41:14 +01:00
*
2017-12-06 07:05:51 +01:00
* @return void
2016-11-02 07:29:00 +01:00
*/
2018-01-21 18:44:46 +01:00
protected function checkWithViewClass(Context $context, array $stmts)
2016-10-30 04:07:13 +01:00
{
2017-12-06 07:05:51 +01:00
$pseudo_method_stmts = [];
2016-11-21 21:51:38 +01:00
2017-12-06 07:05:51 +01:00
foreach ($stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Use_) {
$this->visitUse($stmt);
} else {
$pseudo_method_stmts[] = $stmt;
}
2016-11-21 21:51:38 +01:00
}
2017-12-06 07:05:51 +01:00
$pseudo_method_name = preg_replace('/[^a-zA-Z0-9_]+/', '_', $this->file_name);
2016-10-30 04:07:13 +01:00
2017-12-06 07:05:51 +01:00
$class_method = new PhpParser\Node\Stmt\ClassMethod($pseudo_method_name, ['stmts' => []]);
2016-10-30 04:07:13 +01:00
2017-12-06 07:05:51 +01:00
$class = new PhpParser\Node\Stmt\Class_(self::VIEW_CLASS);
2016-10-30 04:07:13 +01:00
2018-11-11 18:01:14 +01:00
$class_analyzer = new ClassAnalyzer($class, $this, self::VIEW_CLASS);
2017-12-06 07:05:51 +01:00
2018-11-11 18:01:14 +01:00
$view_method_analyzer = new MethodAnalyzer($class_method, $class_analyzer);
2017-12-06 07:05:51 +01:00
if (!$context->check_variables) {
2018-11-11 18:01:14 +01:00
$view_method_analyzer->addSuppressedIssue('UndefinedVariable');
2017-12-06 07:05:51 +01:00
}
2016-10-30 04:07:13 +01:00
2018-11-11 18:01:14 +01:00
$statements_source = new StatementsAnalyzer($view_method_analyzer);
2016-10-30 04:07:13 +01:00
2018-11-06 03:57:36 +01:00
$statements_source->analyze($pseudo_method_stmts, $context);
2016-10-30 04:07:13 +01:00
}
}