1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/psalm.php

847 lines
24 KiB
PHP
Raw Normal View History

2016-07-26 21:12:44 +02:00
<?php
2020-07-22 21:57:24 +02:00
namespace Psalm;
gc_collect_cycles();
gc_disable();
// show all errors
error_reporting(-1);
require_once('command_functions.php');
require_once __DIR__ . '/Psalm/Internal/Composer.php';
2020-07-22 21:57:24 +02:00
require_once __DIR__ . '/Psalm/Internal/exception_handler.php';
2016-07-26 21:12:44 +02:00
use Psalm\Exception\ConfigException;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Composer;
use Psalm\Internal\Provider;
use Psalm\Internal\IncludeCollector;
use Psalm\Progress\DebugProgress;
use Psalm\Progress\DefaultProgress;
use Psalm\Progress\LongProgress;
use Psalm\Progress\VoidProgress;
2020-07-22 21:57:24 +02:00
use function array_slice;
use function getopt;
use function implode;
use function is_scalar;
2020-07-22 21:57:24 +02:00
use function array_map;
use function substr;
use function preg_replace;
use function in_array;
use function fwrite;
use const STDERR;
use const PHP_EOL;
use function array_key_exists;
use function ini_set;
use function is_array;
use function getcwd;
use const DIRECTORY_SEPARATOR;
use function is_string;
use function realpath;
use function file_exists;
use function array_values;
use function array_filter;
use function strpos;
use function count;
use function preg_match;
use function file_put_contents;
use function is_numeric;
use function chdir;
use function max;
use function ini_get;
use const PHP_OS;
use function version_compare;
use const PHP_VERSION;
use function setlocale;
use const LC_CTYPE;
use function microtime;
use function str_repeat;
use function json_encode;
use function array_merge;
use function array_sum;
use function gc_collect_cycles;
use function gc_disable;
use function error_reporting;
2016-07-26 21:12:44 +02:00
$valid_short_options = [
'f:',
'm',
'h',
'v',
'c:',
'i',
'r:',
];
$valid_long_options = [
'clear-cache',
2018-10-15 17:29:57 +02:00
'clear-global-cache',
'config:',
'debug',
'debug-by-line',
'debug-performance',
'debug-emitted-issues',
'diff',
'disable-extension:',
'find-dead-code::',
'find-unused-code::',
'find-unused-variables',
'find-references-to:',
'help',
'ignore-baseline',
'init',
'memory-limit:',
'monochrome',
'no-diff',
'no-cache',
'no-reflection-cache',
'no-file-cache',
'output-format:',
'plugin:',
'report:',
'report-show-info:',
'root:',
'set-baseline:',
'show-info:',
'show-snippet:',
'stats',
'threads:',
'update-baseline',
'use-baseline:',
'use-ini-defaults',
'version',
'php-version:',
2019-02-24 07:33:25 +01:00
'generate-json-map:',
'generate-stubs:',
2019-03-04 04:59:00 +01:00
'alter',
2019-03-27 16:55:10 +01:00
'language-server',
2019-06-02 07:10:50 +02:00
'refactor',
2019-03-31 20:02:30 +02:00
'shepherd::',
'no-progress',
'long-progress',
'no-suggestions',
'include-php-versions', // used for baseline
'pretty-print', // used for JSON reports
2019-08-18 20:27:50 +02:00
'track-tainted-input',
2020-06-22 17:24:38 +02:00
'taint-analysis',
'security-analysis',
2019-08-18 20:27:50 +02:00
'find-unused-psalm-suppress',
2020-02-18 04:43:13 +01:00
'error-level:',
];
$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'])) {
require_once __DIR__ . '/psalter.php';
2019-03-04 04:59:00 +01:00
exit;
}
2019-03-27 16:55:10 +01:00
if (isset($options['language-server'])) {
require_once __DIR__ . '/psalm-language-server.php';
2019-03-27 16:55:10 +01:00
exit;
}
2019-06-02 07:10:50 +02:00
if (isset($options['refactor'])) {
require_once __DIR__ . '/psalm-refactor.php';
2019-06-02 07:10:50 +02:00
exit;
}
array_map(
/**
* @param string $arg
*/
function ($arg) use ($valid_long_options, $valid_short_options): void {
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)
) {
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, 1) === '-' && $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
2016-12-08 04:38:57 +01:00
);
if (!array_key_exists('use-ini-defaults', $options)) {
ini_set('display_errors', 'stderr');
ini_set('display_startup_errors', '1');
$memoryLimit = (8 * 1024 * 1024 * 1024);
if (array_key_exists('memory-limit', $options)) {
$memoryLimit = $options['memory-limit'];
if (!is_scalar($memoryLimit)) {
throw new ConfigException('Invalid memory limit specified.');
}
}
ini_set('memory_limit', (string) $memoryLimit);
}
if (array_key_exists('help', $options)) {
2016-11-05 03:55:13 +01:00
$options['h'] = false;
}
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;
}
if (array_key_exists('monochrome', $options)) {
2016-11-05 03:55:13 +01:00
$options['m'] = false;
}
if (isset($options['config'])) {
2016-11-05 03:55:13 +01:00
$options['c'] = $options['config'];
}
2017-01-17 01:44:31 +01:00
if (isset($options['c']) && is_array($options['c'])) {
fwrite(STDERR, 'Too many config files provided' . PHP_EOL);
exit(1);
2017-01-17 01:44:31 +01:00
}
if (array_key_exists('h', $options)) {
echo getPsalmHelpText();
/*
2019-03-31 20:02:30 +02: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
*/
exit;
}
2016-07-26 21:12:44 +02:00
if (getcwd() === false) {
fwrite(STDERR, 'Cannot get current working directory' . PHP_EOL);
exit(1);
}
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) {
fwrite(
STDERR,
'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL
);
exit(1);
}
$current_dir = $root_path . DIRECTORY_SEPARATOR;
}
$path_to_config = get_path_to_config($options);
2020-07-22 21:57:24 +02:00
$vendor_dir = \Psalm\getVendorDir($current_dir);
require_once __DIR__ . '/' . 'Psalm/Internal/IncludeCollector.php';
$include_collector = new IncludeCollector();
$first_autoloader = $include_collector->runAndCollect(
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
}
);
$run_taint_analysis = (isset($options['track-tainted-input'])
|| isset($options['security-analysis'])
|| isset($options['taint-analysis']));
2020-02-17 22:33:28 +01:00
if (array_key_exists('v', $options)) {
echo 'Psalm ' . PSALM_VERSION . PHP_EOL;
exit;
}
$output_format = isset($options['output-format']) && is_string($options['output-format'])
? $options['output-format']
: \Psalm\Report::TYPE_CONSOLE;
2020-02-18 04:43:13 +01:00
$init_level = null;
$init_source_dir = null;
2020-02-17 22:33:28 +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,
function (string $arg): bool {
return $arg !== '--ansi'
&& $arg !== '--no-ansi'
&& $arg !== '-i'
&& $arg !== '--init'
2020-02-17 22:33:28 +01:00
&& $arg !== '--debug'
&& $arg !== '--debug-by-line'
&& $arg !== '--debug-emitted-issues'
&& 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);
}
2020-02-18 04:43:13 +01:00
$init_level = (int)$args[1];
}
2020-02-18 04:43:13 +01:00
$init_source_dir = $args[0];
}
2020-07-22 21:57:24 +02:00
$vendor_dir = \Psalm\getVendorDir($current_dir);
2020-02-18 04:43:13 +01:00
if ($init_level === null) {
2020-02-17 22:33:28 +01:00
echo "Calculating best config level based on project files\n";
2020-07-22 21:57:24 +02:00
\Psalm\Config\Creator::createBareConfig($current_dir, $init_source_dir, $vendor_dir);
2020-02-17 22:33:28 +01:00
$config = \Psalm\Config::getInstance();
} else {
try {
2020-07-22 21:57:24 +02:00
$template_contents = \Psalm\Config\Creator::getContents(
2020-02-17 22:33:28 +01:00
$current_dir,
2020-02-18 04:43:13 +01:00
$init_source_dir,
$init_level,
2020-02-17 22:33:28 +01:00
$vendor_dir
);
2020-07-22 21:57:24 +02:00
} catch (\Psalm\Exception\ConfigCreationException $e) {
2020-02-17 22:33:28 +01:00
die($e->getMessage() . PHP_EOL);
}
2020-02-17 22:33:28 +01:00
if (!file_put_contents($current_dir . 'psalm.xml', $template_contents)) {
die('Could not write to psalm.xml' . PHP_EOL);
}
2020-02-17 22:33:28 +01:00
exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
}
} else {
$config = initialiseConfig($path_to_config, $current_dir, $output_format, $first_autoloader, $run_taint_analysis);
2020-02-18 04:43:13 +01:00
if (isset($options['error-level'])
&& is_numeric($options['error-level'])
) {
$config_level = (int) $options['error-level'];
if (!in_array($config_level, [1, 2, 3, 4, 5, 6, 7, 8], true)) {
throw new \Psalm\Exception\ConfigException(
'Invalid error level ' . $config_level
);
}
$config->level = $config_level;
}
}
$config->setIncludeCollector($include_collector);
$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 {
2020-03-06 19:13:18 +01:00
$threads = max(1, ProjectAnalyzer::getCpuCount() - 1);
}
if (!isset($options['threads'])
&& !isset($options['debug'])
&& $threads === 1
&& ini_get('pcre.jit') === '1'
&& PHP_OS === 'Darwin'
&& version_compare(PHP_VERSION, '7.3.0') >= 0
2019-11-30 14:26:26 +01:00
&& version_compare(PHP_VERSION, '7.4.0') < 0
) {
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-06 03:57:36 +01:00
$ini_handler = new \Psalm\Internal\Fork\PsalmRestarter('PSALM');
2018-07-17 22:54:34 +02:00
if (isset($options['disable-extension'])) {
if (is_array($options['disable-extension'])) {
/** @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']);
}
}
if ($threads > 1) {
$ini_handler->disableExtension('grpc');
}
2019-06-06 16:36:26 +02:00
$ini_handler->disableExtension('uopz');
2019-02-24 07:33:25 +01:00
$type_map_location = null;
if (isset($options['generate-json-map']) && is_string($options['generate-json-map'])) {
$type_map_location = $options['generate-json-map'];
2019-02-24 07:33:25 +01:00
}
$stubs_location = null;
if (isset($options['generate-stubs']) && is_string($options['generate-stubs'])) {
$stubs_location = $options['generate-stubs'];
}
// If Xdebug is enabled, restart without it
$ini_handler->check();
2017-10-07 20:41:16 +02:00
if ($config->load_xdebug_stub === null && '' !== $ini_handler->getSkippedVersion()) {
$config->load_xdebug_stub = true;
}
if (isset($options['debug-emitted-issues'])) {
$config->debug_emitted_issues = true;
}
2018-02-18 04:11:42 +01:00
setlocale(LC_CTYPE, 'C');
if (isset($options['set-baseline'])) {
if (is_array($options['set-baseline'])) {
die('Only one baseline file can be created at a time' . PHP_EOL);
}
}
2020-07-22 21:57:24 +02:00
$paths_to_check = \Psalm\getPathsToCheck(isset($options['f']) ? $options['f'] : null);
2020-10-25 14:46:49 +01:00
if ($config->resolve_from_config_file) {
$current_dir = $config->base_dir;
chdir($current_dir);
}
$plugins = [];
if (isset($options['plugin'])) {
$plugins = $options['plugin'];
if (!is_array($plugins)) {
$plugins = [$plugins];
}
}
2016-11-05 03:55:13 +01:00
$show_info = isset($options['show-info'])
2020-02-17 22:33:28 +01:00
? $options['show-info'] === 'true' || $options['show-info'] === '1'
: false;
2016-11-05 03:55:13 +01:00
$is_diff = !isset($options['no-diff'])
&& !isset($options['set-baseline'])
&& !isset($options['update-baseline']);
2016-07-26 21:12:44 +02:00
/** @var false|'always'|'auto' $find_unused_code */
$find_unused_code = false;
if (isset($options['find-dead-code'])) {
$options['find-unused-code'] = $options['find-dead-code'];
}
if (isset($options['find-unused-code'])) {
if ($options['find-unused-code'] === 'always') {
$find_unused_code = 'always';
} else {
$find_unused_code = 'auto';
}
}
$find_unused_variables = isset($options['find-unused-variables']);
$find_references_to = isset($options['find-references-to']) && is_string($options['find-references-to'])
? $options['find-references-to']
: null;
2019-03-31 20:02:30 +02:00
if (isset($options['shepherd'])) {
if (is_string($options['shepherd'])) {
$config->shepherd_host = $options['shepherd'];
}
2019-03-31 20:02:30 +02:00
$shepherd_plugin = __DIR__ . '/Psalm/Plugin/Shepherd.php';
2019-03-31 20:02:30 +02:00
if (!file_exists($shepherd_plugin)) {
die('Could not find Shepherd plugin location ' . $shepherd_plugin . PHP_EOL);
}
2019-03-31 20:02:30 +02:00
$plugins[] = $shepherd_plugin;
}
2018-02-08 02:15:56 +01:00
if (isset($options['clear-cache'])) {
$cache_directory = $config->getCacheDirectory();
if ($cache_directory !== null) {
Config::removeCacheDirectory($cache_directory);
}
2018-02-08 02:15:56 +01:00
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();
2019-04-14 19:04:25 +02:00
if ($cache_directory) {
Config::removeCacheDirectory($cache_directory);
echo 'Global cache directory deleted' . PHP_EOL;
}
2018-10-15 17:29:57 +02:00
exit;
}
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
if ($debug) {
$progress = new DebugProgress();
} elseif (isset($options['no-progress'])) {
$progress = new VoidProgress();
} else {
$show_errors = !$config->error_baseline || isset($options['ignore-baseline']);
if (isset($options['long-progress'])) {
$progress = new LongProgress($show_errors, $show_info);
} else {
$progress = new DefaultProgress($show_errors, $show_info);
}
}
2020-02-17 22:33:28 +01:00
if (isset($options['no-cache']) || isset($options['i'])) {
$providers = new Provider\Providers(
new Provider\FileProvider
);
} else {
$no_reflection_cache = isset($options['no-reflection-cache']);
$no_file_cache = isset($options['no-file-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, !$no_file_cache),
$file_storage_cache_provider,
$classlike_storage_cache_provider,
new Provider\FileReferenceCacheProvider($config),
new Provider\ProjectCacheProvider(Composer::getLockFilePath($current_dir))
);
}
$stdout_report_options = new \Psalm\Report\ReportOptions();
$stdout_report_options->use_color = !array_key_exists('m', $options);
$stdout_report_options->show_info = $show_info;
$stdout_report_options->show_suggestions = !array_key_exists('no-suggestions', $options);
/**
* @psalm-suppress PropertyTypeCoercion
*/
$stdout_report_options->format = $output_format;
$stdout_report_options->show_snippet = !isset($options['show-snippet']) || $options['show-snippet'] !== "false";
$stdout_report_options->pretty = isset($options['pretty-print']) && $options['pretty-print'] !== "false";
/** @var list<string>|string $report_file_paths type guaranteed by argument to getopt() */
$report_file_paths = $options['report'] ?? [];
if (is_string($report_file_paths)) {
$report_file_paths = [$report_file_paths];
}
2018-11-11 18:01:14 +01:00
$project_analyzer = new ProjectAnalyzer(
$config,
$providers,
$stdout_report_options,
ProjectAnalyzer::getFileReportOptions(
$report_file_paths,
isset($options['report-show-info'])
? $options['report-show-info'] !== 'false' && $options['report-show-info'] !== '0'
: true
),
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$threads,
$progress
);
2017-01-16 17:05:29 +01:00
if (!isset($options['php-version'])) {
$options['php-version'] = $config->getPhpVersion();
}
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']);
}
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-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
}
if (array_key_exists('debug-performance', $options)) {
$project_analyzer->debug_performance = true;
}
if ($config->find_unused_code) {
$find_unused_code = 'auto';
}
if ($find_references_to !== null) {
$project_analyzer->getCodebase()->collectLocations();
$project_analyzer->show_issues = false;
2018-02-04 00:52:35 +01:00
}
if ($find_unused_code) {
$project_analyzer->getCodebase()->reportUnusedCode($find_unused_code);
}
if ($config->find_unused_variables || $find_unused_variables) {
$project_analyzer->getCodebase()->reportUnusedVariables();
}
if ($config->run_taint_analysis || $run_taint_analysis) {
2020-11-17 23:59:05 +01:00
$is_diff = false;
$project_analyzer->trackTaintedInputs();
}
if ($config->find_unused_psalm_suppress || isset($options['find-unused-psalm-suppress'])) {
2019-08-18 20:27:50 +02:00
$project_analyzer->trackUnusedSuppressions();
}
/** @var string $plugin_path */
foreach ($plugins as $plugin_path) {
$config->addPluginPath($plugin_path);
}
if ($paths_to_check === null) {
2018-11-11 18:01:14 +01:00
$project_analyzer->check($current_dir, $is_diff);
} 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-22 05:42:57 +01:00
if ($find_references_to) {
2018-11-11 18:01:14 +01:00
$project_analyzer->findReferencesTo($find_references_to);
2018-01-22 05:42:57 +01:00
}
if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);
ErrorBaseline::create(
new \Psalm\Internal\Provider\FileProvider,
$options['set-baseline'],
IssueBuffer::getIssuesData(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
);
fwrite(STDERR, "Baseline saved to {$options['set-baseline']}.");
update_config_file(
$config,
$path_to_config ?? $current_dir,
$options['set-baseline']
);
fwrite(STDERR, 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 {
$issue_current_baseline = ErrorBaseline::read(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile
);
$total_issues_current_baseline = ErrorBaseline::countTotalIssues($issue_current_baseline);
$issue_baseline = ErrorBaseline::update(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile,
IssueBuffer::getIssuesData(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
);
$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";
}
} catch (\Psalm\Exception\ConfigException $exception) {
fwrite(STDERR, 'Could not update baseline file: ' . $exception->getMessage() . PHP_EOL);
exit(1);
}
}
if (isset($options['use-baseline'])) {
if (!is_string($options['use-baseline'])) {
fwrite(STDERR, '--use-baseline must be a string' . PHP_EOL);
exit(1);
}
$baseline_file_path = $options['use-baseline'];
} else {
$baseline_file_path = Config::getInstance()->error_baseline;
}
if ($baseline_file_path && !isset($options['ignore-baseline'])) {
try {
$issue_baseline = ErrorBaseline::read(
2018-11-06 03:57:36 +01:00
new \Psalm\Internal\Provider\FileProvider,
$baseline_file_path
);
} catch (\Psalm\Exception\ConfigException $exception) {
fwrite(STDERR, 'Error while reading baseline: ' . $exception->getMessage() . PHP_EOL);
exit(1);
}
}
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 = \Psalm\Internal\Codebase\ReferenceMapGenerator::getReferenceMap(
$providers->classlike_storage_provider,
$expected_references
);
2019-02-24 07:33:25 +01:00
$type_map_string = json_encode(['files' => $name_file_map, 'references' => $reference_dictionary]);
2019-02-24 07:33:25 +01:00
$providers->file_provider->setContents(
$type_map_location,
$type_map_string
2019-02-24 07:33:25 +01:00
);
}
if ($stubs_location) {
$providers->file_provider->setContents(
$stubs_location,
\Psalm\Internal\Stubs\Generator\StubsGenerator::getAll(
$project_analyzer->getCodebase(),
2020-01-08 23:40:50 +01:00
$providers->classlike_storage_provider,
$providers->file_storage_provider
)
);
}
2020-02-17 22:33:28 +01: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) {
2020-02-18 04:43:13 +01:00
$init_level = 1;
2020-02-17 22:33:28 +01:00
} else {
$codebase = $project_analyzer->getCodebase();
$mixed_counts = $codebase->analyzer->getTotalTypeCoverage($codebase);
2020-02-18 04:43:13 +01:00
$init_level = \Psalm\Config\Creator::getLevel(
2020-02-17 22:33:28 +01:00
array_merge(...array_values($issues_by_file)),
array_sum($mixed_counts)
2020-02-17 22:33:28 +01:00
);
}
2020-02-18 04:43:13 +01:00
echo "\n" . 'Detected level ' . $init_level . ' as a suitable initial default' . "\n";
2020-02-17 22:33:28 +01:00
try {
2020-07-22 21:57:24 +02:00
$template_contents = \Psalm\Config\Creator::getContents(
2020-02-17 22:33:28 +01:00
$current_dir,
2020-02-18 04:43:13 +01:00
$init_source_dir,
$init_level,
2020-02-17 22:33:28 +01:00
$vendor_dir
);
2020-07-22 21:57:24 +02:00
} catch (\Psalm\Exception\ConfigCreationException $e) {
2020-02-17 22:33:28 +01:00
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);
}