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

295 lines
6.0 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
*/
protected $file_path;
2016-11-21 03:49:06 +01:00
/**
* @var string|null
*/
protected $include_file_name;
/**
* @var string|null
*/
protected $include_file_path;
2016-11-21 03:49:06 +01:00
/**
* @var array<int, string>
2016-11-21 03:49:06 +01:00
*/
protected $suppressed_issues = [];
/**
2016-12-17 06:48:31 +01:00
* @var array<string, bool>
2016-11-21 03:49:06 +01:00
*/
protected $declared_classes = [];
/**
* @var StatementsSource|null
*/
protected $source = null;
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[strtolower($use_path)] = $use->alias;
2016-11-21 04:40:19 +01:00
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[strtolower($use_path)] = $use->alias;
2016-11-21 04:40:19 +01:00
break;
}
2016-11-21 03:49:06 +01:00
}
}
/**
* @return array<string, string>
2016-11-21 03:49:06 +01:00
*/
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;
}
/**
* @return null
*/
public function getFQCLN()
{
return null;
}
/**
* @return null
*/
public function getClassName()
{
return null;
}
/**
* @return null
*/
public function getClassLikeChecker()
{
return null;
}
/**
* @return FileChecker
*/
public function getFileChecker()
{
if ($this instanceof FileChecker) {
return $this;
}
if ($this->source === null) {
throw new \UnexpectedValueException('$this->source should not be null');
}
return $this->source->getFileChecker();
}
2016-11-21 03:49:06 +01:00
/**
* @return string|null
*/
public function getParentClass()
{
return null;
}
/**
* @return string
*/
public function getFileName()
{
return $this->file_name;
}
/**
* @return string
*/
public function getFilePath()
{
return $this->file_path;
}
2016-11-21 03:49:06 +01:00
/**
* @return null|string
*/
public function getIncludeFileName()
{
return $this->include_file_name;
}
/**
* @return null|string
*/
public function getIncludeFilePath()
{
return $this->include_file_path;
}
2016-11-21 03:49:06 +01:00
/**
* @param string|null $file_name
* @param string|null $file_path
2016-11-21 03:49:06 +01:00
* @return void
*/
public function setIncludeFileName($file_name, $file_path)
2016-11-21 03:49:06 +01:00
{
$this->include_file_name = $file_name;
$this->include_file_path = $file_path;
2016-11-21 03:49:06 +01:00
}
/**
* @return string
*/
public function getCheckedFileName()
{
return $this->include_file_name ?: $this->file_name;
}
/**
* @return string
*/
public function getCheckedFilePath()
{
return $this->include_file_path ?: $this->file_path;
}
2016-11-21 03:49:06 +01:00
/**
* @return bool
*/
public function isStatic()
{
return false;
}
/**
* @return StatementsSource
2016-11-21 03:49:06 +01:00
*/
public function getSource()
{
return $this->source ?: $this;
2016-11-21 03:49:06 +01:00
}
/**
* Get a list of suppressed issues
*
* @return array<string>
*/
public function getSuppressedIssues()
{
return $this->suppressed_issues;
}
2016-12-17 06:48:31 +01:00
public function getNamespace()
{
return '';
}
2016-11-21 03:49:06 +01:00
}