2016-06-10 00:08:25 +02:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\Config;
|
2016-08-14 01:44:24 +02:00
|
|
|
use Psalm\Exception;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
class ProjectChecker
|
|
|
|
{
|
2016-06-26 19:45:20 +02:00
|
|
|
/**
|
|
|
|
* Cached config
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-06-26 19:45:20 +02:00
|
|
|
* @var Config|null
|
|
|
|
*/
|
|
|
|
protected static $config;
|
|
|
|
|
2016-07-25 21:05:58 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to use colors in error output
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-07-25 21:05:58 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $use_color = true;
|
|
|
|
|
2016-08-04 20:38:43 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to show informational messages
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-08-04 20:38:43 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $show_info = true;
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param boolean $debug
|
|
|
|
* @param boolean $is_diff
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
public static function check($debug = false, $is_diff = false)
|
2016-06-13 21:33:18 +02:00
|
|
|
{
|
2016-10-15 06:12:57 +02:00
|
|
|
$cwd = getcwd();
|
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
$start_checks = (int)microtime(true);
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
if (!$cwd) {
|
|
|
|
throw new \InvalidArgumentException('Cannot work with empty cwd');
|
|
|
|
}
|
|
|
|
|
2016-06-26 21:33:51 +02:00
|
|
|
if (!self::$config) {
|
2016-10-15 06:12:57 +02:00
|
|
|
self::$config = self::getConfigForPath($cwd);
|
2016-06-26 21:33:51 +02:00
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
$diff_files = null;
|
2016-10-07 19:26:29 +02:00
|
|
|
$deleted_files = null;
|
2016-10-05 19:24:46 +02:00
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
if ($is_diff && FileChecker::loadReferenceCache() && FileChecker::canDiffFiles()) {
|
2016-10-07 19:26:29 +02:00
|
|
|
$deleted_files = FileChecker::getDeletedReferencedFiles();
|
|
|
|
$diff_files = $deleted_files;
|
2016-10-07 06:58:08 +02:00
|
|
|
|
|
|
|
foreach (self::$config->getIncludeDirs() as $dir_name) {
|
|
|
|
$diff_files = array_merge($diff_files, self::getDiffFilesInDir($dir_name, self::$config));
|
|
|
|
}
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
2016-10-07 06:58:08 +02:00
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
$files_checked = [];
|
|
|
|
|
2016-10-07 19:26:29 +02:00
|
|
|
if ($diff_files === null || $deleted_files === null || count($diff_files) > 200) {
|
2016-10-05 19:24:46 +02:00
|
|
|
foreach (self::$config->getIncludeDirs() as $dir_name) {
|
2016-10-07 06:58:08 +02:00
|
|
|
self::checkDirWithConfig($dir_name, self::$config, $debug);
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-07 06:58:08 +02:00
|
|
|
if ($debug) {
|
|
|
|
echo count($diff_files) . ' changed files' . PHP_EOL;
|
|
|
|
}
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
$file_list = self::getReferencedFilesFromDiff($diff_files);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-10-07 19:26:29 +02:00
|
|
|
// strip out deleted files
|
|
|
|
$file_list = array_diff($file_list, $deleted_files);
|
2016-11-04 01:51:56 +01:00
|
|
|
self::checkDiffFilesWithConfig(self::$config, $debug, $file_list);
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
$removed_parser_files = FileChecker::deleteOldParserCaches(
|
|
|
|
$is_diff ? FileChecker::getLastGoodRun() : $start_checks
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($debug && $removed_parser_files) {
|
|
|
|
echo 'Removed ' . $removed_parser_files . ' old parser caches' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_diff) {
|
|
|
|
FileChecker::touchParserCaches(self::getAllFiles(self::$config), $start_checks);
|
|
|
|
}
|
|
|
|
|
|
|
|
IssueBuffer::finish(true, (int)$start_checks);
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $dir_name
|
|
|
|
* @param boolean $debug
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-13 21:33:18 +02:00
|
|
|
public static function checkDir($dir_name, $debug = false)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2016-06-26 19:45:20 +02:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = self::getConfigForPath($dir_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$config->hide_external_errors = self::$config->isInProjectDirs(
|
|
|
|
self::$config->shortenFileName($dir_name)
|
|
|
|
);
|
2016-06-26 19:45:20 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
FileChecker::loadReferenceCache();
|
|
|
|
|
2016-06-26 21:25:38 +02:00
|
|
|
self::checkDirWithConfig($dir_name, self::$config, $debug);
|
|
|
|
|
|
|
|
IssueBuffer::finish();
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $dir_name
|
|
|
|
* @param Config $config
|
|
|
|
* @param bool $debug
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
protected static function checkDirWithConfig($dir_name, Config $config, $debug)
|
2016-06-26 21:25:38 +02:00
|
|
|
{
|
|
|
|
$file_extensions = $config->getFileExtensions();
|
|
|
|
$filetype_handlers = $config->getFiletypeHandlers();
|
2016-06-13 21:33:18 +02:00
|
|
|
|
|
|
|
/** @var RecursiveDirectoryIterator */
|
2016-06-28 20:28:45 +02:00
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_name));
|
2016-06-13 21:33:18 +02:00
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions)) {
|
2016-10-15 06:12:57 +02:00
|
|
|
$file_name = (string)$iterator->getRealPath();
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
echo 'Checking ' . $file_name . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($filetype_handlers[$extension])) {
|
|
|
|
/** @var FileChecker */
|
|
|
|
$file_checker = new $filetype_handlers[$extension]($file_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-06-20 06:38:13 +02:00
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_checker->check(true);
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
protected static function getAllFiles(Config $config)
|
|
|
|
{
|
|
|
|
$file_extensions = $config->getFileExtensions();
|
|
|
|
$file_names = [];
|
|
|
|
|
|
|
|
foreach ($config->getIncludeDirs() as $dir_name) {
|
|
|
|
/** @var RecursiveDirectoryIterator */
|
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_name));
|
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions)) {
|
|
|
|
$file_names[] = (string)$iterator->getRealPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file_names;
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $dir_name
|
|
|
|
* @param Config $config
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
protected static function getDiffFilesInDir($dir_name, Config $config)
|
|
|
|
{
|
|
|
|
$file_extensions = $config->getFileExtensions();
|
|
|
|
$filetype_handlers = $config->getFiletypeHandlers();
|
|
|
|
|
|
|
|
/** @var RecursiveDirectoryIterator */
|
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_name));
|
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
$diff_files = [];
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions)) {
|
2016-10-15 06:12:57 +02:00
|
|
|
$file_name = (string)$iterator->getRealPath();
|
2016-10-07 06:58:08 +02:00
|
|
|
|
|
|
|
if (FileChecker::hasFileChanged($file_name)) {
|
|
|
|
$diff_files[] = $file_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $diff_files;
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
* @param bool $debug
|
2016-11-04 01:51:56 +01:00
|
|
|
* @param array<string> $file_list
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-04 01:51:56 +01:00
|
|
|
protected static function checkDiffFilesWithConfig(Config $config, $debug, array $file_list = [])
|
2016-10-05 19:24:46 +02:00
|
|
|
{
|
|
|
|
$file_extensions = $config->getFileExtensions();
|
|
|
|
$filetype_handlers = $config->getFiletypeHandlers();
|
|
|
|
|
|
|
|
foreach ($file_list as $file_name) {
|
2016-10-09 02:49:14 +02:00
|
|
|
if (!file_exists($file_name)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-18 16:17:25 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (!$config->isInProjectDirs(
|
|
|
|
preg_replace('/^' . preg_quote($config->getBaseDir(), '/') . '/', '', $file_name)
|
|
|
|
)) {
|
2016-10-18 16:17:25 +02:00
|
|
|
if ($debug) {
|
|
|
|
echo('skipping ' . $file_name . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-09 02:49:14 +02:00
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
echo 'Checking affected file ' . $file_name . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($filetype_handlers[$extension])) {
|
|
|
|
/** @var FileChecker */
|
|
|
|
$file_checker = new $filetype_handlers[$extension]($file_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-05 19:24:46 +02:00
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_checker->check(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @param boolean $debug
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 06:38:13 +02:00
|
|
|
public static function checkFile($file_name, $debug = false)
|
|
|
|
{
|
|
|
|
if ($debug) {
|
|
|
|
echo 'Checking ' . $file_name . PHP_EOL;
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = self::getConfigForPath($file_name);
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$config->hide_external_errors = self::$config->isInProjectDirs(
|
|
|
|
self::$config->shortenFileName($file_name)
|
|
|
|
);
|
2016-09-01 05:12:35 +02:00
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
$file_name_parts = explode('.', $file_name);
|
|
|
|
|
|
|
|
$extension = array_pop($file_name_parts);
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
$filetype_handlers = self::$config->getFiletypeHandlers();
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
FileChecker::loadReferenceCache();
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if (isset($filetype_handlers[$extension])) {
|
|
|
|
/** @var FileChecker */
|
2016-06-28 20:28:45 +02:00
|
|
|
$file_checker = new $filetype_handlers[$extension]($file_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-06-28 20:28:45 +02:00
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
$file_checker->check(true);
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
IssueBuffer::finish();
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Config object from an XML file.
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
|
|
|
* Searches up a folder hierarchy for the most immediate config.
|
2016-06-26 19:45:20 +02:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return Config
|
2016-11-02 07:29:00 +01:00
|
|
|
* @throws Exception\ConfigException If a config path is not found.
|
2016-06-26 19:45:20 +02:00
|
|
|
*/
|
|
|
|
protected static function getConfigForPath($path)
|
|
|
|
{
|
|
|
|
$dir_path = realpath($path) . '/';
|
|
|
|
|
|
|
|
if (!is_dir($dir_path)) {
|
|
|
|
$dir_path = dirname($dir_path) . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$maybe_path = $dir_path . Config::DEFAULT_FILE_NAME;
|
|
|
|
|
|
|
|
if (file_exists($maybe_path)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$config = Config::loadFromXML($maybe_path);
|
2016-07-26 21:03:15 +02:00
|
|
|
|
|
|
|
if ($config->autoloader) {
|
|
|
|
require_once($dir_path . $config->autoloader);
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir_path = preg_replace('/[^\/]+\/$/', '', $dir_path);
|
2016-11-02 07:29:00 +01:00
|
|
|
} while ($dir_path !== '/');
|
2016-06-26 19:45:20 +02:00
|
|
|
|
|
|
|
if (!$config) {
|
|
|
|
throw new Exception\ConfigException('Config not found for path ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
2016-06-26 21:33:51 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $path_to_config
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
* @throws Exception\ConfigException If a config file is not found in the given location.
|
2016-10-15 06:12:57 +02:00
|
|
|
*/
|
2016-06-26 21:33:51 +02:00
|
|
|
public static function setConfigXML($path_to_config)
|
|
|
|
{
|
|
|
|
if (!file_exists($path_to_config)) {
|
|
|
|
throw new Exception\ConfigException('Config not found at location ' . $path_to_config);
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
$dir_path = dirname($path_to_config) . '/';
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$config = Config::loadFromXML($path_to_config);
|
2016-07-26 21:00:40 +02:00
|
|
|
|
2016-07-26 21:03:15 +02:00
|
|
|
if (self::$config->autoloader) {
|
|
|
|
require_once($dir_path . self::$config->autoloader);
|
|
|
|
}
|
2016-06-26 21:33:51 +02:00
|
|
|
}
|
2016-10-05 19:24:46 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param array<string> $diff_files
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function getReferencedFilesFromDiff(array $diff_files)
|
|
|
|
{
|
2016-10-05 23:08:20 +02:00
|
|
|
$all_inherited_files_to_check = $diff_files;
|
2016-10-05 19:24:46 +02:00
|
|
|
|
|
|
|
while ($diff_files) {
|
|
|
|
$diff_file = array_shift($diff_files);
|
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
$dependent_files = FileChecker::getFilesInheritingFromFile($diff_file);
|
|
|
|
$new_dependent_files = array_diff($dependent_files, $all_inherited_files_to_check);
|
|
|
|
|
|
|
|
$all_inherited_files_to_check += $new_dependent_files;
|
|
|
|
$diff_files += $new_dependent_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
$all_files_to_check = $all_inherited_files_to_check;
|
|
|
|
|
|
|
|
foreach ($all_inherited_files_to_check as $file_name) {
|
|
|
|
$dependent_files = FileChecker::getFilesReferencingFile($file_name);
|
|
|
|
$all_files_to_check = array_merge($dependent_files, $all_files_to_check);
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
return array_unique($all_files_to_check);
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|