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
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
use Psalm\ErrorBaseline;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2018-11-12 17:13:15 +01:00
|
|
|
use Psalm\Internal\Provider;
|
2017-10-07 20:41:16 +02:00
|
|
|
use Psalm\Config;
|
2018-01-09 17:49:10 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2017-10-07 20:41:16 +02:00
|
|
|
|
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
|
|
|
|
2018-07-16 00:43:06 +02:00
|
|
|
$valid_short_options = [
|
|
|
|
'f:',
|
|
|
|
'm',
|
|
|
|
'h',
|
|
|
|
'v',
|
|
|
|
'c:',
|
|
|
|
'i',
|
|
|
|
'r:',
|
|
|
|
];
|
|
|
|
|
|
|
|
$valid_long_options = [
|
2018-07-20 13:07:41 +02:00
|
|
|
'clear-cache',
|
2018-10-15 17:29:57 +02:00
|
|
|
'clear-global-cache',
|
2018-07-20 13:07:41 +02:00
|
|
|
'config:',
|
|
|
|
'debug',
|
|
|
|
'debug-by-line',
|
|
|
|
'diff',
|
2018-10-30 15:32:20 +01:00
|
|
|
'diff-methods',
|
2018-07-20 13:07:41 +02:00
|
|
|
'disable-extension:',
|
2019-02-18 08:57:09 +01:00
|
|
|
'find-dead-code::',
|
2018-07-20 13:07:41 +02:00
|
|
|
'find-references-to:',
|
|
|
|
'help',
|
2018-10-30 15:32:20 +01:00
|
|
|
'ignore-baseline',
|
2018-07-20 13:07:41 +02:00
|
|
|
'init',
|
|
|
|
'monochrome',
|
|
|
|
'no-cache',
|
2018-11-12 17:13:15 +01:00
|
|
|
'no-reflection-cache',
|
2018-07-20 13:07:41 +02:00
|
|
|
'output-format:',
|
|
|
|
'plugin:',
|
|
|
|
'report:',
|
|
|
|
'root:',
|
2018-10-30 15:32:20 +01:00
|
|
|
'set-baseline:',
|
2018-07-20 13:07:41 +02:00
|
|
|
'show-info:',
|
|
|
|
'show-snippet:',
|
|
|
|
'stats',
|
|
|
|
'threads:',
|
2018-10-30 15:32:20 +01:00
|
|
|
'update-baseline',
|
2018-07-20 13:07:41 +02:00
|
|
|
'use-ini-defaults',
|
|
|
|
'version',
|
2019-02-07 18:25:57 +01:00
|
|
|
'php-version:',
|
2019-02-24 07:33:25 +01:00
|
|
|
'generate-json-map:',
|
2019-03-04 04:59:00 +01:00
|
|
|
'alter',
|
2019-03-23 17:49:37 +01:00
|
|
|
'with-spirit::',
|
2018-07-16 00:43:06 +02:00
|
|
|
];
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
gc_collect_cycles();
|
|
|
|
gc_disable();
|
|
|
|
|
2018-07-16 00:43:06 +02:00
|
|
|
$args = array_slice($argv, 1);
|
|
|
|
|
2019-03-04 04:59:00 +01:00
|
|
|
// get options from command line
|
|
|
|
$options = getopt(implode('', $valid_short_options), $valid_long_options);
|
|
|
|
|
|
|
|
if (isset($options['alter'])) {
|
|
|
|
include 'psalter.php';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-07-16 00:43:06 +02:00
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param string $arg
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function ($arg) use ($valid_long_options, $valid_short_options) {
|
|
|
|
if (substr($arg, 0, 2) === '--' && $arg !== '--') {
|
|
|
|
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
|
|
|
|
|
2019-02-18 08:57:09 +01:00
|
|
|
if (!in_array($arg_name, $valid_long_options)
|
|
|
|
&& !in_array($arg_name . ':', $valid_long_options)
|
|
|
|
&& !in_array($arg_name . '::', $valid_long_options)
|
|
|
|
) {
|
2018-07-16 00:43:06 +02:00
|
|
|
echo 'Unrecognised argument "--' . $arg_name . '"' . PHP_EOL
|
|
|
|
. 'Type --help to see a list of supported arguments'. PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} elseif (substr($arg, 0, 2) === '-' && $arg !== '-' && $arg !== '--') {
|
|
|
|
$arg_name = preg_replace('/=.*$/', '', substr($arg, 1));
|
|
|
|
|
|
|
|
if (!in_array($arg_name, $valid_short_options) && !in_array($arg_name . ':', $valid_short_options)) {
|
|
|
|
echo 'Unrecognised argument "-' . $arg_name . '"' . PHP_EOL
|
|
|
|
. 'Type --help to see a list of supported arguments'. PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$args
|
2016-12-08 04:38:57 +01:00
|
|
|
);
|
2016-10-18 23:00:03 +02:00
|
|
|
|
2018-06-27 16:40:02 +02:00
|
|
|
if (!array_key_exists('use-ini-defaults', $options)) {
|
2018-08-29 19:58:07 +02:00
|
|
|
ini_set('display_errors', '1');
|
|
|
|
ini_set('display_startup_errors', '1');
|
|
|
|
ini_set('memory_limit', (string) (4 * 1024 * 1024 * 1024));
|
2018-06-27 16:40:02 +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'])) {
|
2018-04-17 17:48:29 +02:00
|
|
|
echo 'Too many config files provided' . PHP_EOL;
|
|
|
|
exit(1);
|
2017-01-17 01:44:31 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 01:13:16 +01:00
|
|
|
if (array_key_exists('h', $options)) {
|
2017-11-28 22:29:51 +01:00
|
|
|
echo <<<HELP
|
2016-10-18 23:00:03 +02:00
|
|
|
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
|
|
|
|
|
2018-03-02 16:58:12 +01:00
|
|
|
-i, --init [source_dir=src] [level=3]
|
2017-02-13 05:59:33 +01:00
|
|
|
Create a psalm config file in the current directory that points to [source_dir]
|
2018-04-19 07:03:28 +02:00
|
|
|
at the required level, from 1, most strict, to 8, most permissive.
|
2017-02-13 05:59:33 +01:00
|
|
|
|
|
|
|
--debug
|
|
|
|
Debug information
|
|
|
|
|
2018-03-26 15:08:55 +02:00
|
|
|
--debug-by-line
|
|
|
|
Debug information on a line-by-line level
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
-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
|
|
|
|
|
2018-06-04 16:19:20 +02:00
|
|
|
--show-snippet[=true]
|
|
|
|
Show code snippets with errors. Options are 'true' or 'false'
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
--diff
|
|
|
|
Runs Psalm in diff mode, only checking files that have changed (and their dependents)
|
|
|
|
|
2018-10-07 06:42:25 +02:00
|
|
|
--diff-methods
|
|
|
|
Only checks methods that have changed (and their dependents)
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
--output-format=console
|
2018-12-09 21:47:20 +01:00
|
|
|
Changes the output format. Possible values: compact, console, emacs, json, pylint, xml
|
2017-02-13 05:59:33 +01:00
|
|
|
|
2019-02-18 08:57:09 +01:00
|
|
|
--find-dead-code[=auto]
|
|
|
|
Look for dead code. Options are 'auto' or 'always'. If no value is specified, default is 'auto'
|
2016-10-18 23:00:03 +02:00
|
|
|
|
2019-01-22 17:10:37 +01:00
|
|
|
--find-references-to=[class|method|property]
|
2017-02-27 07:30:44 +01:00
|
|
|
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.
|
2018-12-18 19:07:59 +01:00
|
|
|
(Currently supported format: ".json", ".xml", ".txt", ".emacs")
|
2017-09-08 17:18:48 +02:00
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
--clear-cache
|
2018-10-15 17:29:57 +02:00
|
|
|
Clears all cache files that Psalm uses for this specific project
|
|
|
|
|
|
|
|
--clear-global-cache
|
|
|
|
Clears all cache files that Psalm uses for all projects
|
2017-10-07 20:41:16 +02:00
|
|
|
|
2017-10-14 03:27:20 +02:00
|
|
|
--no-cache
|
|
|
|
Runs Psalm without using cache
|
|
|
|
|
2018-11-12 17:13:15 +01:00
|
|
|
--no-reflection-cache
|
|
|
|
Runs Psalm without using cached representations of unchanged classes and files.
|
|
|
|
Useful if you want the afterClassLikeVisit plugin hook to run every time you visit a file.
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
--plugin=PATH
|
|
|
|
Executes a plugin, an alternative to using the Psalm config
|
|
|
|
|
2018-01-31 23:09:09 +01:00
|
|
|
--stats
|
|
|
|
Shows a breakdown of Psalm's ability to infer types in the codebase
|
|
|
|
|
2018-06-27 16:40:02 +02:00
|
|
|
--use-ini-defaults
|
|
|
|
Use PHP-provided ini defaults for memory and error display
|
|
|
|
|
2018-07-17 22:54:34 +02:00
|
|
|
--disable-extension=[extension]
|
|
|
|
Used to disable certain extensions while Psalm is running.
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
--set-baseline=PATH
|
|
|
|
Save all current error level issues to a file, to mark them as info in subsequent runs
|
|
|
|
|
2018-11-01 16:15:08 +01:00
|
|
|
--ignore-baseline
|
2018-10-30 15:32:20 +01:00
|
|
|
Ignore the error baseline
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
--update-baseline
|
|
|
|
Update the baseline by removing fixed issues. This will not add new issues to the baseline
|
2018-07-17 22:54:34 +02:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
--generate-json-map=PATH
|
|
|
|
Generate a map of node references and types in JSON format, saved to the given path.
|
2019-03-04 04:59:00 +01:00
|
|
|
|
|
|
|
--alter
|
|
|
|
Run Psalter
|
2019-03-04 09:33:08 +01:00
|
|
|
|
2016-10-18 23:00:03 +02:00
|
|
|
HELP;
|
|
|
|
|
2019-03-23 17:49:37 +01:00
|
|
|
/*
|
|
|
|
--with-spirit[=host]
|
|
|
|
Send data to Spirit, Psalm's GitHub integration tool.
|
|
|
|
`host` is the location of the Spirit server. It defaults to spirit.psalm.dev
|
|
|
|
More information is available at https://psalm.dev/spirit
|
|
|
|
*/
|
|
|
|
|
2016-10-18 23:00:03 +02:00
|
|
|
exit;
|
|
|
|
}
|
2016-07-26 21:12:44 +02:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
if (getcwd() === false) {
|
2018-04-17 17:48:29 +02:00
|
|
|
echo 'Cannot get current working directory' . PHP_EOL;
|
|
|
|
exit(1);
|
2017-01-17 00:33:04 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-04-17 17:48:29 +02:00
|
|
|
echo 'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL;
|
|
|
|
exit(1);
|
2017-05-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = $root_path . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:32:54 +01:00
|
|
|
$vendor_dir = getVendorDir($current_dir);
|
|
|
|
|
2018-03-03 21:19:05 +01:00
|
|
|
$first_autoloader = requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-11-11 18:11:11 +01:00
|
|
|
if (array_key_exists('v', $options)) {
|
2018-02-25 17:35:43 +01:00
|
|
|
echo 'Psalm ' . PSALM_VERSION . PHP_EOL;
|
2017-11-11 18:11:11 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:08:34 +02:00
|
|
|
$threads = isset($options['threads']) ? (int)$options['threads'] : 1;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$ini_handler = new \Psalm\Internal\Fork\PsalmRestarter('PSALM');
|
2018-07-17 20:08:34 +02:00
|
|
|
|
2018-07-17 22:54:34 +02:00
|
|
|
if (isset($options['disable-extension'])) {
|
|
|
|
if (is_array($options['disable-extension'])) {
|
2018-07-23 01:15:40 +02:00
|
|
|
/** @psalm-suppress MixedAssignment */
|
2018-07-17 22:54:34 +02:00
|
|
|
foreach ($options['disable-extension'] as $extension) {
|
|
|
|
if (is_string($extension)) {
|
|
|
|
$ini_handler->disableExtension($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (is_string($options['disable-extension'])) {
|
|
|
|
$ini_handler->disableExtension($options['disable-extension']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:08:34 +02:00
|
|
|
if ($threads > 1) {
|
2018-07-17 22:40:03 +02:00
|
|
|
$ini_handler->disableExtension('grpc');
|
2018-07-17 20:08:34 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
$type_map_location = null;
|
|
|
|
|
|
|
|
if (isset($options['generate-type-map']) && is_string($options['generate-type-map'])) {
|
|
|
|
$type_map_location = $options['generate-type-map'];
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:41:16 +02:00
|
|
|
// If XDebug is enabled, restart without it
|
2018-07-17 20:08:34 +02:00
|
|
|
$ini_handler->check();
|
2017-10-07 20:41:16 +02:00
|
|
|
|
2018-02-18 04:11:42 +01:00
|
|
|
setlocale(LC_CTYPE, 'C');
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
if (isset($options['set-baseline'])) {
|
|
|
|
if (is_array($options['set-baseline'])) {
|
|
|
|
die('Only one baseline file can be created at a time' . PHP_EOL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:59:33 +01:00
|
|
|
if (isset($options['i'])) {
|
2018-03-19 00:16:35 +01:00
|
|
|
if (file_exists($current_dir . 'psalm.xml')) {
|
2017-02-13 05:59:33 +01:00
|
|
|
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(
|
2018-07-16 00:43:06 +02:00
|
|
|
$args,
|
2017-12-29 18:29:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $arg
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-10-14 03:16:34 +02:00
|
|
|
function ($arg) {
|
2018-03-19 00:16:35 +01:00
|
|
|
return $arg !== '--ansi'
|
|
|
|
&& $arg !== '--no-ansi'
|
2018-10-31 19:46:24 +01:00
|
|
|
&& $arg !== '-i'
|
2018-03-19 00:16:35 +01:00
|
|
|
&& $arg !== '--init'
|
|
|
|
&& strpos($arg, '--root=') !== 0
|
|
|
|
&& strpos($arg, '--r=') !== 0;
|
2017-10-14 03:16:34 +02:00
|
|
|
}
|
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])) {
|
2018-04-19 07:03:28 +02:00
|
|
|
if (!preg_match('/^[1-8]$/', $args[1])) {
|
|
|
|
die('Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL);
|
2017-02-13 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2018-08-09 06:03:32 +02:00
|
|
|
$template = str_replace(
|
|
|
|
'<directory name="src" />',
|
|
|
|
'<directory name="' . $source_dir . '" />',
|
|
|
|
$template
|
|
|
|
);
|
2017-02-13 05:59:33 +01:00
|
|
|
|
2018-03-19 00:16:35 +01:00
|
|
|
if (!file_put_contents($current_dir . 'psalm.xml', $template)) {
|
2017-02-13 05:59:33 +01:00
|
|
|
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']
|
2018-11-06 03:57:36 +01:00
|
|
|
: ProjectAnalyzer::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 */
|
2018-04-17 17:48:29 +02:00
|
|
|
echo 'Could not resolve path to config ' . (string)$options['c'] . PHP_EOL;
|
|
|
|
exit(1);
|
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
|
|
|
|
2019-02-18 08:57:09 +01:00
|
|
|
/** @var false|'always'|'auto' $find_dead_code */
|
|
|
|
$find_dead_code = false;
|
|
|
|
if (isset($options['find-dead-code'])) {
|
|
|
|
if ($options['find-dead-code'] === 'always') {
|
|
|
|
$find_dead_code = 'always';
|
|
|
|
} else {
|
|
|
|
$find_dead_code = 'auto';
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 02:47:16 +01:00
|
|
|
|
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
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
// initialise custom config, if passed
|
2018-04-19 23:29:07 +02:00
|
|
|
try {
|
|
|
|
if ($path_to_config) {
|
|
|
|
$config = Config::loadFromXMLFile($path_to_config, $current_dir);
|
|
|
|
} else {
|
|
|
|
$config = Config::getConfigForPath($current_dir, $current_dir, $output_format);
|
|
|
|
}
|
|
|
|
} catch (Psalm\Exception\ConfigException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
exit(1);
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-23 17:49:37 +01:00
|
|
|
if (isset($options['with-spirit'])) {
|
|
|
|
if (is_string($options['with-spirit'])) {
|
|
|
|
$config->spirit_host = $options['with-spirit'];
|
|
|
|
}
|
|
|
|
$spirit_plugin = __DIR__ . '/Psalm/Plugin/SpiritGuide.php';
|
|
|
|
|
|
|
|
if (!file_exists($spirit_plugin)) {
|
|
|
|
die('Could not find Spirit plugin location ' . $spirit_plugin . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugins[] = $spirit_plugin;
|
|
|
|
}
|
|
|
|
|
2018-03-03 21:19:05 +01:00
|
|
|
$config->setComposerClassLoader($first_autoloader);
|
|
|
|
|
2018-02-08 02:15:56 +01:00
|
|
|
if (isset($options['clear-cache'])) {
|
|
|
|
$cache_directory = $config->getCacheDirectory();
|
|
|
|
|
|
|
|
Config::removeCacheDirectory($cache_directory);
|
|
|
|
echo 'Cache directory deleted' . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:29:57 +02:00
|
|
|
if (isset($options['clear-global-cache'])) {
|
|
|
|
$cache_directory = $config->getGlobalCacheDirectory();
|
|
|
|
|
|
|
|
Config::removeCacheDirectory($cache_directory);
|
|
|
|
echo 'Global cache directory deleted' . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
|
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
if (isset($options['no-cache'])) {
|
2018-11-12 17:13:15 +01:00
|
|
|
$providers = new Provider\Providers(
|
|
|
|
new Provider\FileProvider
|
2018-09-28 22:18:45 +02:00
|
|
|
);
|
|
|
|
} else {
|
2018-11-12 17:13:15 +01:00
|
|
|
$no_reflection_cache = isset($options['no-reflection-cache']);
|
|
|
|
|
|
|
|
$file_storage_cache_provider = $no_reflection_cache
|
|
|
|
? null
|
|
|
|
: new Provider\FileStorageCacheProvider($config);
|
|
|
|
|
|
|
|
$classlike_storage_cache_provider = $no_reflection_cache
|
|
|
|
? null
|
|
|
|
: new Provider\ClassLikeStorageCacheProvider($config);
|
|
|
|
|
|
|
|
$providers = new Provider\Providers(
|
|
|
|
new Provider\FileProvider,
|
|
|
|
new Provider\ParserCacheProvider($config),
|
|
|
|
$file_storage_cache_provider,
|
|
|
|
$classlike_storage_cache_provider,
|
|
|
|
new Provider\FileReferenceCacheProvider($config)
|
2018-09-28 22:18:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers,
|
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-06-30 21:29:37 +02:00
|
|
|
$debug,
|
2018-06-04 16:19:20 +02:00
|
|
|
isset($options['report']) && is_string($options['report']) ? $options['report'] : null,
|
|
|
|
!isset($options['show-snippet']) || $options['show-snippet'] !== "false"
|
2017-02-01 02:47:16 +01:00
|
|
|
);
|
2017-01-16 17:05:29 +01:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if (isset($options['php-version'])) {
|
|
|
|
if (!is_string($options['php-version'])) {
|
|
|
|
die('Expecting a version number in the format x.y' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$project_analyzer->setPhpVersion($options['php-version']);
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->getCodebase()->diff_methods = isset($options['diff-methods']);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($type_map_location) {
|
|
|
|
$project_analyzer->getCodebase()->store_node_types = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-10 22:05:06 +02:00
|
|
|
$start_time = microtime(true);
|
2018-09-25 22:35:23 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer, $debug);
|
2018-03-31 02:03:56 +02:00
|
|
|
|
2018-10-10 22:05:06 +02:00
|
|
|
$now_time = microtime(true);
|
2018-09-25 22:35:23 +02:00
|
|
|
|
|
|
|
if ($debug) {
|
2018-10-26 22:17:15 +02:00
|
|
|
echo 'Visiting autoload files took ' . number_format($now_time - $start_time, 3) . 's' . "\n";
|
2018-09-25 22:35:23 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 15:08:55 +02:00
|
|
|
if (array_key_exists('debug-by-line', $options)) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->debug_lines = true;
|
2018-03-26 15:08:55 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:09 +01:00
|
|
|
if ($config->find_unused_code) {
|
|
|
|
$find_dead_code = 'auto';
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if ($find_dead_code || $find_references_to !== null) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->getCodebase()->collectReferences();
|
2018-03-16 16:15:01 +01:00
|
|
|
|
|
|
|
if ($find_references_to) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->show_issues = false;
|
2018-03-16 16:15:01 +01:00
|
|
|
}
|
2018-02-04 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-17 23:45:30 +01:00
|
|
|
if ($find_dead_code) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->getCodebase()->reportUnusedCode();
|
2018-02-17 23:45:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:09 +01:00
|
|
|
if ($config->find_unused_variables) {
|
|
|
|
$project_analyzer->getCodebase()->reportUnusedVariables();
|
|
|
|
}
|
|
|
|
|
2018-01-02 02:05:54 +01:00
|
|
|
/** @var string $plugin_path */
|
|
|
|
foreach ($plugins as $plugin_path) {
|
2019-03-23 17:49:37 +01:00
|
|
|
$config->addPluginPath($plugin_path);
|
2018-01-02 02:05:54 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 20:34:46 +02:00
|
|
|
if ($paths_to_check === null) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->check($current_dir, $is_diff);
|
2016-11-05 01:13:16 +01:00
|
|
|
} elseif ($paths_to_check) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->checkPaths($paths_to_check);
|
2016-07-26 21:12:44 +02:00
|
|
|
}
|
2018-01-09 17:49:10 +01:00
|
|
|
|
2018-01-22 05:42:57 +01:00
|
|
|
if ($find_references_to) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->findReferencesTo($find_references_to);
|
2019-02-18 08:57:09 +01:00
|
|
|
} elseif (($find_dead_code === 'always') || ($find_dead_code === 'auto' && !$paths_to_check && !$is_diff)) {
|
2018-01-25 21:36:02 +01:00
|
|
|
if ($threads > 1) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($output_format === ProjectAnalyzer::TYPE_CONSOLE) {
|
2018-01-25 21:40:01 +01:00
|
|
|
echo 'Unused classes and methods cannot currently be found in multithreaded mode' . PHP_EOL;
|
2018-01-25 21:36:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->checkClassReferences();
|
2018-01-25 21:36:02 +01:00
|
|
|
}
|
2018-01-22 05:42:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
|
|
|
|
echo 'Writing error baseline to file...', PHP_EOL;
|
|
|
|
|
|
|
|
ErrorBaseline::create(
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
2018-10-30 15:32:20 +01:00
|
|
|
$options['set-baseline'],
|
|
|
|
IssueBuffer::getIssuesData()
|
|
|
|
);
|
|
|
|
|
|
|
|
echo "Baseline saved to {$options['set-baseline']}.";
|
|
|
|
|
2018-11-02 20:56:42 +01:00
|
|
|
/** @var string $configFile */
|
|
|
|
$configFile = Config::locateConfigFile($path_to_config ?? $current_dir);
|
|
|
|
$configFileContents = $amendedConfigFileContents = file_get_contents($configFile);
|
|
|
|
|
|
|
|
if ($config->error_baseline) {
|
|
|
|
$amendedConfigFileContents = preg_replace(
|
|
|
|
'/errorBaseline=".*?"/',
|
|
|
|
"errorBaseline=\"{$options['set-baseline']}\"",
|
|
|
|
$configFileContents
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$endPsalmOpenTag = strpos($configFileContents, '>', (int)strpos($configFileContents, '<psalm'));
|
|
|
|
|
|
|
|
if (!$endPsalmOpenTag) {
|
|
|
|
echo " Don't forget to set errorBaseline=\"{$options['set-baseline']}\" in your config.";
|
|
|
|
} elseif ($configFileContents[$endPsalmOpenTag - 1] === "\n") {
|
|
|
|
$amendedConfigFileContents = substr_replace(
|
|
|
|
$configFileContents,
|
|
|
|
" errorBaseline=\"{$options['set-baseline']}\"\n>",
|
|
|
|
$endPsalmOpenTag,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$amendedConfigFileContents = substr_replace(
|
|
|
|
$configFileContents,
|
|
|
|
" errorBaseline=\"{$options['set-baseline']}\">",
|
|
|
|
$endPsalmOpenTag,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 20:56:42 +01:00
|
|
|
file_put_contents($configFile, $amendedConfigFileContents);
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
echo PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$issue_baseline = [];
|
|
|
|
|
|
|
|
if (isset($options['update-baseline'])) {
|
|
|
|
$baselineFile = Config::getInstance()->error_baseline;
|
|
|
|
|
|
|
|
if (empty($baselineFile)) {
|
|
|
|
die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-02-09 12:13:58 +01:00
|
|
|
$issue_current_baseline = ErrorBaseline::read(
|
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
2019-02-09 17:18:55 +01:00
|
|
|
$baselineFile
|
2019-02-09 12:13:58 +01:00
|
|
|
);
|
|
|
|
$total_issues_current_baseline = ErrorBaseline::countTotalIssues($issue_current_baseline);
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
$issue_baseline = ErrorBaseline::update(
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
2018-10-30 15:32:20 +01:00
|
|
|
$baselineFile,
|
|
|
|
IssueBuffer::getIssuesData()
|
|
|
|
);
|
2019-02-09 12:13:58 +01:00
|
|
|
$total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline);
|
|
|
|
|
|
|
|
$total_fixed_issues = $total_issues_current_baseline - $total_issues_updated_baseline;
|
|
|
|
|
|
|
|
if ($total_fixed_issues > 0) {
|
|
|
|
echo str_repeat('-', 30) . "\n";
|
|
|
|
echo $total_fixed_issues . ' errors fixed' . "\n";
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
} catch (\Psalm\Exception\ConfigException $exception) {
|
|
|
|
die('Could not update baseline file: ' . $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty(Config::getInstance()->error_baseline) && !isset($options['ignore-baseline'])) {
|
|
|
|
try {
|
|
|
|
$issue_baseline = ErrorBaseline::read(
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
2018-10-30 15:32:20 +01:00
|
|
|
(string)Config::getInstance()->error_baseline
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\ConfigException $exception) {
|
|
|
|
die('Error while reading baseline: ' . $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($type_map_location) {
|
|
|
|
$file_map = $providers->file_reference_provider->getFileMaps();
|
|
|
|
|
|
|
|
$name_file_map = [];
|
|
|
|
|
|
|
|
$expected_references = [];
|
|
|
|
|
|
|
|
foreach ($file_map as $file_path => $map) {
|
|
|
|
$file_name = $config->shortenFileName($file_path);
|
2019-02-24 07:47:53 +01:00
|
|
|
foreach ($map[0] as $map_parts) {
|
|
|
|
$expected_references[$map_parts[1]] = true;
|
2019-02-24 07:33:25 +01:00
|
|
|
}
|
|
|
|
$map[2] = [];
|
|
|
|
$name_file_map[$file_name] = $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference_dictionary = [];
|
|
|
|
|
|
|
|
foreach ($providers->classlike_storage_provider->getAll() as $storage) {
|
|
|
|
if (!$storage->location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_classlike_name = $storage->name;
|
|
|
|
|
|
|
|
if (isset($expected_references[$fq_classlike_name])) {
|
|
|
|
$reference_dictionary[$fq_classlike_name]
|
|
|
|
= $storage->location->file_name
|
|
|
|
. ':' . $storage->location->getLineNumber()
|
|
|
|
. ':' . $storage->location->getColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($storage->methods as $method_name => $method_storage) {
|
|
|
|
if (!$method_storage->location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($expected_references[$fq_classlike_name . '::' . $method_name . '()'])) {
|
|
|
|
$reference_dictionary[$fq_classlike_name . '::' . $method_name . '()']
|
|
|
|
= $method_storage->location->file_name
|
|
|
|
. ':' . $method_storage->location->getLineNumber()
|
|
|
|
. ':' . $method_storage->location->getColumn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($storage->properties as $property_name => $property_storage) {
|
|
|
|
if (!$property_storage->location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($expected_references[$fq_classlike_name . '::$' . $property_name])) {
|
|
|
|
$reference_dictionary[$fq_classlike_name . '::$' . $property_name]
|
|
|
|
= $property_storage->location->file_name
|
|
|
|
. ':' . $property_storage->location->getLineNumber()
|
|
|
|
. ':' . $property_storage->location->getColumn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$providers->file_provider->setContents(
|
|
|
|
$type_map_location,
|
|
|
|
json_encode(['files' => $name_file_map, 'references' => $reference_dictionary])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
IssueBuffer::finish(
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer,
|
2018-10-30 15:32:20 +01:00
|
|
|
!$paths_to_check,
|
|
|
|
$start_time,
|
|
|
|
isset($options['stats']),
|
|
|
|
$issue_baseline
|
|
|
|
);
|