*/ function getArguments() : array { global $argv; if (!$argv) { return []; } $filtered_input_paths = []; for ($i = 0, $iMax = count($argv); $i < $iMax; ++$i) { $input_path = $argv[$i]; if (realpath($input_path) !== false) { continue; } if ($input_path[0] === '-' && strlen($input_path) === 2) { if ($input_path[1] === 'c' || $input_path[1] === 'f') { ++$i; } continue; } if ($input_path[0] === '-' && $input_path[2] === '=') { continue; } $filtered_input_paths[] = $input_path; } return $filtered_input_paths; } /** * @param string|array|null|false $f_paths * * @return list|null */ function getPathsToCheck($f_paths): ?array { global $argv; $paths_to_check = []; if ($f_paths) { $input_paths = is_array($f_paths) ? $f_paths : [$f_paths]; } else { $input_paths = $argv ? $argv : null; } if ($input_paths) { $filtered_input_paths = []; for ($i = 0, $iMax = count($input_paths); $i < $iMax; ++$i) { /** @var string */ $input_path = $input_paths[$i]; if (realpath($input_path) === realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'psalm') || realpath($input_path) === realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'psalter') || realpath($input_path) === realpath(Phar::running(false)) ) { continue; } if ($input_path[0] === '-' && strlen($input_path) === 2) { if ($input_path[1] === 'c' || $input_path[1] === 'f') { ++$i; } continue; } if ($input_path[0] === '-' && $input_path[2] === '=') { continue; } if (substr($input_path, 0, 2) === '--' && strlen($input_path) > 2) { continue; } $filtered_input_paths[] = $input_path; } if ($filtered_input_paths === ['-']) { $meta = stream_get_meta_data(STDIN); stream_set_blocking(STDIN, false); if ($stdin = fgets(STDIN)) { $filtered_input_paths = preg_split('/\s+/', trim($stdin)); } $blocked = $meta['blocked']; stream_set_blocking(STDIN, $blocked); } foreach ($filtered_input_paths as $path_to_check) { if ($path_to_check[0] === '-') { fwrite(STDERR, 'Invalid usage, expecting psalm [options] [file...]' . PHP_EOL); exit(1); } if (!file_exists($path_to_check)) { fwrite(STDERR, 'Cannot locate ' . $path_to_check . PHP_EOL); exit(1); } $path_to_check = realpath($path_to_check); if (!$path_to_check) { fwrite(STDERR, 'Error getting realpath for file' . PHP_EOL); exit(1); } $paths_to_check[] = $path_to_check; } if (!$paths_to_check) { $paths_to_check = null; } } return $paths_to_check; } /** * @psalm-pure */ function getPsalmHelpText(): string { return <<getMessage() . PHP_EOL); exit(1); } $config->setComposerClassLoader($first_autoloader); return $config; } function update_config_file(Config $config, string $config_file_path, string $baseline_path) : void { if ($config->error_baseline === $baseline_path) { return; } $configFile = $config_file_path; if (is_dir($config_file_path)) { $configFile = Config::locateConfigFile($config_file_path); } if (!$configFile) { fwrite(STDERR, "Don't forget to set errorBaseline=\"{$baseline_path}\" to your config."); return; } $configFileContents = file_get_contents($configFile); if ($config->error_baseline) { $amendedConfigFileContents = preg_replace( '/errorBaseline=".*?"/', "errorBaseline=\"{$baseline_path}\"", $configFileContents ); } else { $endPsalmOpenTag = strpos($configFileContents, '>', (int)strpos($configFileContents, '", $endPsalmOpenTag, 1 ); } else { $amendedConfigFileContents = substr_replace( $configFileContents, " errorBaseline=\"{$baseline_path}\">", $endPsalmOpenTag, 1 ); } } file_put_contents($configFile, $amendedConfigFileContents); } function get_path_to_config(array $options): ?string { $path_to_config = isset($options['c']) && is_string($options['c']) ? realpath($options['c']) : null; if ($path_to_config === false) { fwrite(STDERR, 'Could not resolve path to config ' . (string) ($options['c'] ?? '') . PHP_EOL); exit(1); } return $path_to_config; } /** * @psalm-pure */ function getMemoryLimitInBytes(): int { $limit = ini_get('memory_limit'); // for unlimited = -1 if ($limit < 0) { return -1; } if (preg_match('/^(\d+)(\D?)$/', $limit, $matches)) { $limit = (int)$matches[1]; switch (strtoupper($matches[2] ?? '')) { case 'G': $limit *= 1024 * 1024 * 1024; break; case 'M': $limit *= 1024 * 1024; break; case 'K': $limit *= 1024; break; } } return (int)$limit; }