1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/src/Psalm/Checker/NamespaceChecker.php

170 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
2016-10-15 06:12:57 +02:00
use PhpParser\Node\Stmt\Namespace_;
2016-02-04 15:22:46 +01:00
use PhpParser;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
use Psalm\StatementsSource;
2016-11-21 03:49:06 +01:00
use Psalm\Type;
2016-11-21 03:49:06 +01:00
class NamespaceChecker extends SourceChecker implements StatementsSource
{
2016-10-15 06:12:57 +02:00
/**
* @var Namespace_
*/
protected $namespace;
2016-10-15 06:12:57 +02:00
/**
* @var string
*/
protected $namespace_name;
2016-10-15 06:12:57 +02:00
/**
2016-11-21 03:49:06 +01:00
* A lookup table for public namespace constants
*
* @var array<string, array<string, Type\Union>>
*/
2016-11-21 03:49:06 +01:00
protected static $public_namespace_constants = [];
2016-11-02 07:29:00 +01:00
/**
* @param Namespace_ $namespace
* @param StatementsSource $source
*/
2016-10-15 06:12:57 +02:00
public function __construct(Namespace_ $namespace, StatementsSource $source)
{
$this->namespace = $namespace;
2016-10-15 06:12:57 +02:00
$this->namespace_name = $this->namespace->name ? implode('\\', $this->namespace->name->parts) : '';
$this->file_name = $source->getFileName();
$this->file_path = $source->getFilePath();
2016-08-25 01:00:44 +02:00
$this->include_file_name = $source->getIncludeFileName();
$this->include_file_path = $source->getIncludeFilePath();
$this->suppressed_issues = $source->getSuppressedIssues();
}
2016-11-02 07:29:00 +01:00
/**
* @param bool $check_classes
* @param bool $check_class_statements
2016-11-13 17:24:46 +01:00
* @param bool $update_docblocks
2016-11-13 00:51:48 +01:00
* @return void
2016-11-02 07:29:00 +01:00
*/
2016-11-13 00:51:48 +01:00
public function check($check_classes = true, $check_class_statements = true, $update_docblocks = false)
{
$leftover_stmts = [];
2016-11-21 03:49:06 +01:00
self::$public_namespace_constants[$this->namespace_name] = [];
foreach ($this->namespace->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassLike) {
2016-11-21 03:49:06 +01:00
$this->visitClassLike($stmt, $check_classes, $check_class_statements, $update_docblocks);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
2016-11-21 03:49:06 +01:00
$this->visitUse($stmt);
2016-11-21 04:40:19 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\GroupUse) {
$this->visitGroupUse($stmt);
2016-11-21 03:49:06 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
foreach ($stmt->consts as $const) {
self::$public_namespace_constants[$this->namespace_name][$const->name] = Type::getMixed();
}
2016-11-21 03:49:06 +01:00
$leftover_stmts[] = $stmt;
2016-11-02 07:29:00 +01:00
} else {
$leftover_stmts[] = $stmt;
}
}
if ($leftover_stmts) {
$statments_checker = new StatementsChecker($this);
$context = new Context($this->file_name);
$statments_checker->check($leftover_stmts, $context);
}
}
2016-11-21 04:02:26 +01:00
/**
* @param PhpParser\Node\Stmt\ClassLike $stmt
* @param bool $check_classes
* @param bool $check_class_statements
* @param bool $update_docblocks
* @return void
*/
2016-11-21 03:49:06 +01:00
public function visitClassLike(PhpParser\Node\Stmt\ClassLike $stmt, $check_classes, $check_class_statements, $update_docblocks)
{
if (!$stmt->name) {
throw new \UnexpectedValueException('Did not expect anonymous class here');
}
2016-12-14 18:28:38 +01:00
2016-11-21 03:49:06 +01:00
$fq_class_name = ClassLikeChecker::getFQCLNFromString($stmt->name, $this->namespace_name, []);
2016-11-21 03:49:06 +01:00
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
2016-12-17 06:48:31 +01:00
$this->declared_classes[$fq_class_name] = true;
2016-11-21 03:49:06 +01:00
if ($check_classes) {
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
?: new ClassChecker($stmt, $this, $fq_class_name);
2016-11-13 00:51:48 +01:00
2016-11-21 03:49:06 +01:00
$class_checker->check($check_class_statements, null, $update_docblocks);
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
if ($check_classes) {
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name)
?: new InterfaceChecker($stmt, $this, $fq_class_name);
2016-12-17 06:48:31 +01:00
$this->declared_classes[$class_checker->getFQCLN()] = true;
2016-11-21 03:49:06 +01:00
$class_checker->check(false);
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
if ($check_classes) {
// register the trait checker
ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
?: new TraitChecker($stmt, $this, $fq_class_name);
}
}
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
2016-11-21 03:49:06 +01:00
public function getNamespace()
{
2016-11-21 03:49:06 +01:00
return $this->namespace_name;
}
2016-11-02 07:29:00 +01:00
/**
2016-11-21 03:49:06 +01:00
* Get a list of suppressed issues
*
* @return array<string>
2016-11-02 07:29:00 +01:00
*/
2016-11-21 03:49:06 +01:00
public function getSuppressedIssues()
2016-08-25 01:00:44 +02:00
{
2016-11-21 03:49:06 +01:00
return $this->suppressed_issues;
2016-08-25 01:00:44 +02:00
}
/**
2016-11-21 03:49:06 +01:00
* @param string $const_name
* @param Type\Union $const_type
2016-11-02 07:29:00 +01:00
* @return void
*/
2016-11-21 03:49:06 +01:00
public function setConstType($const_name, Type\Union $const_type)
2016-08-25 01:00:44 +02:00
{
2016-11-21 03:49:06 +01:00
self::$public_namespace_constants[$this->namespace_name][$const_name] = $const_type;
2016-08-25 01:00:44 +02:00
}
2016-11-02 07:29:00 +01:00
/**
2016-11-21 03:49:06 +01:00
* @param string $namespace_name
* @param mixed $visibility
* @return array<string,Type\Union>
2016-11-02 07:29:00 +01:00
*/
2016-11-21 03:49:06 +01:00
public static function getConstantsForNamespace($namespace_name, $visibility)
2016-08-25 01:00:44 +02:00
{
2016-11-21 03:49:06 +01:00
// remove for PHP 7.1 support
$visibility = \ReflectionProperty::IS_PUBLIC;
2016-08-25 01:00:44 +02:00
2016-11-21 03:49:06 +01:00
// @todo this does not allow for loading in namespace constants not already defined in the current sweep
2016-11-21 04:40:19 +01:00
if (!isset(self::$public_namespace_constants[$namespace_name])) {
self::$public_namespace_constants[$namespace_name] = [];
}
2016-11-21 03:49:06 +01:00
if ($visibility === \ReflectionProperty::IS_PUBLIC) {
return self::$public_namespace_constants[$namespace_name];
}
2016-11-21 03:49:06 +01:00
throw new \InvalidArgumentException('Given $visibility not supported');
}
}