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

174 lines
5.6 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;
use Psalm\Storage\MethodStorage;
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 $file_context = null, bool $preserve_analyzers = 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())) {
2020-05-29 04:14:41 +02:00
$comment_block = DocComment::parsePreservingLength($doc_comment);
2017-12-06 07:05:51 +01:00
2020-05-29 04:14:41 +02:00
if (isset($comment_block->tags['variablesfrom'])) {
$variables_from = trim($comment_block->tags['variablesfrom'][0]);
2017-12-06 07:05:51 +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)) {
throw new \InvalidArgumentException('Could not interpret doc comment correctly');
}
$method_id = new \Psalm\Internal\MethodIdentifier(...explode('::', $matches[1]));
$this_params = $this->checkMethod($method_id, $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
/**
* @param \Psalm\Internal\MethodIdentifier $method_id
2017-12-06 07:05:51 +01:00
* @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
*/
private function checkMethod(\Psalm\Internal\MethodIdentifier $method_id, PhpParser\Node $stmt, Codebase $codebase)
2016-10-30 04:07:13 +01:00
{
2018-11-06 03:57:36 +01:00
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$this,
$method_id->fq_class_name,
2017-12-06 07:05:51 +01:00
new CodeLocation($this, $stmt),
null,
null,
2017-12-06 07:05:51 +01:00
[],
true
) === false
) {
2016-10-30 04:07:13 +01:00
return false;
}
2017-12-06 07:05:51 +01:00
$this_context = new Context();
$this_context->self = $method_id->fq_class_name;
$class_storage = $codebase->classlike_storage_provider->get($method_id->fq_class_name);
2016-10-30 04:07:13 +01:00
$this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class_storage->name)]);
2016-10-30 04:07:13 +01:00
$this->project_analyzer->getMethodMutations(
new \Psalm\Internal\MethodIdentifier($method_id->fq_class_name, '__construct'),
$this_context,
$this->getRootFilePath(),
$this->getRootFileName()
);
2016-10-30 04:07:13 +01:00
$this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class_storage->name)]);
2016-10-30 04:07:13 +01:00
// check the actual method
$this->project_analyzer->getMethodMutations(
$method_id,
$this_context,
$this->getRootFilePath(),
$this->getRootFileName()
);
2016-10-30 04:07:13 +01:00
2017-12-06 07:05:51 +01:00
$view_context = new Context();
$view_context->self = strtolower(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
$view_method_analyzer = new MethodAnalyzer($class_method, $class_analyzer, new MethodStorage());
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
$statements_source = new StatementsAnalyzer(
$view_method_analyzer,
new \Psalm\Internal\Provider\NodeDataProvider()
);
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
}
}