1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 02:07:37 +01:00
psalm/src/Psalm/Checker/CanAlias.php

138 lines
4.0 KiB
PHP
Raw Normal View History

2017-01-07 20:35:07 +01:00
<?php
namespace Psalm\Checker;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser;
use Psalm\Context;
use Psalm\StatementsSource;
use Psalm\Type;
trait CanAlias
{
/**
* @var array<string, string>
*/
private $aliased_classes = [];
2017-01-07 20:35:07 +01:00
/**
* @var array<string, string>
*/
private $aliased_classes_flipped = [];
2017-01-07 20:35:07 +01:00
/**
* @var array<string, string>
*/
private $aliased_functions = [];
2017-01-07 20:35:07 +01:00
/**
* @var array<string, string>
*/
private $aliased_constants = [];
2017-01-07 20:35:07 +01:00
/**
* @param PhpParser\Node\Stmt\Use_ $stmt
* @return void
*/
public function visitUse(PhpParser\Node\Stmt\Use_ $stmt)
{
foreach ($stmt->uses as $use) {
$use_path = implode('\\', $use->name->parts);
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use->alias)] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use->alias] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
2017-02-28 00:24:20 +01:00
if ($this->getFileChecker()->project_checker->collect_references) {
// register the path
$project_checker = $this->getFileChecker()->project_checker;
$project_checker->use_referencing_locations[$use_path][$this->getFilePath()] =
new \Psalm\CodeLocation($this, $use);
}
2017-01-07 20:35:07 +01:00
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[strtolower($use_path)] = $use->alias;
break;
}
}
}
/**
* @param PhpParser\Node\Stmt\GroupUse $stmt
* @return void
*/
public function visitGroupUse(PhpParser\Node\Stmt\GroupUse $stmt)
{
$use_prefix = implode('\\', $stmt->prefix->parts);
foreach ($stmt->uses as $use) {
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use->alias)] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use->alias] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
2017-02-28 00:24:20 +01:00
if ($this->getFileChecker()->project_checker->collect_references) {
// register the path
$project_checker = $this->getFileChecker()->project_checker;
$project_checker->use_referencing_locations[$use_path][$this->getFilePath()] =
new \Psalm\CodeLocation($this, $use);
}
2017-01-07 20:35:07 +01:00
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[strtolower($use_path)] = $use->alias;
break;
}
}
}
/**
* @return array<string, string>
*/
public function getAliasedClasses()
{
return $this->aliased_classes;
}
/**
* @return array<string, string>
*/
public function getAliasedClassesFlipped()
{
return $this->aliased_classes_flipped;
}
/**
* Gets a list of all aliased constants
*
* @return array<string, string>
*/
public function getAliasedConstants()
{
return $this->aliased_constants;
}
/**
* Gets a list of all aliased functions
*
* @return array<string, string>
*/
public function getAliasedFunctions()
{
return $this->aliased_functions;
}
}