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;
|
|
|
|
|
|
|
|
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-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-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-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
|
|
|
|
|
|
|
parent::check($check_methods, $class_context);
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-15 20:20:06 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param array $method_map
|
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
return trait_exists($trait_name);
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|