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;
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $file_path;
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $include_file_name;
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $include_file_path;
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
2017-01-02 07:07:44 +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 = [];
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
2016-12-10 19:07:12 +01:00
|
|
|
$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;
|
2016-12-10 19:07:12 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-24 19:23:22 +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;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getIncludeFilePath()
|
|
|
|
{
|
|
|
|
return $this->include_file_path;
|
|
|
|
}
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* @param string|null $file_name
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param string|null $file_path
|
2016-11-21 03:49:06 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-04 01:11:30 +01:00
|
|
|
public function setIncludeFileName($file_name, $file_path)
|
2016-11-21 03:49:06 +01:00
|
|
|
{
|
|
|
|
$this->include_file_name = $file_name;
|
2016-12-04 01:11:30 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @return StatementsSource
|
2016-11-21 03:49:06 +01:00
|
|
|
*/
|
|
|
|
public function getSource()
|
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
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
|
|
|
}
|