2018-01-07 06:11:23 +01:00
|
|
|
<?php
|
|
|
|
require_once('command_functions.php');
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2018-01-07 06:11:23 +01:00
|
|
|
use Psalm\Config;
|
2018-01-09 17:49:10 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
// show all errors
|
|
|
|
error_reporting(-1);
|
2018-08-29 19:58:07 +02:00
|
|
|
ini_set('display_errors', '1');
|
|
|
|
ini_set('display_startup_errors', '1');
|
2019-04-17 17:12:18 +02:00
|
|
|
ini_set('memory_limit', '4096M');
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2019-04-24 04:31:38 +02:00
|
|
|
gc_collect_cycles();
|
|
|
|
gc_disable();
|
|
|
|
|
|
|
|
$args = array_slice($argv, 1);
|
|
|
|
|
|
|
|
$valid_short_options = ['f:', 'm', 'h', 'r:'];
|
|
|
|
$valid_long_options = [
|
|
|
|
'help', 'debug', 'debug-by-line', 'config:', 'file:', 'root:',
|
|
|
|
'plugin:', 'issues:', 'php-version:', 'dry-run', 'safe-types',
|
|
|
|
'find-unused-code', 'threads:',
|
|
|
|
];
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
// get options from command line
|
2019-04-24 04:31:38 +02:00
|
|
|
$options = getopt(implode('', $valid_short_options), $valid_long_options);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
if (!in_array($arg_name, $valid_long_options)
|
|
|
|
&& !in_array($arg_name . ':', $valid_long_options)
|
|
|
|
&& !in_array($arg_name . '::', $valid_long_options)
|
|
|
|
) {
|
|
|
|
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
|
2018-01-07 06:11:23 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (array_key_exists('help', $options)) {
|
|
|
|
$options['h'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('monochrome', $options)) {
|
|
|
|
$options['m'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['config'])) {
|
|
|
|
$options['c'] = $options['config'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['c']) && is_array($options['c'])) {
|
|
|
|
die('Too many config files provided' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('h', $options)) {
|
|
|
|
echo <<< HELP
|
|
|
|
Usage:
|
2018-10-18 20:34:46 +02:00
|
|
|
psalter [options] [file...]
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help
|
|
|
|
Display this help message
|
|
|
|
|
2019-04-17 19:15:06 +02:00
|
|
|
--debug, --debug-by-line
|
2018-01-07 06:11:23 +01:00
|
|
|
Debug information
|
|
|
|
|
|
|
|
-c, --config=psalm.xml
|
|
|
|
Path to a psalm.xml configuration file. Run psalm --init to create one.
|
|
|
|
|
|
|
|
-m, --monochrome
|
|
|
|
Enable monochrome output
|
|
|
|
|
|
|
|
-r, --root
|
|
|
|
If running Psalm globally you'll need to specify a project root. Defaults to cwd
|
|
|
|
|
|
|
|
--plugin=PATH
|
|
|
|
Executes a plugin, an alternative to using the Psalm config
|
|
|
|
|
|
|
|
--dry-run
|
|
|
|
Shows a diff of all the changes, without making them
|
|
|
|
|
2018-01-07 23:17:18 +01:00
|
|
|
--safe-types
|
|
|
|
Only update PHP types when the new type information comes from other PHP types,
|
|
|
|
as opposed to type information that just comes from docblocks
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
--php-version=PHP_MAJOR_VERSION.PHP_MINOR_VERSION
|
|
|
|
|
|
|
|
--issues=IssueType1,IssueType2
|
|
|
|
If any issues can be fixed automatically, Psalm will update the codebase
|
|
|
|
|
2019-04-24 04:31:38 +02:00
|
|
|
--find-unused-code
|
2019-04-17 17:12:18 +02:00
|
|
|
Include unused code as a candidate for removal
|
2019-04-17 19:15:06 +02:00
|
|
|
|
|
|
|
--threads=INT
|
|
|
|
If greater than one, Psalm will run analysis on multiple threads, speeding things up.
|
2018-01-07 06:11:23 +01:00
|
|
|
HELP;
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-01-07 17:48:33 +01:00
|
|
|
if (!isset($options['issues']) && (!isset($options['plugin']) || $options['plugin'] === false)) {
|
|
|
|
die('Please specify the issues you want to fix with --issues=IssueOne,IssueTwo '
|
|
|
|
. 'or provide a plugin that has its own manipulations with --plugin=path/to/plugin.php' . PHP_EOL);
|
2018-01-07 06:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['root'])) {
|
|
|
|
$options['r'] = $options['root'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_dir = (string)getcwd() . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
if (isset($options['r']) && is_string($options['r'])) {
|
|
|
|
$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-25 17:32:54 +01:00
|
|
|
$vendor_dir = getVendorDir($current_dir);
|
|
|
|
|
2018-03-07 16:50:16 +01:00
|
|
|
$first_autoloader = requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2018-03-13 17:52:00 +01:00
|
|
|
// If XDebug is enabled, restart without it
|
|
|
|
(new \Composer\XdebugHandler\XdebugHandler('PSALTER'))->check();
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
$paths_to_check = getPathsToCheck(isset($options['f']) ? $options['f'] : null);
|
|
|
|
|
2018-01-22 04:14:29 +01:00
|
|
|
if ($paths_to_check && count($paths_to_check) > 1) {
|
|
|
|
die('Psalter can currently only be run on one path at a time' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
$path_to_config = isset($options['c']) && is_string($options['c']) ? realpath($options['c']) : null;
|
|
|
|
|
|
|
|
if ($path_to_config === false) {
|
|
|
|
/** @psalm-suppress InvalidCast */
|
|
|
|
die('Could not resolve path to config ' . (string)$options['c'] . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
// initialise custom config, if passed
|
|
|
|
if ($path_to_config) {
|
|
|
|
$config = Config::loadFromXMLFile($path_to_config, $current_dir);
|
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$config = Config::getConfigForPath($current_dir, $current_dir, ProjectAnalyzer::TYPE_CONSOLE);
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:50:16 +01:00
|
|
|
$config->setComposerClassLoader($first_autoloader);
|
|
|
|
|
2019-04-17 19:15:06 +02:00
|
|
|
$threads = isset($options['threads']) ? (int)$options['threads'] : 1;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2018-11-06 03:57:36 +01:00
|
|
|
new Psalm\Internal\Provider\Providers(
|
|
|
|
new Psalm\Internal\Provider\FileProvider(),
|
|
|
|
new Psalm\Internal\Provider\ParserCacheProvider($config),
|
|
|
|
new Psalm\Internal\Provider\FileStorageCacheProvider($config),
|
|
|
|
new Psalm\Internal\Provider\ClassLikeStorageCacheProvider($config)
|
2018-09-28 22:18:45 +02:00
|
|
|
),
|
2018-01-07 06:11:23 +01:00
|
|
|
!array_key_exists('m', $options),
|
|
|
|
false,
|
2018-11-06 03:57:36 +01:00
|
|
|
ProjectAnalyzer::TYPE_CONSOLE,
|
2019-04-17 19:15:06 +02:00
|
|
|
$threads,
|
2018-01-07 06:11:23 +01:00
|
|
|
array_key_exists('debug', $options)
|
|
|
|
);
|
|
|
|
|
2019-04-17 19:15:06 +02:00
|
|
|
if (array_key_exists('debug-by-line', $options)) {
|
|
|
|
$project_analyzer->debug_lines = true;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer);
|
2018-03-31 02:03:56 +02:00
|
|
|
|
2018-01-07 17:48:33 +01:00
|
|
|
if (array_key_exists('issues', $options)) {
|
|
|
|
if (!is_string($options['issues']) || !$options['issues']) {
|
|
|
|
die('Expecting a comma-separated list of issues' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$issues = explode(',', $options['issues']);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2018-01-07 17:48:33 +01:00
|
|
|
$keyed_issues = [];
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2018-01-07 17:48:33 +01:00
|
|
|
foreach ($issues as $issue) {
|
|
|
|
$keyed_issues[$issue] = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$keyed_issues = [];
|
2018-01-07 06:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['php-version'])) {
|
2019-02-07 18:25:57 +01:00
|
|
|
if (!is_string($options['php-version'])) {
|
2018-01-07 06:11:23 +01:00
|
|
|
die('Expecting a version number in the format x.y' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
$project_analyzer->setPhpVersion($options['php-version']);
|
2018-01-07 06:11:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 17:48:33 +01:00
|
|
|
$plugins = [];
|
|
|
|
|
|
|
|
if (isset($options['plugin'])) {
|
|
|
|
$plugins = $options['plugin'];
|
|
|
|
|
|
|
|
if (!is_array($plugins)) {
|
|
|
|
$plugins = [$plugins];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string $plugin_path */
|
|
|
|
foreach ($plugins as $plugin_path) {
|
2019-01-08 15:16:59 +01:00
|
|
|
Config::getInstance()->addPluginPath($current_dir . $plugin_path);
|
2018-01-07 17:48:33 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 19:15:06 +02:00
|
|
|
$find_unused_code = array_key_exists('find-unused-code', $options);
|
|
|
|
|
|
|
|
if ($config->find_unused_code) {
|
|
|
|
$find_unused_code = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($find_unused_code) {
|
2019-04-17 17:12:18 +02:00
|
|
|
$project_analyzer->getCodebase()->reportUnusedCode();
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->alterCodeAfterCompletion(
|
2018-01-07 22:11:51 +01:00
|
|
|
array_key_exists('dry-run', $options),
|
|
|
|
array_key_exists('safe-types', $options)
|
2018-01-07 17:48:33 +01:00
|
|
|
);
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->setIssuesToFix($keyed_issues);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2018-10-10 22:05:06 +02:00
|
|
|
$start_time = microtime(true);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
if ($paths_to_check === null) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->check($current_dir);
|
2018-01-07 06:11:23 +01:00
|
|
|
} elseif ($paths_to_check) {
|
|
|
|
foreach ($paths_to_check as $path_to_check) {
|
|
|
|
if (is_dir($path_to_check)) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->checkDir($path_to_check);
|
2018-01-07 06:11:23 +01:00
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer->checkFile($path_to_check);
|
2018-01-07 06:11:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 17:49:10 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
IssueBuffer::finish($project_analyzer, false, $start_time);
|