2016-07-26 21:12:44 +02:00
|
|
|
<?php
|
2018-01-07 06:11:23 +01:00
|
|
|
require_once('command_functions.php');
|
2016-07-26 21:12:44 +02:00
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
|
2016-07-26 21:12:44 +02:00
|
|
|
// show all errors
|
2016-10-30 17:46:18 +01:00
|
|
|
error_reporting(-1);
|
2016-07-26 21:12:44 +02:00
|
|
|
ini_set('display_errors', 1);
|
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
ini_set('memory_limit', '2048M');
|
|
|
|
|
|
|
|
// get options from command line
|
2016-12-08 04:38:57 +01:00
|
|
|
$options = getopt(
|
2017-11-11 18:11:11 +01:00
|
|
|
'f:mhvc:ir:',
|
2016-12-08 04:38:57 +01:00
|
|
|
[
|
|
|
|
'help', 'debug', 'config:', 'monochrome', 'show-info:', 'diff',
|
2018-01-07 06:11:23 +01:00
|
|
|
'self-check', 'output-format:', 'report:', 'find-dead-code', 'init',
|
|
|
|
'find-references-to:', 'root:', 'threads:', 'clear-cache', 'no-cache',
|
|
|
|
'version', 'plugin:',
|
2016-12-08 04:38:57 +01:00
|
|
|
]
|
|
|
|
);
|
2016-10-18 23:00:03 +02:00
|
|
|
|
2016-11-05 01:13:16 +01:00
|
|
|
if (array_key_exists('help', $options)) {
|
2016-11-05 03:55:13 +01:00
|
|
|
$options['h'] = false;
|
2016-11-05 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2017-11-11 18:11:11 +01:00
|
|
|
if (array_key_exists('version', $options)) {
|
|
|
|
$options['v'] = false;
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
if (array_key_exists('init', $options)) {
|
|
|
|
$options['i'] = false;
|
|
|
|
}
|
|
|
|
|
2016-11-05 01:13:16 +01:00
|
|
|
if (array_key_exists('monochrome', $options)) {
|
2016-11-05 03:55:13 +01:00
|
|
|
$options['m'] = false;
|
2016-11-05 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['config'])) {
|
2016-11-05 03:55:13 +01:00
|
|
|
$options['c'] = $options['config'];
|
2016-11-05 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:44:31 +01:00
|
|
|
if (isset($options['c']) && is_array($options['c'])) {
|
|
|
|
die('Too many config files provided' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2016-11-05 01:13:16 +01:00
|
|
|
if (array_key_exists('h', $options)) {
|
2016-10-18 23:00:03 +02:00
|
|
|
echo <<< HELP
|
|
|
|
Usage:
|
2016-11-05 03:55:13 +01:00
|
|
|
psalm [options] [file...]
|
2016-10-18 23:00:03 +02:00
|
|
|
|
|
|
|
Options:
|
2017-02-13 05:59:33 +01:00
|
|
|
-h, --help
|
|
|
|
Display this help message
|
|
|
|
|
2017-11-11 18:11:11 +01:00
|
|
|
-v, --version
|
|
|
|
Display the Psalm version
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
-i, --init [source_dir=src] [--level=3]
|
|
|
|
Create a psalm config file in the current directory that points to [source_dir]
|
|
|
|
at the required level, from 1, most strict, to 5, most permissive
|
|
|
|
|
|
|
|
--debug
|
|
|
|
Debug information
|
|
|
|
|
|
|
|
-c, --config=psalm.xml
|
|
|
|
Path to a psalm.xml configuration file. Run psalm --init to create one.
|
|
|
|
|
|
|
|
-m, --monochrome
|
|
|
|
Enable monochrome output
|
|
|
|
|
2017-05-04 20:25:58 +02:00
|
|
|
-r, --root
|
|
|
|
If running Psalm globally you'll need to specify a project root. Defaults to cwd
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
--show-info[=BOOLEAN]
|
|
|
|
Show non-exception parser findings
|
|
|
|
|
|
|
|
--diff
|
|
|
|
Runs Psalm in diff mode, only checking files that have changed (and their dependents)
|
|
|
|
|
|
|
|
--self-check
|
|
|
|
Psalm checks itself
|
|
|
|
|
|
|
|
--output-format=console
|
2017-10-13 05:33:53 +02:00
|
|
|
Changes the output format. Possible values: console, json, xml
|
2017-02-13 05:59:33 +01:00
|
|
|
|
|
|
|
--find-dead-code
|
|
|
|
Look for dead code
|
2016-10-18 23:00:03 +02:00
|
|
|
|
2017-02-27 07:30:44 +01:00
|
|
|
--find-references-to=[class|method]
|
|
|
|
Searches the codebase for references to the given fully-qualified class or method,
|
|
|
|
where method is in the format class::methodName
|
2016-10-18 23:00:03 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
--threads=INT
|
|
|
|
If greater than one, Psalm will run analysis on multiple threads, speeding things up.
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
--report=PATH
|
|
|
|
The path where to output report file. The output format is base on the file extension.
|
|
|
|
(Currently supported format: ".json", ".xml", ".txt")
|
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
--clear-cache
|
|
|
|
Clears all cache files that Psalm uses
|
|
|
|
|
2017-10-14 03:27:20 +02:00
|
|
|
--no-cache
|
|
|
|
Runs Psalm without using cache
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
--plugin=PATH
|
|
|
|
Executes a plugin, an alternative to using the Psalm config
|
|
|
|
|
2016-10-18 23:00:03 +02:00
|
|
|
HELP;
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
2016-07-26 21:12:44 +02:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
if (getcwd() === false) {
|
|
|
|
die('Cannot get current working directory');
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:25:58 +02:00
|
|
|
if (isset($options['root'])) {
|
|
|
|
$options['r'] = $options['root'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = (string)getcwd() . DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
if (isset($options['r']) && is_string($options['r'])) {
|
2017-05-04 20:25:58 +02:00
|
|
|
$root_path = realpath($options['r']);
|
|
|
|
|
|
|
|
if (!$root_path) {
|
|
|
|
die('Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = $root_path . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
requireAutoloaders($current_dir);
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-11-11 18:11:11 +01:00
|
|
|
if (array_key_exists('v', $options)) {
|
2017-12-29 18:29:36 +01:00
|
|
|
/** @var string */
|
2018-01-01 16:01:04 +01:00
|
|
|
$version = \Muglug\PackageVersions\Versions::getVersion('vimeo/psalm');
|
2017-11-11 18:11:11 +01:00
|
|
|
echo 'Psalm ' . $version . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
// If XDebug is enabled, restart without it
|
|
|
|
(new \Composer\XdebugHandler(\Composer\Factory::createOutput()))->check();
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
if (isset($options['i'])) {
|
|
|
|
if (file_exists('psalm.xml')) {
|
|
|
|
die('A config file already exists in the current directory' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:44:16 +01:00
|
|
|
$args = array_values(array_filter(
|
2017-10-14 03:16:34 +02:00
|
|
|
array_slice($argv, 2),
|
2017-12-29 18:29:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $arg
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-10-14 03:16:34 +02:00
|
|
|
function ($arg) {
|
|
|
|
return $arg !== '--ansi' && $arg !== '--no-ansi';
|
|
|
|
}
|
2017-11-10 22:44:16 +01:00
|
|
|
));
|
2017-10-14 03:16:34 +02:00
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
$level = 3;
|
|
|
|
$source_dir = 'src';
|
|
|
|
|
|
|
|
if (count($args)) {
|
|
|
|
if (count($args) > 2) {
|
|
|
|
die('Too many arguments provided for psalm --init' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($args[1])) {
|
|
|
|
if (!preg_match('/^[1-5]$/', $args[1])) {
|
|
|
|
die('Config strictness must be a number between 1 and 5 inclusive' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$level = (int)$args[1];
|
|
|
|
}
|
|
|
|
|
2017-02-13 06:12:56 +01:00
|
|
|
$source_dir = $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($source_dir)) {
|
|
|
|
$bad_dir_path = getcwd() . DIRECTORY_SEPARATOR . $source_dir;
|
|
|
|
|
|
|
|
if (!isset($args[0])) {
|
|
|
|
die('Please specify a directory - the default, "src", was not found in this project.' . PHP_EOL);
|
2017-02-13 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 06:12:56 +01:00
|
|
|
die('The given path "' . $bad_dir_path . '" does not appear to be a directory' . PHP_EOL);
|
2017-02-13 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 06:07:26 +02:00
|
|
|
$template_file_name = dirname(__DIR__) . '/assets/config_levels/' . $level . '.xml';
|
2017-02-13 05:59:33 +01:00
|
|
|
|
|
|
|
if (!file_exists($template_file_name)) {
|
2017-05-28 06:07:26 +02:00
|
|
|
die('Could not open config template ' . $template_file_name . PHP_EOL);
|
2017-02-13 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$template = (string)file_get_contents($template_file_name);
|
|
|
|
|
|
|
|
$template = str_replace('<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>', '<projectFiles>
|
|
|
|
<directory name="' . $source_dir . '" />
|
|
|
|
</projectFiles>', $template);
|
|
|
|
|
|
|
|
if (!file_put_contents('psalm.xml', $template)) {
|
|
|
|
die('Could not write to psalm.xml' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
$output_format = isset($options['output-format']) && is_string($options['output-format'])
|
|
|
|
? $options['output-format']
|
|
|
|
: ProjectChecker::TYPE_CONSOLE;
|
2016-12-08 04:38:57 +01:00
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
$paths_to_check = getPathsToCheck(isset($options['f']) ? $options['f'] : null);
|
2016-11-05 01:13:16 +01:00
|
|
|
|
2018-01-02 02:05:54 +01:00
|
|
|
$plugins = [];
|
|
|
|
|
|
|
|
if (isset($options['plugin'])) {
|
|
|
|
$plugins = $options['plugin'];
|
|
|
|
|
|
|
|
if (!is_array($plugins)) {
|
|
|
|
$plugins = [$plugins];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
$path_to_config = isset($options['c']) && is_string($options['c']) ? realpath($options['c']) : null;
|
2016-11-05 03:55:13 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
if ($path_to_config === false) {
|
2017-12-29 18:29:36 +01:00
|
|
|
/** @psalm-suppress InvalidCast */
|
|
|
|
die('Could not resolve path to config ' . (string)$options['c'] . PHP_EOL);
|
2017-01-16 17:41:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 20:38:43 +02:00
|
|
|
$show_info = isset($options['show-info'])
|
2018-01-07 06:11:23 +01:00
|
|
|
? $options['show-info'] !== 'false' && $options['show-info'] !== '0'
|
|
|
|
: true;
|
2016-11-05 03:55:13 +01:00
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
$is_diff = isset($options['diff']);
|
2016-07-26 21:12:44 +02:00
|
|
|
|
2017-02-01 02:47:16 +01:00
|
|
|
$find_dead_code = isset($options['find-dead-code']);
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
$find_references_to = isset($options['find-references-to']) && is_string($options['find-references-to'])
|
|
|
|
? $options['find-references-to']
|
|
|
|
: null;
|
2017-02-27 07:30:44 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$threads = isset($options['threads']) ? (int)$options['threads'] : 1;
|
|
|
|
|
2017-10-14 03:27:20 +02:00
|
|
|
$cache_provider = isset($options['no-cache'])
|
2017-10-15 17:57:44 +02:00
|
|
|
? new Psalm\Provider\Cache\NoParserCacheProvider()
|
|
|
|
: new Psalm\Provider\ParserCacheProvider();
|
2017-10-14 03:27:20 +02:00
|
|
|
|
2017-02-01 02:47:16 +01:00
|
|
|
$project_checker = new ProjectChecker(
|
2017-07-25 22:11:02 +02:00
|
|
|
new Psalm\Provider\FileProvider(),
|
2017-10-14 03:27:20 +02:00
|
|
|
$cache_provider,
|
2018-01-07 06:11:23 +01:00
|
|
|
!array_key_exists('m', $options),
|
2017-02-01 02:47:16 +01:00
|
|
|
$show_info,
|
|
|
|
$output_format,
|
2017-07-25 22:11:02 +02:00
|
|
|
$threads,
|
2018-01-07 06:11:23 +01:00
|
|
|
array_key_exists('debug', $options),
|
2017-02-27 07:30:44 +01:00
|
|
|
$find_dead_code || $find_references_to !== null,
|
2017-09-08 17:18:48 +02:00
|
|
|
$find_references_to,
|
2017-12-29 18:29:36 +01:00
|
|
|
isset($options['report']) && is_string($options['report']) ? $options['report'] : null
|
2017-02-01 02:47:16 +01:00
|
|
|
);
|
2017-01-16 17:05:29 +01:00
|
|
|
|
2016-07-26 21:12:44 +02:00
|
|
|
// initialise custom config, if passed
|
|
|
|
if ($path_to_config) {
|
2017-05-04 20:25:58 +02:00
|
|
|
$project_checker->setConfigXML($path_to_config, $current_dir);
|
2016-07-26 21:12:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
if (isset($options['clear-cache'])) {
|
|
|
|
// initialise config if it hasn't already been
|
|
|
|
if (!$path_to_config) {
|
2017-12-29 18:29:36 +01:00
|
|
|
$project_checker->getConfigForPath($current_dir, $current_dir);
|
2017-10-07 20:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
Config::removeCacheDirectory($cache_directory);
|
2017-10-07 20:41:16 +02:00
|
|
|
echo 'Cache directory deleted' . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
2017-02-18 23:49:05 +01:00
|
|
|
|
2018-01-02 02:04:03 +01:00
|
|
|
$config = $project_checker->getConfig();
|
|
|
|
|
|
|
|
if (!$config) {
|
|
|
|
$project_checker->getConfigForPath($current_dir, $current_dir);
|
|
|
|
}
|
|
|
|
|
2018-01-02 02:05:54 +01:00
|
|
|
/** @var string $plugin_path */
|
|
|
|
foreach ($plugins as $plugin_path) {
|
|
|
|
Config::getInstance()->addPluginPath($current_dir . DIRECTORY_SEPARATOR . $plugin_path);
|
|
|
|
}
|
|
|
|
|
2017-12-29 18:29:36 +01:00
|
|
|
/** @psalm-suppress MixedArgument */
|
2016-12-15 01:24:16 +01:00
|
|
|
\Psalm\IssueBuffer::setStartTime(microtime(true));
|
2016-07-26 21:12:44 +02:00
|
|
|
|
2016-11-05 01:24:43 +01:00
|
|
|
if (array_key_exists('self-check', $options)) {
|
2018-01-02 02:04:03 +01:00
|
|
|
$project_checker->checkDir(__DIR__);
|
2016-11-05 01:24:43 +01:00
|
|
|
} elseif ($paths_to_check === null) {
|
2017-05-04 20:25:58 +02:00
|
|
|
$project_checker->check($current_dir, $is_diff);
|
2016-11-05 01:13:16 +01:00
|
|
|
} elseif ($paths_to_check) {
|
|
|
|
foreach ($paths_to_check as $path_to_check) {
|
|
|
|
if (is_dir($path_to_check)) {
|
2018-01-02 02:04:03 +01:00
|
|
|
$project_checker->checkDir($path_to_check);
|
2016-11-05 01:13:16 +01:00
|
|
|
} else {
|
2018-01-02 02:04:03 +01:00
|
|
|
$project_checker->checkFile($path_to_check);
|
2016-07-26 21:12:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|