2016-07-26 15:12:44 -04:00
|
|
|
<?php
|
2018-01-07 00:11:23 -05:00
|
|
|
require_once('command_functions.php');
|
2016-07-26 15:12:44 -04:00
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
use Psalm\ErrorBaseline;
|
2018-11-05 21:57:36 -05:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2018-11-12 11:13:15 -05:00
|
|
|
use Psalm\Internal\Provider;
|
2017-10-07 14:41:16 -04:00
|
|
|
use Psalm\Config;
|
2018-01-09 11:49:10 -05:00
|
|
|
use Psalm\IssueBuffer;
|
2019-05-30 16:30:41 +02:00
|
|
|
use Psalm\Progress\DebugProgress;
|
|
|
|
use Psalm\Progress\DefaultProgress;
|
2019-11-22 08:56:18 -05:00
|
|
|
use Psalm\Progress\LongProgress;
|
2019-05-30 16:30:41 +02:00
|
|
|
use Psalm\Progress\VoidProgress;
|
2017-10-07 14:41:16 -04:00
|
|
|
|
2016-07-26 15:12:44 -04:00
|
|
|
// show all errors
|
2016-10-30 12:46:18 -04:00
|
|
|
error_reporting(-1);
|
2016-07-26 15:12:44 -04:00
|
|
|
|
2018-07-15 18:43:06 -04:00
|
|
|
$valid_short_options = [
|
|
|
|
'f:',
|
|
|
|
'm',
|
|
|
|
'h',
|
|
|
|
'v',
|
|
|
|
'c:',
|
|
|
|
'i',
|
|
|
|
'r:',
|
|
|
|
];
|
|
|
|
|
|
|
|
$valid_long_options = [
|
2018-07-20 04:07:41 -07:00
|
|
|
'clear-cache',
|
2018-10-15 11:29:57 -04:00
|
|
|
'clear-global-cache',
|
2018-07-20 04:07:41 -07:00
|
|
|
'config:',
|
|
|
|
'debug',
|
|
|
|
'debug-by-line',
|
|
|
|
'diff',
|
2018-10-30 15:32:20 +01:00
|
|
|
'diff-methods',
|
2018-07-20 04:07:41 -07:00
|
|
|
'disable-extension:',
|
2019-02-18 09:57:09 +02:00
|
|
|
'find-dead-code::',
|
2019-04-30 13:23:18 -04:00
|
|
|
'find-unused-code::',
|
2019-11-17 14:49:28 -05:00
|
|
|
'find-unused-variables',
|
2018-07-20 04:07:41 -07:00
|
|
|
'find-references-to:',
|
|
|
|
'help',
|
2018-10-30 15:32:20 +01:00
|
|
|
'ignore-baseline',
|
2018-07-20 04:07:41 -07:00
|
|
|
'init',
|
|
|
|
'monochrome',
|
|
|
|
'no-cache',
|
2018-11-12 11:13:15 -05:00
|
|
|
'no-reflection-cache',
|
2018-07-20 04:07:41 -07:00
|
|
|
'output-format:',
|
|
|
|
'plugin:',
|
|
|
|
'report:',
|
2019-06-09 12:37:28 -04:00
|
|
|
'report-show-info:',
|
2018-07-20 04:07:41 -07:00
|
|
|
'root:',
|
2018-10-30 15:32:20 +01:00
|
|
|
'set-baseline:',
|
2018-07-20 04:07:41 -07:00
|
|
|
'show-info:',
|
|
|
|
'show-snippet:',
|
|
|
|
'stats',
|
|
|
|
'threads:',
|
2018-10-30 15:32:20 +01:00
|
|
|
'update-baseline',
|
2018-07-20 04:07:41 -07:00
|
|
|
'use-ini-defaults',
|
|
|
|
'version',
|
2019-02-07 12:25:57 -05:00
|
|
|
'php-version:',
|
2019-02-24 01:33:25 -05:00
|
|
|
'generate-json-map:',
|
2020-01-08 17:23:40 -05:00
|
|
|
'generate-stubs:',
|
2019-03-03 22:59:00 -05:00
|
|
|
'alter',
|
2019-03-27 11:55:10 -04:00
|
|
|
'language-server',
|
2019-06-02 01:10:50 -04:00
|
|
|
'refactor',
|
2019-03-31 14:02:30 -04:00
|
|
|
'shepherd::',
|
2019-05-30 16:30:41 +02:00
|
|
|
'no-progress',
|
2019-11-22 08:56:18 -05:00
|
|
|
'long-progress',
|
2019-12-02 15:24:01 -05:00
|
|
|
'no-suggestions',
|
2019-07-11 10:41:44 -04:00
|
|
|
'include-php-versions', // used for baseline
|
2019-08-18 14:27:50 -04:00
|
|
|
'track-tainted-input',
|
|
|
|
'find-unused-psalm-suppress',
|
2018-07-15 18:43:06 -04:00
|
|
|
];
|
|
|
|
|
2018-11-18 11:39:14 -05:00
|
|
|
gc_collect_cycles();
|
|
|
|
gc_disable();
|
|
|
|
|
2018-07-15 18:43:06 -04:00
|
|
|
$args = array_slice($argv, 1);
|
|
|
|
|
2019-03-03 22:59:00 -05:00
|
|
|
// get options from command line
|
|
|
|
$options = getopt(implode('', $valid_short_options), $valid_long_options);
|
|
|
|
|
|
|
|
if (isset($options['alter'])) {
|
2020-02-07 09:06:15 -05:00
|
|
|
require_once __DIR__ . '/psalter.php';
|
2019-03-03 22:59:00 -05:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-03-27 11:55:10 -04:00
|
|
|
if (isset($options['language-server'])) {
|
2020-02-07 09:06:15 -05:00
|
|
|
require_once __DIR__ . '/psalm-language-server.php';
|
2019-03-27 11:55:10 -04:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-06-02 01:10:50 -04:00
|
|
|
if (isset($options['refactor'])) {
|
2020-02-07 09:06:15 -05:00
|
|
|
require_once __DIR__ . '/psalm-refactor.php';
|
2019-06-02 01:10:50 -04:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:17:08 -05:00
|
|
|
require_once __DIR__ . '/Psalm/Internal/exception_handler.php';
|
|
|
|
|
2018-07-15 18:43:06 -04: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 09:57:09 +02:00
|
|
|
if (!in_array($arg_name, $valid_long_options)
|
|
|
|
&& !in_array($arg_name . ':', $valid_long_options)
|
|
|
|
&& !in_array($arg_name . '::', $valid_long_options)
|
|
|
|
) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(
|
|
|
|
STDERR,
|
|
|
|
'Unrecognised argument "--' . $arg_name . '"' . PHP_EOL
|
|
|
|
. 'Type --help to see a list of supported arguments'. PHP_EOL
|
|
|
|
);
|
2018-07-15 18:43:06 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-10-29 04:55:34 -04:00
|
|
|
} elseif (substr($arg, 0, 1) === '-' && $arg !== '-' && $arg !== '--') {
|
2018-07-15 18:43:06 -04:00
|
|
|
$arg_name = preg_replace('/=.*$/', '', substr($arg, 1));
|
|
|
|
|
|
|
|
if (!in_array($arg_name, $valid_short_options) && !in_array($arg_name . ':', $valid_short_options)) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(
|
|
|
|
STDERR,
|
|
|
|
'Unrecognised argument "-' . $arg_name . '"' . PHP_EOL
|
|
|
|
. 'Type --help to see a list of supported arguments'. PHP_EOL
|
|
|
|
);
|
2018-07-15 18:43:06 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$args
|
2016-12-07 22:38:57 -05:00
|
|
|
);
|
2016-10-18 17:00:03 -04:00
|
|
|
|
2018-06-27 10:40:02 -04:00
|
|
|
if (!array_key_exists('use-ini-defaults', $options)) {
|
2018-08-29 13:58:07 -04:00
|
|
|
ini_set('display_errors', '1');
|
|
|
|
ini_set('display_startup_errors', '1');
|
2019-08-04 10:37:36 -04:00
|
|
|
ini_set('memory_limit', (string) (8 * 1024 * 1024 * 1024));
|
2018-06-27 10:40:02 -04:00
|
|
|
}
|
|
|
|
|
2016-11-04 20:13:16 -04:00
|
|
|
if (array_key_exists('help', $options)) {
|
2016-11-04 22:55:13 -04:00
|
|
|
$options['h'] = false;
|
2016-11-04 20:13:16 -04:00
|
|
|
}
|
|
|
|
|
2017-11-11 12:11:11 -05:00
|
|
|
if (array_key_exists('version', $options)) {
|
|
|
|
$options['v'] = false;
|
|
|
|
}
|
|
|
|
|
2017-02-12 23:59:33 -05:00
|
|
|
if (array_key_exists('init', $options)) {
|
|
|
|
$options['i'] = false;
|
|
|
|
}
|
|
|
|
|
2016-11-04 20:13:16 -04:00
|
|
|
if (array_key_exists('monochrome', $options)) {
|
2016-11-04 22:55:13 -04:00
|
|
|
$options['m'] = false;
|
2016-11-04 20:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['config'])) {
|
2016-11-04 22:55:13 -04:00
|
|
|
$options['c'] = $options['config'];
|
2016-11-04 20:13:16 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 19:44:31 -05:00
|
|
|
if (isset($options['c']) && is_array($options['c'])) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Too many config files provided' . PHP_EOL);
|
2018-04-17 11:48:29 -04:00
|
|
|
exit(1);
|
2017-01-16 19:44:31 -05:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:00:03 -04:00
|
|
|
|
2019-07-07 13:55:53 +01:00
|
|
|
if (array_key_exists('h', $options)) {
|
|
|
|
echo getPsalmHelpText();
|
2019-03-23 12:49:37 -04:00
|
|
|
/*
|
2019-03-31 14:02:30 -04:00
|
|
|
--shepherd[=host]
|
|
|
|
Send data to Shepherd, Psalm's GitHub integration tool.
|
|
|
|
`host` is the location of the Shepherd server. It defaults to shepherd.dev
|
|
|
|
More information is available at https://psalm.dev/shepherd
|
2019-03-23 12:49:37 -04:00
|
|
|
*/
|
|
|
|
|
2016-10-18 17:00:03 -04:00
|
|
|
exit;
|
|
|
|
}
|
2016-07-26 15:12:44 -04:00
|
|
|
|
2017-01-16 18:33:04 -05:00
|
|
|
if (getcwd() === false) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Cannot get current working directory' . PHP_EOL);
|
2018-04-17 11:48:29 -04:00
|
|
|
exit(1);
|
2017-01-16 18:33:04 -05:00
|
|
|
}
|
|
|
|
|
2017-05-04 14:25:58 -04:00
|
|
|
if (isset($options['root'])) {
|
|
|
|
$options['r'] = $options['root'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = (string)getcwd() . DIRECTORY_SEPARATOR;
|
|
|
|
|
2019-08-11 21:12:18 +02:00
|
|
|
if (isset($options['r']) && is_string($options['r'])) {
|
|
|
|
$root_path = realpath($options['r']);
|
|
|
|
|
|
|
|
if (!$root_path) {
|
|
|
|
fwrite(
|
|
|
|
STDERR,
|
|
|
|
'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = $root_path . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2019-07-07 13:55:53 +01:00
|
|
|
$path_to_config = get_path_to_config($options);
|
2019-07-06 17:21:39 +01:00
|
|
|
|
|
|
|
$vendor_dir = getVendorDir($current_dir);
|
|
|
|
|
|
|
|
$first_autoloader = requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
|
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
if (array_key_exists('v', $options)) {
|
|
|
|
echo 'Psalm ' . PSALM_VERSION . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-07-06 17:21:39 +01:00
|
|
|
$output_format = isset($options['output-format']) && is_string($options['output-format'])
|
|
|
|
? $options['output-format']
|
|
|
|
: \Psalm\Report::TYPE_CONSOLE;
|
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
$level = null;
|
|
|
|
$inferred_source_dir = null;
|
|
|
|
|
2019-07-06 17:21:39 +01:00
|
|
|
if (isset($options['i'])) {
|
|
|
|
if (file_exists($current_dir . 'psalm.xml')) {
|
|
|
|
die('A config file already exists in the current directory' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = array_values(array_filter(
|
|
|
|
$args,
|
|
|
|
/**
|
|
|
|
* @param string $arg
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function ($arg) {
|
|
|
|
return $arg !== '--ansi'
|
|
|
|
&& $arg !== '--no-ansi'
|
|
|
|
&& $arg !== '-i'
|
|
|
|
&& $arg !== '--init'
|
2020-02-17 16:33:28 -05:00
|
|
|
&& $arg !== '--debug'
|
|
|
|
&& $arg !== '--debug-by-line'
|
2019-07-06 17:21:39 +01:00
|
|
|
&& strpos($arg, '--disable-extension=') !== 0
|
|
|
|
&& strpos($arg, '--root=') !== 0
|
|
|
|
&& strpos($arg, '--r=') !== 0;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
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-8]$/', $args[1])) {
|
|
|
|
die('Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$level = (int)$args[1];
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
$inferred_source_dir = $args[0];
|
2019-07-06 17:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 12:59:04 +01:00
|
|
|
$vendor_dir = getVendorDir($current_dir);
|
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
if ($level === null) {
|
|
|
|
echo "Calculating best config level based on project files\n";
|
|
|
|
Psalm\Config\Creator::createBareConfig($current_dir, $inferred_source_dir, $vendor_dir);
|
|
|
|
$config = \Psalm\Config::getInstance();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$template_contents = Psalm\Config\Creator::getContents(
|
|
|
|
$current_dir,
|
|
|
|
$inferred_source_dir,
|
|
|
|
$level,
|
|
|
|
$vendor_dir
|
|
|
|
);
|
|
|
|
} catch (Psalm\Exception\ConfigCreationException $e) {
|
|
|
|
die($e->getMessage() . PHP_EOL);
|
|
|
|
}
|
2019-07-06 17:21:39 +01:00
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
if (!file_put_contents($current_dir . 'psalm.xml', $template_contents)) {
|
|
|
|
die('Could not write to psalm.xml' . PHP_EOL);
|
|
|
|
}
|
2019-07-06 17:21:39 +01:00
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$config = initialiseConfig($path_to_config, $current_dir, $output_format, $first_autoloader);
|
2019-07-06 17:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($config->resolve_from_config_file) {
|
|
|
|
$current_dir = $config->base_dir;
|
2019-07-07 13:55:53 +01:00
|
|
|
chdir($current_dir);
|
2019-07-06 17:21:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 17:48:01 -05:00
|
|
|
$in_ci = isset($_SERVER['TRAVIS'])
|
|
|
|
|| isset($_SERVER['CIRCLECI'])
|
|
|
|
|| isset($_SERVER['APPVEYOR'])
|
|
|
|
|| isset($_SERVER['JENKINS_URL'])
|
|
|
|
|| isset($_SERVER['SCRUTINIZER'])
|
|
|
|
|| isset($_SERVER['GITLAB_CI'])
|
|
|
|
|| isset($_SERVER['GITHUB_WORKFLOW']);
|
|
|
|
|
|
|
|
// disable progressbar on CI
|
|
|
|
if ($in_ci) {
|
|
|
|
$options['long-progress'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['threads'])) {
|
|
|
|
$threads = (int)$options['threads'];
|
|
|
|
} elseif (isset($options['debug']) || $in_ci) {
|
|
|
|
$threads = 1;
|
|
|
|
} else {
|
|
|
|
$threads = max(1, ProjectAnalyzer::getCpuCount() - 2);
|
|
|
|
}
|
2018-07-17 14:08:34 -04:00
|
|
|
|
2020-02-17 17:48:01 -05:00
|
|
|
if (!isset($options['threads'])
|
|
|
|
&& !isset($options['debug'])
|
|
|
|
&& $threads === 1
|
2019-06-27 10:51:13 -04:00
|
|
|
&& ini_get('pcre.jit') === '1'
|
|
|
|
&& PHP_OS === 'Darwin'
|
|
|
|
&& version_compare(PHP_VERSION, '7.3.0') >= 0
|
2019-11-30 08:26:26 -05:00
|
|
|
&& version_compare(PHP_VERSION, '7.4.0') < 0
|
2019-06-27 10:51:13 -04:00
|
|
|
) {
|
|
|
|
echo(
|
|
|
|
'If you want to run Psalm as a language server, or run Psalm with' . PHP_EOL
|
|
|
|
. 'multiple processes (--threads=4), beware:' . PHP_EOL
|
|
|
|
. \Psalm\Internal\Fork\Pool::MAC_PCRE_MESSAGE . PHP_EOL . PHP_EOL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:57:36 -05:00
|
|
|
$ini_handler = new \Psalm\Internal\Fork\PsalmRestarter('PSALM');
|
2018-07-17 14:08:34 -04:00
|
|
|
|
2018-07-17 16:54:34 -04:00
|
|
|
if (isset($options['disable-extension'])) {
|
|
|
|
if (is_array($options['disable-extension'])) {
|
2018-07-22 19:15:40 -04:00
|
|
|
/** @psalm-suppress MixedAssignment */
|
2018-07-17 16:54:34 -04: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 14:08:34 -04:00
|
|
|
if ($threads > 1) {
|
2018-07-17 16:40:03 -04:00
|
|
|
$ini_handler->disableExtension('grpc');
|
2018-07-17 14:08:34 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 10:36:26 -04:00
|
|
|
$ini_handler->disableExtension('uopz');
|
|
|
|
|
2019-02-24 01:33:25 -05:00
|
|
|
$type_map_location = null;
|
|
|
|
|
2019-04-21 11:30:42 -04:00
|
|
|
if (isset($options['generate-json-map']) && is_string($options['generate-json-map'])) {
|
|
|
|
$type_map_location = $options['generate-json-map'];
|
2019-02-24 01:33:25 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 17:23:40 -05:00
|
|
|
$stubs_location = null;
|
|
|
|
|
|
|
|
if (isset($options['generate-stubs']) && is_string($options['generate-stubs'])) {
|
|
|
|
$stubs_location = $options['generate-stubs'];
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:13:39 +01:00
|
|
|
// If Xdebug is enabled, restart without it
|
2018-07-17 14:08:34 -04:00
|
|
|
$ini_handler->check();
|
2017-10-07 14:41:16 -04:00
|
|
|
|
2019-09-14 15:13:39 +01:00
|
|
|
if (is_null($config->load_xdebug_stub) && '' !== $ini_handler->getSkippedVersion()) {
|
|
|
|
$config->load_xdebug_stub = true;
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:11:42 -05: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 00:11:23 -05:00
|
|
|
$paths_to_check = getPathsToCheck(isset($options['f']) ? $options['f'] : null);
|
2016-11-04 20:13:16 -04:00
|
|
|
|
2018-01-01 20:05:54 -05:00
|
|
|
$plugins = [];
|
|
|
|
|
|
|
|
if (isset($options['plugin'])) {
|
|
|
|
$plugins = $options['plugin'];
|
|
|
|
|
|
|
|
if (!is_array($plugins)) {
|
|
|
|
$plugins = [$plugins];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:55:13 -04:00
|
|
|
|
2017-01-16 11:41:57 -05:00
|
|
|
|
2016-08-04 14:38:43 -04:00
|
|
|
$show_info = isset($options['show-info'])
|
2020-02-17 16:33:28 -05:00
|
|
|
? $options['show-info'] === 'true' || $options['show-info'] === '1'
|
|
|
|
: false;
|
2016-11-04 22:55:13 -04:00
|
|
|
|
2016-10-07 00:58:08 -04:00
|
|
|
$is_diff = isset($options['diff']);
|
2016-07-26 15:12:44 -04:00
|
|
|
|
2019-04-12 18:28:07 -04:00
|
|
|
/** @var false|'always'|'auto' $find_unused_code */
|
|
|
|
$find_unused_code = false;
|
2019-02-18 09:57:09 +02:00
|
|
|
if (isset($options['find-dead-code'])) {
|
2019-04-30 13:23:18 -04:00
|
|
|
$options['find-unused-code'] = $options['find-dead-code'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['find-unused-code'])) {
|
|
|
|
if ($options['find-unused-code'] === 'always') {
|
2019-04-12 18:28:07 -04:00
|
|
|
$find_unused_code = 'always';
|
2019-02-18 09:57:09 +02:00
|
|
|
} else {
|
2019-04-12 18:28:07 -04:00
|
|
|
$find_unused_code = 'auto';
|
2019-02-18 09:57:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-31 20:47:16 -05:00
|
|
|
|
2019-11-17 14:49:28 -05:00
|
|
|
$find_unused_variables = isset($options['find-unused-variables']);
|
|
|
|
|
2017-12-29 12:29:36 -05:00
|
|
|
$find_references_to = isset($options['find-references-to']) && is_string($options['find-references-to'])
|
|
|
|
? $options['find-references-to']
|
|
|
|
: null;
|
2017-02-27 01:30:44 -05:00
|
|
|
|
2019-07-06 17:21:39 +01:00
|
|
|
|
2018-01-21 10:22:04 -05:00
|
|
|
|
2019-03-31 14:02:30 -04:00
|
|
|
if (isset($options['shepherd'])) {
|
|
|
|
if (is_string($options['shepherd'])) {
|
|
|
|
$config->shepherd_host = $options['shepherd'];
|
2019-03-23 12:49:37 -04:00
|
|
|
}
|
2019-03-31 14:02:30 -04:00
|
|
|
$shepherd_plugin = __DIR__ . '/Psalm/Plugin/Shepherd.php';
|
2019-03-23 12:49:37 -04:00
|
|
|
|
2019-03-31 14:02:30 -04:00
|
|
|
if (!file_exists($shepherd_plugin)) {
|
|
|
|
die('Could not find Shepherd plugin location ' . $shepherd_plugin . PHP_EOL);
|
2019-03-23 12:49:37 -04:00
|
|
|
}
|
|
|
|
|
2019-03-31 14:02:30 -04:00
|
|
|
$plugins[] = $shepherd_plugin;
|
2019-03-23 12:49:37 -04:00
|
|
|
}
|
|
|
|
|
2018-02-07 20:15:56 -05:00
|
|
|
if (isset($options['clear-cache'])) {
|
|
|
|
$cache_directory = $config->getCacheDirectory();
|
|
|
|
|
|
|
|
Config::removeCacheDirectory($cache_directory);
|
|
|
|
echo 'Cache directory deleted' . PHP_EOL;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-10-15 11:29:57 -04:00
|
|
|
if (isset($options['clear-global-cache'])) {
|
|
|
|
$cache_directory = $config->getGlobalCacheDirectory();
|
|
|
|
|
2019-04-14 13:04:25 -04:00
|
|
|
if ($cache_directory) {
|
|
|
|
Config::removeCacheDirectory($cache_directory);
|
|
|
|
echo 'Global cache directory deleted' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2018-10-15 11:29:57 -04:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-06-30 15:29:37 -04:00
|
|
|
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
|
2019-10-02 03:28:42 +03:00
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
$progress = new DebugProgress();
|
|
|
|
} elseif (isset($options['no-progress'])) {
|
|
|
|
$progress = new VoidProgress();
|
|
|
|
} else {
|
|
|
|
$show_errors = !$config->error_baseline || isset($options['ignore-baseline']);
|
2019-11-22 08:56:18 -05:00
|
|
|
if (isset($options['long-progress'])) {
|
|
|
|
$progress = new LongProgress($show_errors, $show_info);
|
|
|
|
} else {
|
|
|
|
$progress = new DefaultProgress($show_errors, $show_info);
|
|
|
|
}
|
2019-10-02 03:28:42 +03:00
|
|
|
}
|
2018-06-30 15:29:37 -04:00
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
if (isset($options['no-cache']) || isset($options['i'])) {
|
2018-11-12 11:13:15 -05:00
|
|
|
$providers = new Provider\Providers(
|
|
|
|
new Provider\FileProvider
|
2018-09-28 16:18:45 -04:00
|
|
|
);
|
|
|
|
} else {
|
2018-11-12 11:13:15 -05: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 16:18:45 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-09 12:37:28 -04:00
|
|
|
$stdout_report_options = new \Psalm\Report\ReportOptions();
|
|
|
|
$stdout_report_options->use_color = !array_key_exists('m', $options);
|
|
|
|
$stdout_report_options->show_info = $show_info;
|
2019-12-02 15:24:01 -05:00
|
|
|
$stdout_report_options->show_suggestions = !array_key_exists('no-suggestions', $options);
|
2019-06-09 12:37:28 -04:00
|
|
|
/**
|
|
|
|
* @psalm-suppress PropertyTypeCoercion
|
|
|
|
*/
|
|
|
|
$stdout_report_options->format = $output_format;
|
|
|
|
$stdout_report_options->show_snippet = !isset($options['show-snippet']) || $options['show-snippet'] !== "false";
|
|
|
|
|
2018-11-11 12:01:14 -05:00
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
2018-01-21 10:22:04 -05:00
|
|
|
$config,
|
2018-09-28 16:18:45 -04:00
|
|
|
$providers,
|
2019-06-09 12:37:28 -04:00
|
|
|
$stdout_report_options,
|
|
|
|
ProjectAnalyzer::getFileReportOptions(
|
|
|
|
isset($options['report']) && is_string($options['report']) ? [$options['report']] : [],
|
|
|
|
isset($options['report-show-info'])
|
|
|
|
? $options['report-show-info'] !== 'false' && $options['report-show-info'] !== '0'
|
|
|
|
: true
|
|
|
|
),
|
2017-07-25 16:11:02 -04:00
|
|
|
$threads,
|
2019-06-09 12:37:28 -04:00
|
|
|
$progress
|
2017-01-31 20:47:16 -05:00
|
|
|
);
|
2017-01-16 11:05:29 -05:00
|
|
|
|
2020-01-30 08:20:23 +01:00
|
|
|
if (!isset($options['php-version'])) {
|
|
|
|
$options['php-version'] = $config->getPhpVersion();
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:25:57 -05: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 12:01:14 -05:00
|
|
|
$project_analyzer->getCodebase()->diff_methods = isset($options['diff-methods']);
|
2018-09-28 16:18:45 -04:00
|
|
|
|
2019-02-24 01:33:25 -05:00
|
|
|
if ($type_map_location) {
|
|
|
|
$project_analyzer->getCodebase()->store_node_types = true;
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:05:06 -04:00
|
|
|
$start_time = microtime(true);
|
2018-09-25 16:35:23 -04:00
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer, $progress);
|
2018-03-30 20:03:56 -04:00
|
|
|
|
2018-10-10 16:05:06 -04:00
|
|
|
$now_time = microtime(true);
|
2018-09-25 16:35:23 -04:00
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
$progress->debug('Visiting autoload files took ' . number_format($now_time - $start_time, 3) . 's' . "\n");
|
2018-09-25 16:35:23 -04:00
|
|
|
|
2018-03-26 09:08:55 -04:00
|
|
|
if (array_key_exists('debug-by-line', $options)) {
|
2018-11-11 12:01:14 -05:00
|
|
|
$project_analyzer->debug_lines = true;
|
2018-03-26 09:08:55 -04:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:45:09 -05:00
|
|
|
if ($config->find_unused_code) {
|
2019-04-12 18:28:07 -04:00
|
|
|
$find_unused_code = 'auto';
|
2019-03-05 15:45:09 -05:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:28:07 -04:00
|
|
|
if ($find_references_to !== null) {
|
|
|
|
$project_analyzer->getCodebase()->collectLocations();
|
|
|
|
$project_analyzer->show_issues = false;
|
2018-02-03 18:52:35 -05:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:28:07 -04:00
|
|
|
if ($find_unused_code) {
|
2019-04-17 13:15:06 -04:00
|
|
|
$project_analyzer->getCodebase()->reportUnusedCode($find_unused_code);
|
2018-02-17 17:45:30 -05:00
|
|
|
}
|
|
|
|
|
2019-11-17 14:49:28 -05:00
|
|
|
if ($config->find_unused_variables || $find_unused_variables) {
|
2019-03-05 15:45:09 -05:00
|
|
|
$project_analyzer->getCodebase()->reportUnusedVariables();
|
|
|
|
}
|
|
|
|
|
2019-08-04 10:37:36 -04:00
|
|
|
if (isset($options['track-tainted-input'])) {
|
|
|
|
$project_analyzer->trackTaintedInputs();
|
|
|
|
}
|
|
|
|
|
2019-08-18 14:27:50 -04:00
|
|
|
if (isset($options['find-unused-psalm-suppress'])) {
|
|
|
|
$project_analyzer->trackUnusedSuppressions();
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:05:54 -05:00
|
|
|
/** @var string $plugin_path */
|
|
|
|
foreach ($plugins as $plugin_path) {
|
2019-03-23 12:49:37 -04:00
|
|
|
$config->addPluginPath($plugin_path);
|
2018-01-01 20:05:54 -05:00
|
|
|
}
|
|
|
|
|
2018-10-18 14:34:46 -04:00
|
|
|
if ($paths_to_check === null) {
|
2018-11-11 12:01:14 -05:00
|
|
|
$project_analyzer->check($current_dir, $is_diff);
|
2016-11-04 20:13:16 -04:00
|
|
|
} elseif ($paths_to_check) {
|
2018-11-11 12:01:14 -05:00
|
|
|
$project_analyzer->checkPaths($paths_to_check);
|
2016-07-26 15:12:44 -04:00
|
|
|
}
|
2018-01-09 11:49:10 -05:00
|
|
|
|
2018-01-21 23:42:57 -05:00
|
|
|
if ($find_references_to) {
|
2018-11-11 12:01:14 -05:00
|
|
|
$project_analyzer->findReferencesTo($find_references_to);
|
2018-01-21 23:42:57 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
|
2019-04-16 18:16:46 -04:00
|
|
|
if ($is_diff) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Cannot set baseline in --diff mode' . PHP_EOL);
|
2019-04-16 18:16:46 -04:00
|
|
|
} else {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
ErrorBaseline::create(
|
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
|
|
|
$options['set-baseline'],
|
2019-07-11 10:41:44 -04:00
|
|
|
IssueBuffer::getIssuesData(),
|
2019-07-12 15:31:12 +01:00
|
|
|
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
|
2019-04-16 18:16:46 -04:00
|
|
|
);
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, "Baseline saved to {$options['set-baseline']}.");
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-09-09 17:03:23 -04:00
|
|
|
update_config_file(
|
|
|
|
$config,
|
|
|
|
$path_to_config ?? $current_dir,
|
|
|
|
$options['set-baseline']
|
|
|
|
);
|
2018-11-02 20:56:42 +01:00
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, PHP_EOL);
|
2019-04-16 18:16:46 -04:00
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$issue_baseline = [];
|
|
|
|
|
|
|
|
if (isset($options['update-baseline'])) {
|
2019-04-16 18:16:46 -04:00
|
|
|
if ($is_diff) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Cannot update baseline in --diff mode' . PHP_EOL);
|
2019-04-16 18:16:46 -04:00
|
|
|
} else {
|
|
|
|
$baselineFile = Config::getInstance()->error_baseline;
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
if (empty($baselineFile)) {
|
|
|
|
die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
try {
|
|
|
|
$issue_current_baseline = ErrorBaseline::read(
|
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
|
|
|
$baselineFile
|
|
|
|
);
|
|
|
|
$total_issues_current_baseline = ErrorBaseline::countTotalIssues($issue_current_baseline);
|
2019-02-09 12:13:58 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
$issue_baseline = ErrorBaseline::update(
|
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
|
|
|
$baselineFile,
|
2019-07-11 10:41:44 -04:00
|
|
|
IssueBuffer::getIssuesData(),
|
2019-07-12 15:31:12 +01:00
|
|
|
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
|
2019-04-16 18:16:46 -04:00
|
|
|
);
|
|
|
|
$total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline);
|
2019-02-09 12:13:58 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
$total_fixed_issues = $total_issues_current_baseline - $total_issues_updated_baseline;
|
2019-02-09 12:13:58 +01:00
|
|
|
|
2019-04-16 18:16:46 -04:00
|
|
|
if ($total_fixed_issues > 0) {
|
|
|
|
echo str_repeat('-', 30) . "\n";
|
|
|
|
echo $total_fixed_issues . ' errors fixed' . "\n";
|
|
|
|
}
|
|
|
|
} catch (\Psalm\Exception\ConfigException $exception) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, 'Could not update baseline file: ' . $exception->getMessage() . PHP_EOL);
|
|
|
|
exit(1);
|
2019-02-09 12:13:58 +01:00
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty(Config::getInstance()->error_baseline) && !isset($options['ignore-baseline'])) {
|
|
|
|
try {
|
|
|
|
$issue_baseline = ErrorBaseline::read(
|
2018-11-05 21:57:36 -05:00
|
|
|
new \Psalm\Internal\Provider\FileProvider,
|
2018-10-30 15:32:20 +01:00
|
|
|
(string)Config::getInstance()->error_baseline
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\ConfigException $exception) {
|
2019-06-28 17:49:57 +03:00
|
|
|
fwrite(STDERR, 'Error while reading baseline: ' . $exception->getMessage() . PHP_EOL);
|
|
|
|
exit(1);
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 01:33:25 -05: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 01:47:53 -05:00
|
|
|
foreach ($map[0] as $map_parts) {
|
|
|
|
$expected_references[$map_parts[1]] = true;
|
2019-02-24 01:33:25 -05: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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:30:42 -04:00
|
|
|
$type_map_string = json_encode(['files' => $name_file_map, 'references' => $reference_dictionary]);
|
|
|
|
|
2019-02-24 01:33:25 -05:00
|
|
|
$providers->file_provider->setContents(
|
|
|
|
$type_map_location,
|
2019-04-21 11:30:42 -04:00
|
|
|
$type_map_string
|
2019-02-24 01:33:25 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:23:40 -05:00
|
|
|
if ($stubs_location) {
|
|
|
|
$providers->file_provider->setContents(
|
|
|
|
$stubs_location,
|
|
|
|
\Psalm\Internal\Stubs\Generator\StubsGenerator::getAll(
|
|
|
|
$project_analyzer->getCodebase(),
|
2020-01-08 17:40:50 -05:00
|
|
|
$providers->classlike_storage_provider,
|
|
|
|
$providers->file_storage_provider
|
2020-01-08 17:23:40 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:33:28 -05:00
|
|
|
if (!isset($options['i'])) {
|
|
|
|
IssueBuffer::finish(
|
|
|
|
$project_analyzer,
|
|
|
|
!$paths_to_check,
|
|
|
|
$start_time,
|
|
|
|
isset($options['stats']),
|
|
|
|
$issue_baseline
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$issues_by_file = IssueBuffer::getIssuesData();
|
|
|
|
|
|
|
|
if (!$issues_by_file) {
|
|
|
|
$level = 1;
|
|
|
|
} else {
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
|
|
|
$mixed_counts = $codebase->analyzer->getTotalTypeCoverage($codebase);
|
|
|
|
|
|
|
|
$level = \Psalm\Config\Creator::getLevel(
|
|
|
|
array_merge(...array_values($issues_by_file)),
|
|
|
|
(int) array_sum($mixed_counts)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\n" . 'Detected level ' . $level . ' as a suitable initial default' . "\n";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$template_contents = Psalm\Config\Creator::getContents(
|
|
|
|
$current_dir,
|
|
|
|
$inferred_source_dir,
|
|
|
|
$level,
|
|
|
|
$vendor_dir
|
|
|
|
);
|
|
|
|
} catch (Psalm\Exception\ConfigCreationException $e) {
|
|
|
|
die($e->getMessage() . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_put_contents($current_dir . 'psalm.xml', $template_contents)) {
|
|
|
|
die('Could not write to psalm.xml' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
|
|
|
|
}
|