1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/src/Psalm/Checker/SourceChecker.php

252 lines
5.2 KiB
PHP
Raw Normal View History

2016-11-21 03:49:06 +01:00
<?php
namespace Psalm\Checker;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser;
use Psalm\Context;
use Psalm\StatementsSource;
use Psalm\Type;
abstract class SourceChecker implements StatementsSource
{
/**
* @var array<string, string>
*/
protected $aliased_classes = [];
/**
* @var array<string, string>
*/
protected $aliased_classes_flipped = [];
/**
* @var array<string, string>
*/
protected $aliased_functions = [];
/**
* @var array<string, string>
*/
protected $aliased_constants = [];
/**
* @var string
*/
protected $file_name;
/**
* @var string|null
*/
protected $include_file_name;
/**
* @var array
*/
protected $suppressed_issues = [];
/**
* @var array
*/
protected $declared_classes = [];
2016-11-21 05:45:10 +01:00
/**
* @param PhpParser\Node\Stmt\Use_ $stmt
* @return void
*/
2016-11-21 03:49:06 +01:00
public function visitUse(PhpParser\Node\Stmt\Use_ $stmt)
{
2016-11-21 04:40:19 +01:00
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:
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[$use_path] = strtolower($use->alias);
break;
}
}
}
2016-11-21 05:45:10 +01:00
/**
* @param PhpParser\Node\Stmt\GroupUse $stmt
* @return void
*/
2016-11-21 04:40:19 +01:00
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:
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[$use_path] = strtolower($use->alias);
break;
}
2016-11-21 03:49:06 +01:00
}
}
/**
* @param string $class_name
* @return bool
*/
public function containsClass($class_name)
{
return isset($this->declared_classes[$class_name]);
}
/**
* @return array
*/
public function getAliasedClasses()
{
return $this->aliased_classes;
}
/**
* @return array
*/
public function getAliasedClassesFlipped()
{
return $this->aliased_classes_flipped;
}
/**
* Gets a list of all aliased constants
*
* @return array
*/
public function getAliasedConstants()
{
return $this->aliased_constants;
}
/**
* Gets a list of all aliased functions
*
* @return array
*/
public function getAliasedFunctions()
{
return $this->aliased_functions;
}
/**
* Gets a list of the classes declared
*
* @return array<int, string>
*/
public function getDeclaredClasses()
{
return $this->declared_classes;
}
/**
* @return null
*/
public function getFQCLN()
{
return null;
}
/**
* @return null
*/
public function getClassName()
{
return null;
}
/**
* @return null
*/
public function getClassLikeChecker()
{
return null;
}
/**
* @return string|null
*/
public function getParentClass()
{
return null;
}
/**
* @return string
*/
public function getFileName()
{
return $this->file_name;
}
/**
* @return null|string
*/
public function getIncludeFileName()
{
return $this->include_file_name;
}
/**
* @param string|null $file_name
* @return void
*/
public function setIncludeFileName($file_name)
{
$this->include_file_name = $file_name;
}
/**
* @return string
*/
public function getCheckedFileName()
{
return $this->include_file_name ?: $this->file_name;
}
/**
* @return bool
*/
public function isStatic()
{
return false;
}
/**
* @return null
*/
public function getSource()
{
return null;
}
/**
* Get a list of suppressed issues
*
* @return array<string>
*/
public function getSuppressedIssues()
{
return $this->suppressed_issues;
}
}