2016-01-20 00:27:06 +01:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
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-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
class NamespaceChecker implements StatementsSource
|
|
|
|
{
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @var Namespace_
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace;
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace_name;
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $declared_classes = [];
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $aliased_classes = [];
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $file_name;
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $include_file_name;
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
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)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->namespace = $namespace;
|
2016-10-15 06:12:57 +02:00
|
|
|
$this->namespace_name = $this->namespace->name ? implode('\\', $this->namespace->name->parts) : '';
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->file_name = $source->getFileName();
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->include_file_name = $source->getIncludeFileName();
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param bool $check_classes
|
|
|
|
* @param bool $check_class_statements
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-16 05:06:03 +02:00
|
|
|
public function check($check_classes = true, $check_class_statements = true)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
foreach ($this->namespace->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassLike) {
|
2016-11-08 01:16:51 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromString($stmt->name, $this->namespace_name, []);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->declared_classes[$fq_class_name] = 1;
|
2016-08-14 00:54:49 +02:00
|
|
|
|
|
|
|
if ($check_classes) {
|
2016-11-07 23:29:51 +01:00
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
|
|
|
|
?: new ClassChecker($stmt, $this, $fq_class_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$class_checker->check($check_class_statements);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
2016-08-14 00:54:49 +02:00
|
|
|
if ($check_classes) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name)
|
2016-11-07 23:29:51 +01:00
|
|
|
?: new InterfaceChecker($stmt, $this, $fq_class_name);
|
2016-11-08 01:16:51 +01:00
|
|
|
$this->declared_classes[] = $class_checker->getFQCLN();
|
2016-08-14 00:54:49 +02:00
|
|
|
$class_checker->check(false);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
2016-08-14 00:54:49 +02:00
|
|
|
if ($check_classes) {
|
|
|
|
// register the trait checker
|
2016-11-07 23:29:51 +01:00
|
|
|
ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
|
|
|
|
?: new TraitChecker($stmt, $this, $fq_class_name);
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
2016-09-10 05:17:56 +02:00
|
|
|
$this->aliased_classes[strtolower($use->alias)] = implode('\\', $use->name->parts);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
2016-08-14 03:14:32 +02:00
|
|
|
$context = new Context($this->file_name);
|
|
|
|
$statments_checker->check($leftover_stmts, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-06-20 06:38:13 +02:00
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getDeclaredClasses()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return array_keys($this->declared_classes);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function containsClass($class_name)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return isset($this->declared_classes[$class_name]);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->namespace_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public function getClassLikeChecker()
|
2016-01-30 00:48:09 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return string|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-04-17 17:22:18 +02:00
|
|
|
public function getParentClass()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getFileName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->file_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
public function getIncludeFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string|null $file_name
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
public function setIncludeFileName($file_name)
|
|
|
|
{
|
|
|
|
$this->include_file_name = $file_name;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name ?: $this->file_name;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-05-16 05:06:03 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* Get a list of suppressed issues
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-07-22 19:29:46 +02:00
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|