Skip to content

Commit

Permalink
Add JSON output format
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 8, 2016
1 parent 6356f28 commit a5195b2
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 139 deletions.
25 changes: 16 additions & 9 deletions bin/psalm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ ini_set('memory_limit', '2048M');
ini_set('xdebug.max_nesting_level', 512);

// get options from command line
$options = getopt('f:m:hc:', ['help', 'debug', 'config:', 'monochrome', 'show-info:', 'diff', 'file:', 'self-check', 'update-docblocks']);
$options = getopt(
'f:m:hc:',
[
'help', 'debug', 'config:', 'monochrome', 'show-info:', 'diff',
'file:', 'self-check', 'update-docblocks', 'output-format:',
]
);

if (array_key_exists('help', $options)) {
$options['h'] = false;
Expand Down Expand Up @@ -47,6 +53,7 @@ Options:
--diff File to check is a diff
--self-check Psalm checks itself
--update-docblocks Adds correct return types to the given file(s)
--output-format=json Changes the output format
HELP;
Expand All @@ -59,11 +66,12 @@ $debug = array_key_exists('debug', $options);

if (isset($options['f'])) {
$input_paths = is_array($options['f']) ? $options['f'] : [$options['f']];
}
else {
} else {
$input_paths = $argv ? $argv : null;
}

$output_format = isset($options['output-format']) ? $options['output-format'] : ProjectChecker::TYPE_CONSOLE;

$paths_to_check = null;

if ($input_paths) {
Expand Down Expand Up @@ -120,21 +128,20 @@ if ($path_to_config) {
ProjectChecker::setConfigXML($path_to_config);
}

ProjectChecker::$use_color = $use_color;
ProjectChecker::$show_info = $show_info;
$project_checker = new ProjectChecker($use_color, $show_info, $output_format);

$time = microtime(true);

if (array_key_exists('self-check', $options)) {
ProjectChecker::checkDir(dirname(__DIR__) . '/src', $debug);
$project_checker->checkDir(dirname(__DIR__) . '/src', $debug);
} elseif ($paths_to_check === null) {
ProjectChecker::check($debug, $is_diff);
$project_checker->check($debug, $is_diff);
} elseif ($paths_to_check) {
foreach ($paths_to_check as $path_to_check) {
if (is_dir($path_to_check)) {
ProjectChecker::checkDir($path_to_check, $debug, $update_docblocks);
$project_checker->checkDir($path_to_check, $debug, $update_docblocks);
} else {
ProjectChecker::checkFile($path_to_check, $debug, $update_docblocks);
$project_checker->checkFile($path_to_check, $debug, $update_docblocks);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Psalm/Checker/ClassLikeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,10 @@ public static function checkFullyQualifiedClassLikeName(
}
}

FileChecker::addFileReferenceToClass(Config::getInstance()->getBaseDir() . $code_location->file_name, $fq_class_name);
FileChecker::addFileReferenceToClass(
Config::getInstance()->getBaseDir() . $code_location->file_name,
$fq_class_name
);

return true;
}
Expand Down
17 changes: 9 additions & 8 deletions src/Psalm/Checker/FileChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class FileChecker extends SourceChecker implements StatementsSource
public function __construct($file_name, array $preloaded_statements = [])
{
$this->file_path = $file_name;
$this->file_name = Config::getInstance()->shortenFileName($file_name);
$this->file_name = Config::getInstance()->shortenFileName($this->file_path);

self::$file_checkers[$this->file_name] = $this;
self::$file_checkers[$file_name] = $this;
self::$file_checkers[$this->file_path] = $this;

if ($preloaded_statements) {
$this->preloaded_statements = $preloaded_statements;
Expand All @@ -143,7 +143,7 @@ public function check(
$cache = true,
$update_docblocks = false
) {
if ($cache && isset(self::$functions_checked[$this->file_name])) {
if ($cache && isset(self::$functions_checked[$this->file_path])) {
return null;
}

Expand Down Expand Up @@ -343,13 +343,14 @@ protected function getStatements()
}

/**
* @param string $file_name
* @param string $file_path
* @return array<int, \PhpParser\Node>
*/
public static function getStatementsForFile($file_name)
public static function getStatementsForFile($file_path)
{
$stmts = [];

$project_checker = ProjectChecker::getInstance();
$root_cache_directory = Config::getInstance()->getCacheDirectory();
$parser_cache_directory = $root_cache_directory
? $root_cache_directory . '/' . self::PARSER_CACHE_DIRECTORY
Expand All @@ -361,9 +362,9 @@ public static function getStatementsForFile($file_name)

$version = 'parsercache3';

$file_contents = (string)file_get_contents($file_name);
$file_contents = $project_checker->getFileContents($file_path);
$file_content_hash = md5($version . $file_contents);
$name_cache_key = self::getParserCacheKey($file_name);
$name_cache_key = self::getParserCacheKey($file_path);

if (self::$file_content_hashes === null) {
/** @var array<string, string> */
Expand All @@ -379,7 +380,7 @@ public static function getStatementsForFile($file_name)
if (isset(self::$file_content_hashes[$name_cache_key]) &&
$file_content_hash === self::$file_content_hashes[$name_cache_key] &&
is_readable($cache_location) &&
filemtime($cache_location) > filemtime($file_name)
filemtime($cache_location) > filemtime($file_path)
) {
/** @var array<int, \PhpParser\Node> */
$stmts = unserialize((string)file_get_contents($cache_location));
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Checker/FunctionLikeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function check(Context $context, Context $global_context = null)

$statements_checker->registerVariable(
$function_param->name,
$function_param->code_location->line_number
$function_param->code_location->getLineNumber()
);
}

Expand Down
Loading

0 comments on commit a5195b2

Please sign in to comment.