2019-06-02 07:10:50 +02:00
|
|
|
<?php
|
|
|
|
require_once('command_functions.php');
|
|
|
|
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Progress\DebugProgress;
|
|
|
|
use Psalm\Progress\DefaultProgress;
|
|
|
|
|
|
|
|
// show all errors
|
|
|
|
error_reporting(-1);
|
|
|
|
ini_set('display_errors', '1');
|
|
|
|
ini_set('display_startup_errors', '1');
|
2019-06-06 01:32:27 +02:00
|
|
|
ini_set('memory_limit', '8192M');
|
2019-06-02 07:10:50 +02:00
|
|
|
|
|
|
|
gc_collect_cycles();
|
|
|
|
gc_disable();
|
|
|
|
|
2019-12-05 05:17:08 +01:00
|
|
|
require_once __DIR__ . '/Psalm/Internal/exception_handler.php';
|
|
|
|
|
2019-06-02 07:10:50 +02:00
|
|
|
$args = array_slice($argv, 1);
|
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
$valid_short_options = ['f:', 'm', 'h', 'r:', 'c:'];
|
2019-06-02 07:10:50 +02:00
|
|
|
$valid_long_options = [
|
2020-06-23 00:16:47 +02:00
|
|
|
'help', 'debug', 'debug-by-line', 'debug-emitted-issues', 'config:', 'root:',
|
2019-06-02 18:02:32 +02:00
|
|
|
'threads:', 'move:', 'into:', 'rename:', 'to:',
|
2019-06-02 07:10:50 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// get options from command line
|
|
|
|
$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 ($arg_name === 'refactor') {
|
|
|
|
// valid option for psalm, ignored by psalter
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($arg_name, $valid_long_options)
|
|
|
|
&& !in_array($arg_name . ':', $valid_long_options)
|
|
|
|
&& !in_array($arg_name . '::', $valid_long_options)
|
|
|
|
) {
|
|
|
|
fwrite(
|
|
|
|
STDERR,
|
|
|
|
'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)) {
|
|
|
|
fwrite(
|
|
|
|
STDERR,
|
|
|
|
'Unrecognised argument "-' . $arg_name . '"' . PHP_EOL
|
|
|
|
. 'Type --help to see a list of supported arguments'. PHP_EOL
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
|
|
|
|
if (array_key_exists('help', $options)) {
|
|
|
|
$options['h'] = 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:
|
|
|
|
psalm-refactor [options] [symbol1] into [symbol2]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help
|
|
|
|
Display this help message
|
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
--debug, --debug-by-line, --debug-emitted-issues
|
2019-06-02 07:10:50 +02:00
|
|
|
Debug information
|
|
|
|
|
|
|
|
-c, --config=psalm.xml
|
|
|
|
Path to a psalm.xml configuration file. Run psalm --init to create one.
|
|
|
|
|
|
|
|
-r, --root
|
|
|
|
If running Psalm globally you'll need to specify a project root. Defaults to cwd
|
|
|
|
|
2019-06-02 18:02:32 +02:00
|
|
|
--threads=auto
|
2019-06-02 07:10:50 +02:00
|
|
|
If greater than one, Psalm will run analysis on multiple threads, speeding things up.
|
2019-06-02 18:02:32 +02:00
|
|
|
By default
|
|
|
|
|
|
|
|
--move "[Identifier]" --into "[Class]"
|
|
|
|
Moves the specified item into the class. More than one item can be moved into a class
|
|
|
|
by passing a comma-separated list of values e.g.
|
|
|
|
|
|
|
|
--move "Ns\Foo::bar,Ns\Foo::baz" --into "Biz\Bang\DestinationClass"
|
|
|
|
|
2019-06-03 18:24:32 +02:00
|
|
|
--rename "[Identifier]" --to "[NewIdentifier]"
|
2019-08-11 23:13:43 +02:00
|
|
|
Renames a specified item (e.g. method) and updates all references to it that Psalm can
|
2019-06-02 18:02:32 +02:00
|
|
|
identify.
|
|
|
|
|
2019-06-02 07:10:50 +02:00
|
|
|
HELP;
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vendor_dir = getVendorDir($current_dir);
|
|
|
|
|
|
|
|
$first_autoloader = requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
|
|
|
|
|
2019-09-14 16:13:39 +02:00
|
|
|
// If Xdebug is enabled, restart without it
|
2019-06-02 07:10:50 +02:00
|
|
|
(new \Composer\XdebugHandler\XdebugHandler('PSALTER'))->check();
|
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
$path_to_config = get_path_to_config($options);
|
2019-06-02 07:10:50 +02:00
|
|
|
|
2019-06-02 18:02:32 +02:00
|
|
|
$args = getArguments();
|
|
|
|
|
|
|
|
$operation = null;
|
|
|
|
$last_arg = null;
|
|
|
|
|
2019-06-03 05:33:57 +02:00
|
|
|
$to_refactor = [];
|
2019-06-02 18:02:32 +02:00
|
|
|
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
if ($arg === '--move') {
|
|
|
|
$operation = 'move';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg === '--into') {
|
|
|
|
if ($operation !== 'move' || !$last_arg) {
|
|
|
|
die('--into is not expected here' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
$operation = 'move_into';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg === '--rename') {
|
|
|
|
$operation = 'rename';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg === '--to') {
|
2019-06-03 18:30:20 +02:00
|
|
|
if ($operation !== 'rename' || !$last_arg) {
|
2019-06-02 18:02:32 +02:00
|
|
|
die('--to is not expected here' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:30:20 +02:00
|
|
|
$operation = 'rename_to';
|
2019-06-02 18:02:32 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg[0] === '-') {
|
|
|
|
$operation = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:30:20 +02:00
|
|
|
if ($operation === 'move_into' || $operation === 'rename_to') {
|
2019-06-02 18:02:32 +02:00
|
|
|
if (!$last_arg) {
|
|
|
|
die('Expecting a previous argument' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($operation === 'move_into') {
|
|
|
|
$last_arg_parts = preg_split('/, ?/', $last_arg);
|
|
|
|
|
|
|
|
foreach ($last_arg_parts as $last_arg_part) {
|
2019-06-09 06:54:13 +02:00
|
|
|
if (strpos($last_arg_part, '::')) {
|
|
|
|
list(, $identifier_name) = explode('::', $last_arg_part);
|
|
|
|
$to_refactor[$last_arg_part] = $arg . '::' . $identifier_name;
|
|
|
|
} else {
|
|
|
|
$namespace_parts = explode('\\', $last_arg_part);
|
|
|
|
$class_name = end($namespace_parts);
|
|
|
|
$to_refactor[$last_arg_part] = $arg . '\\' . $class_name;
|
|
|
|
}
|
2019-06-02 18:02:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-03 18:24:32 +02:00
|
|
|
$to_refactor[$last_arg] = $arg;
|
2019-06-02 18:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$last_arg = null;
|
|
|
|
$operation = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($operation === 'move' || $operation === 'rename') {
|
|
|
|
$last_arg = $arg;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
die('Unexpected argument "' . $arg . '"' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2019-06-03 05:33:57 +02:00
|
|
|
if (!$to_refactor) {
|
2019-06-02 18:02:32 +02:00
|
|
|
die('No --move or --rename arguments supplied' . PHP_EOL);
|
|
|
|
}
|
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
$config = initialiseConfig($path_to_config, $current_dir, \Psalm\Report::TYPE_CONSOLE, $first_autoloader);
|
2019-06-02 07:10:50 +02:00
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
if ($config->resolve_from_config_file) {
|
|
|
|
$current_dir = $config->base_dir;
|
|
|
|
chdir($current_dir);
|
|
|
|
}
|
2019-06-02 07:10:50 +02:00
|
|
|
|
2019-06-02 18:02:32 +02:00
|
|
|
$threads = isset($options['threads'])
|
|
|
|
? (int)$options['threads']
|
|
|
|
: max(1, ProjectAnalyzer::getCpuCount() - 2);
|
2019-06-02 07:10:50 +02:00
|
|
|
|
|
|
|
$providers = new Psalm\Internal\Provider\Providers(
|
|
|
|
new Psalm\Internal\Provider\FileProvider(),
|
2020-04-12 14:34:57 +02:00
|
|
|
new Psalm\Internal\Provider\ParserCacheProvider($config, false),
|
2019-06-02 07:10:50 +02:00
|
|
|
new Psalm\Internal\Provider\FileStorageCacheProvider($config),
|
2020-04-12 17:41:01 +02:00
|
|
|
new Psalm\Internal\Provider\ClassLikeStorageCacheProvider($config),
|
|
|
|
null,
|
|
|
|
new Psalm\Internal\Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
|
2019-06-02 07:10:50 +02:00
|
|
|
);
|
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
|
2019-06-02 07:10:50 +02:00
|
|
|
$progress = $debug
|
|
|
|
? new DebugProgress()
|
|
|
|
: new DefaultProgress();
|
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
if (array_key_exists('debug-emitted-issues', $options)) {
|
|
|
|
$config->debug_emitted_issues = true;
|
|
|
|
}
|
|
|
|
|
2019-06-02 07:10:50 +02:00
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
|
|
|
$config,
|
|
|
|
$providers,
|
2019-06-09 18:37:28 +02:00
|
|
|
new \Psalm\Report\ReportOptions(),
|
|
|
|
[],
|
2019-06-02 07:10:50 +02:00
|
|
|
$threads,
|
|
|
|
$progress
|
|
|
|
);
|
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
if (array_key_exists('debug-by-line', $options)) {
|
|
|
|
$project_analyzer->debug_lines = true;
|
|
|
|
}
|
|
|
|
|
2019-06-02 07:10:50 +02:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer);
|
|
|
|
|
2019-06-03 05:33:57 +02:00
|
|
|
$project_analyzer->refactorCodeAfterCompletion($to_refactor);
|
2019-06-02 07:10:50 +02:00
|
|
|
|
|
|
|
$start_time = microtime(true);
|
|
|
|
|
|
|
|
$project_analyzer->check($current_dir);
|
|
|
|
|
|
|
|
IssueBuffer::finish($project_analyzer, false, $start_time);
|