2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Error;
|
|
|
|
use PhpParser\ParserFactory;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class FileChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-06-10 23:20:04 +02:00
|
|
|
protected $_real_file_name;
|
|
|
|
protected $_short_file_name;
|
2016-01-08 00:28:27 +01:00
|
|
|
protected $_namespace;
|
|
|
|
protected $_aliased_classes = [];
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
protected $_function_params = [];
|
|
|
|
protected $_class_name;
|
|
|
|
|
2016-02-18 21:05:13 +01:00
|
|
|
protected $_namespace_aliased_classes = [];
|
|
|
|
|
2016-04-04 01:41:54 +02:00
|
|
|
protected $_preloaded_statements = [];
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
protected $_declared_classes = [];
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_suppressed_issues = [];
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
protected static $_cache_dir = null;
|
2016-01-08 00:28:27 +01:00
|
|
|
protected static $_file_checkers = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
protected static $_functions = [];
|
2016-03-23 18:05:25 +01:00
|
|
|
protected static $_includes_to_ignore = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-05-10 20:00:44 +02:00
|
|
|
protected static $_ignore_check_variables_pattern = null;
|
|
|
|
|
2016-01-11 17:05:24 +01:00
|
|
|
public static $show_notices = true;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-04-04 01:41:54 +02:00
|
|
|
public function __construct($file_name, array $preloaded_statements = [])
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-06-10 23:20:04 +02:00
|
|
|
$this->_real_file_name = $file_name;
|
|
|
|
$this->_short_file_name = Config::getInstance()->shortenFileName($file_name);
|
2016-01-11 23:23:05 +01:00
|
|
|
|
2016-06-10 23:20:04 +02:00
|
|
|
self::$_file_checkers[$this->_short_file_name] = $this;
|
2016-04-04 01:41:54 +02:00
|
|
|
|
|
|
|
if ($preloaded_statements) {
|
|
|
|
$this->_preloaded_statements = $preloaded_statements;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-07-24 23:06:54 +02:00
|
|
|
public function check($check_classes = true, $check_class_statements = true, Context $file_context = null)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-07-24 23:06:54 +02:00
|
|
|
if (!$file_context) {
|
|
|
|
$file_context = new Context();
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
$stmts = $this->getStatements();
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Interface_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Trait_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Namespace_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Use_
|
|
|
|
) {
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
|
|
|
$statments_checker->check($leftover_stmts, $file_context);
|
|
|
|
$leftover_stmts = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
if ($check_classes) {
|
|
|
|
$class_checker = ClassChecker::getClassCheckerFromClass($stmt->name) ?: new ClassChecker($stmt, $this, $stmt->name);
|
|
|
|
$this->_declared_classes[] = $class_checker->getAbsoluteClass();
|
|
|
|
$class_checker->check($check_class_statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
// @todo check interfaces
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
if ($check_classes) {
|
|
|
|
$trait_checker = ClassChecker::getClassCheckerFromClass($stmt->name) ?: new TraitChecker($stmt, $this, $stmt->name);
|
|
|
|
$trait_checker->check($check_class_statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_) {
|
|
|
|
$namespace_name = implode('\\', $stmt->name->parts);
|
|
|
|
|
|
|
|
$namespace_checker = new NamespaceChecker($stmt, $this);
|
|
|
|
$this->_namespace_aliased_classes[$namespace_name] = $namespace_checker->check($check_classes, $check_class_statements);
|
|
|
|
$this->_declared_classes = array_merge($namespace_checker->getDeclaredClasses());
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$this->_aliased_classes[$use->alias] = implode('\\', $use->name->parts);
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-07-24 23:06:54 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
2016-07-24 23:06:54 +02:00
|
|
|
$statments_checker->check($leftover_stmts, $file_context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
|
|
|
return $stmts;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
public static function getAbsoluteClassFromNameInFile($class, $namespace, $file_name)
|
2016-01-11 20:21:29 +01:00
|
|
|
{
|
2016-06-20 06:38:13 +02:00
|
|
|
if (isset(self::$_file_checkers[$file_name])) {
|
|
|
|
$aliased_classes = self::$_file_checkers[$file_name]->getAliasedClasses($namespace);
|
2016-01-11 20:21:29 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
} else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-07-24 23:06:54 +02:00
|
|
|
$file_checker->check(false, false, new Context());
|
2016-06-20 06:38:13 +02:00
|
|
|
$aliased_classes = $file_checker->getAliasedClasses($namespace);
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
return ClassChecker::getAbsoluteClassFromString($class, $namespace, $aliased_classes);
|
|
|
|
}
|
2016-02-04 22:05:36 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getDeclaredClasses()
|
|
|
|
{
|
|
|
|
return $this->_declared_classes;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared in that file
|
|
|
|
* @param string $file_name
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getDeclaredClassesInFile($file_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
if (isset(self::$_file_checkers[$file_name])) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$file_checker = self::$_file_checkers[$file_name];
|
|
|
|
}
|
|
|
|
else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-07-24 23:06:54 +02:00
|
|
|
$file_checker->check(false, false, new Context());
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
return $file_checker->getDeclaredClasses();
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return array<\PhpParser\Node>
|
|
|
|
*/
|
2016-06-18 20:45:55 +02:00
|
|
|
protected function getStatements()
|
|
|
|
{
|
|
|
|
return $this->_preloaded_statements ?
|
|
|
|
$this->_preloaded_statements :
|
|
|
|
self::getStatementsForFile($this->_real_file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<\PhpParser\Node>
|
|
|
|
*/
|
|
|
|
public static function getStatementsForFile($file_name)
|
2016-01-08 04:52:26 +01:00
|
|
|
{
|
|
|
|
$contents = file_get_contents($file_name);
|
|
|
|
|
|
|
|
$stmts = [];
|
|
|
|
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = false;
|
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
$cache_location = null;
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
if (self::$_cache_dir) {
|
|
|
|
$key = md5($contents);
|
|
|
|
|
|
|
|
$cache_location = self::$_cache_dir . '/' . $key;
|
|
|
|
|
|
|
|
if (is_readable($cache_location)) {
|
|
|
|
$stmts = unserialize(file_get_contents($cache_location));
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = true;
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmts) {
|
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
$stmts = $parser->parse($contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::$_cache_dir) {
|
2016-01-08 16:47:15 +01:00
|
|
|
if ($from_cache) {
|
|
|
|
touch($cache_location);
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-01-08 16:47:15 +01:00
|
|
|
if (!file_exists(self::$_cache_dir)) {
|
|
|
|
mkdir(self::$_cache_dir);
|
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-01-08 16:47:15 +01:00
|
|
|
file_put_contents($cache_location, serialize($stmts));
|
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:32:37 +02:00
|
|
|
if (!$stmts) {
|
2016-04-28 21:18:36 +02:00
|
|
|
return [];
|
2016-04-27 00:32:37 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
return $stmts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setCacheDir($cache_dir)
|
|
|
|
{
|
|
|
|
self::$_cache_dir = $cache_dir;
|
|
|
|
}
|
2016-01-11 23:23:05 +01:00
|
|
|
|
2016-04-17 20:48:57 +02:00
|
|
|
public function registerFunction(PhpParser\Node\Stmt\Function_ $function)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-04-17 20:48:57 +02:00
|
|
|
$function_name = $function->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
$this->_function_params[$function_name] = [];
|
|
|
|
|
|
|
|
foreach ($function->params as $param) {
|
|
|
|
$this->_function_params[$function_name][] = $param->byRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses($namespace_name = null)
|
|
|
|
{
|
2016-02-18 21:05:13 +01:00
|
|
|
if ($namespace_name && isset($this->_namespace_aliased_classes[$namespace_name])) {
|
|
|
|
return $this->_namespace_aliased_classes[$namespace_name];
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->_class_name;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-30 00:48:09 +01:00
|
|
|
public function getClassChecker()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-04-17 17:22:18 +02:00
|
|
|
public function getParentClass()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2016-06-10 23:20:04 +02:00
|
|
|
return $this->_short_file_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
|
|
|
return $this->_suppressed_issues;
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
public static function getFileCheckerFromFileName($file_name)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return self::$_file_checkers[$file_name];
|
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public static function getClassCheckerFromClass($class_name)
|
|
|
|
{
|
|
|
|
$file_name = (new \ReflectionClass($class_name))->getFileName();
|
|
|
|
|
|
|
|
if (isset(self::$_file_checkers[$file_name])) {
|
|
|
|
$file_checker = self::$_file_checkers[$file_name];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_checker->check(true, false);
|
|
|
|
|
|
|
|
return ClassChecker::getClassCheckerFromClass($class_name);
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function hasFunction($function_name)
|
|
|
|
{
|
|
|
|
return isset($this->_function_params[$function_name]);
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isPassedByReference($function_name, $argument_offset)
|
|
|
|
{
|
|
|
|
return $argument_offset < count($this->_function_params[$function_name]) && $this->_function_params[$function_name][$argument_offset];
|
|
|
|
}
|
2016-02-04 15:22:46 +01:00
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
public static function ignoreIncludes(array $includes)
|
|
|
|
{
|
|
|
|
self::$_includes_to_ignore = $includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getIncludesToIgnore()
|
|
|
|
{
|
|
|
|
return self::$_includes_to_ignore;
|
|
|
|
}
|
2016-05-10 20:00:44 +02:00
|
|
|
|
|
|
|
public static function ignoreVariableChecksFor($pattern)
|
|
|
|
{
|
|
|
|
self::$_ignore_check_variables_pattern = $pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function shouldCheckVariables($file_name)
|
|
|
|
{
|
|
|
|
return !self::$_ignore_check_variables_pattern || !preg_match(self::$_ignore_check_variables_pattern, $file_name);
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|