1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Add Psalm\Config::locateConfigFile method for easier path fetching

This commit is contained in:
Brown 2018-11-01 13:05:33 -04:00
parent 5eca4a8b27
commit 1d7ea1292e

View File

@ -324,6 +324,29 @@ class Config
* @psalm-suppress MixedArgument * @psalm-suppress MixedArgument
*/ */
public static function getConfigForPath($path, $base_dir, $output_format) public static function getConfigForPath($path, $base_dir, $output_format)
{
$config_path = self::locateConfigFile($path);
if (!$config_path) {
if ($output_format === ProjectChecker::TYPE_CONSOLE) {
exit(
'Could not locate a config XML file in path ' . $path . '. Have you run \'psalm --init\' ?' .
PHP_EOL
);
}
throw new ConfigException('Config not found for path ' . $path);
}
return self::loadFromXMLFile($config_path, $base_dir);
}
/**
* Searches up a folder hierarchy for the most immediate config.
*
* @throws ConfigException
* @return ?string
*/
public static function locateConfigFile(string $path)
{ {
$dir_path = realpath($path); $dir_path = realpath($path);
@ -335,32 +358,16 @@ class Config
$dir_path = dirname($dir_path); $dir_path = dirname($dir_path);
} }
$config = null;
do { do {
$maybe_path = $dir_path . DIRECTORY_SEPARATOR . Config::DEFAULT_FILE_NAME; $maybe_path = $dir_path . DIRECTORY_SEPARATOR . Config::DEFAULT_FILE_NAME;
if (file_exists($maybe_path) || file_exists($maybe_path .= '.dist')) { if (file_exists($maybe_path) || file_exists($maybe_path .= '.dist')) {
$config = self::loadFromXMLFile($maybe_path, $base_dir); return $maybe_path;
break;
} }
$dir_path = dirname($dir_path); $dir_path = dirname($dir_path);
} while (dirname($dir_path) !== $dir_path); } while (dirname($dir_path) !== $dir_path);
return null;
if (!$config) {
if ($output_format === ProjectChecker::TYPE_CONSOLE) {
exit(
'Could not locate a config XML file in path ' . $path . '. Have you run \'psalm --init\' ?' .
PHP_EOL
);
}
throw new ConfigException('Config not found for path ' . $path);
}
return $config;
} }
/** /**