1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Checker/TraitChecker.php

142 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
use PhpParser;
use Psalm\StatementsSource;
use Psalm\Context;
use Psalm\Storage\ClassLikeStorage;
class TraitChecker extends ClassLikeChecker
{
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-11-02 07:29:00 +01:00
/**
2016-11-05 02:14:04 +01:00
* @param PhpParser\Node\Stmt\ClassLike $class
* @param StatementsSource $source
* @param string $fq_class_name
2016-11-02 07:29:00 +01:00
*/
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
{
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');
}
$this->source = $source;
$this->class = $class;
$this->fq_class_name = $fq_class_name;
if (!isset(self::$storage[$fq_class_name])) {
self::$storage[$fq_class_name] = new ClassLikeStorage();
2017-01-07 20:35:07 +01:00
self::$storage[$fq_class_name]->file_name = $this->source->getFileName();
self::$storage[$fq_class_name]->file_path = $this->source->getFilePath();
}
2017-01-07 20:35:07 +01:00
self::$trait_names[strtolower($fq_class_name)] = $fq_class_name;
2016-12-12 19:50:46 +01:00
self::$trait_checkers[$fq_class_name] = $this;
2016-08-08 20:36:18 +02:00
}
2016-11-02 07:29:00 +01:00
/**
* @param Context|null $class_context
* @param Context|null $global_context
2016-11-02 07:29:00 +01:00
* @return void
*/
public function visit(
Context $class_context = null,
Context $global_context = null
) {
2016-08-08 20:36:18 +02:00
if (!$class_context) {
throw new \InvalidArgumentException('TraitChecker::check must be called with a $class_context');
}
2016-08-08 20:36:18 +02:00
parent::visit($class_context, $global_context);
}
2016-08-15 20:20:06 +02:00
2016-11-02 07:29:00 +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
* @param FileChecker $file_checker
2016-10-30 17:46:18 +01:00
* @return boolean
*/
public static function traitExists($trait_name, FileChecker $file_checker)
2016-10-30 17:46:18 +01:00
{
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)];
}
if ($file_checker->evaluateClassLike($trait_name) === false) {
self::$existing_traits[strtolower($trait_name)] = false;
return false;
}
2016-12-12 19:50:46 +01:00
self::$existing_traits[strtolower($trait_name)] = true;
2016-12-12 19:50:46 +01:00
return true;
2016-12-12 19:50:46 +01:00
}
/**
* @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
}
}