2016-05-16 05:06:03 +02:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Context;
|
2016-12-30 21:53:35 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
|
|
|
class TraitChecker extends ClassLikeChecker
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2016-11-01 05:39:41 +01:00
|
|
|
protected $method_map = [];
|
2016-08-15 20:20:06 +02:00
|
|
|
|
2016-12-12 19:50:46 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected static $trait_names = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
|
|
|
protected static $existing_traits = [];
|
|
|
|
|
2016-12-29 03:37:24 +01:00
|
|
|
/**
|
|
|
|
* @var array<MethodChecker>
|
|
|
|
*/
|
|
|
|
protected $methods = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-11-05 02:14:04 +01:00
|
|
|
* @param PhpParser\Node\Stmt\ClassLike $class
|
|
|
|
* @param StatementsSource $source
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-11-05 02:14:04 +01:00
|
|
|
if (!$class instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
throw new \InvalidArgumentException('Trait checker must be passed a trait');
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->class = $class;
|
|
|
|
$this->namespace = $source->getNamespace();
|
|
|
|
$this->aliased_classes = $source->getAliasedClasses();
|
|
|
|
$this->file_name = $source->getFileName();
|
2016-12-04 01:11:30 +01:00
|
|
|
$this->file_path = $source->getFilePath();
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name = $fq_class_name;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->parent_class = null;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-12-30 21:53:35 +01:00
|
|
|
if (!isset(self::$storage[$fq_class_name])) {
|
|
|
|
self::$storage[$fq_class_name] = new ClassLikeStorage();
|
|
|
|
self::$storage[$fq_class_name]->file_name = $this->file_name;
|
|
|
|
self::$storage[$fq_class_name]->file_path = $this->file_path;
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:50:46 +01:00
|
|
|
self::$trait_names[strtolower($this->fq_class_name)] = $this->fq_class_name;
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
self::$class_checkers[$fq_class_name] = $this;
|
2016-08-08 20:36:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param bool $check_methods
|
|
|
|
* @param Context|null $class_context
|
2016-11-21 19:37:27 +01:00
|
|
|
* @param bool $update_docblocks
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-13 00:51:48 +01:00
|
|
|
public function check($check_methods = true, Context $class_context = null, $update_docblocks = false)
|
2016-08-08 20:36:18 +02:00
|
|
|
{
|
|
|
|
if (!$class_context) {
|
|
|
|
throw new \InvalidArgumentException('TraitChecker::check must be called with a $class_context');
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-12-29 03:37:24 +01:00
|
|
|
parent::check(false, $class_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Context $class_context
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function checkMethods(Context $class_context)
|
|
|
|
{
|
|
|
|
foreach ($this->methods as $method_checker) {
|
|
|
|
$method_checker->check($class_context, null);
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-15 20:20:06 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 07:07:44 +01:00
|
|
|
* @param array<string, string> $method_map
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-15 20:20:06 +02:00
|
|
|
public function setMethodMap(array $method_map)
|
|
|
|
{
|
|
|
|
$this->method_map = $method_map;
|
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-15 20:20:06 +02:00
|
|
|
protected function getMappedMethodName($method_name)
|
|
|
|
{
|
|
|
|
if (isset($this->method_map[$method_name])) {
|
|
|
|
return $this->method_map[$method_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $method_name;
|
|
|
|
}
|
2016-10-30 17:46:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $trait_name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function traitExists($trait_name)
|
|
|
|
{
|
2016-12-12 19:50:46 +01:00
|
|
|
if (isset(self::$trait_names[strtolower($trait_name)])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::$existing_traits[strtolower($trait_name)])) {
|
|
|
|
return self::$existing_traits[strtolower($trait_name)];
|
|
|
|
}
|
|
|
|
|
2016-12-31 02:39:12 +01:00
|
|
|
$old_level = error_reporting();
|
|
|
|
error_reporting(0);
|
2016-12-12 19:50:46 +01:00
|
|
|
$trait_exists = trait_exists($trait_name);
|
2016-12-31 02:39:12 +01:00
|
|
|
error_reporting($old_level);
|
2016-12-12 19:50:46 +01:00
|
|
|
|
|
|
|
self::$existing_traits[strtolower($trait_name)] = $trait_exists;
|
|
|
|
|
|
|
|
return $trait_exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $trait_name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function hasCorrectCase($trait_name)
|
|
|
|
{
|
|
|
|
if (isset(self::$trait_names[strtolower($trait_name)])) {
|
|
|
|
return self::$trait_names[strtolower($trait_name)] === $trait_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$reflection_trait = new \ReflectionClass($trait_name);
|
|
|
|
return $reflection_trait->getName() === $trait_name;
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$trait_names = [];
|
|
|
|
self::$existing_traits = [];
|
2016-10-30 17:46:18 +01:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|