1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #7303 from weirdan/rector-json-exceptions

This commit is contained in:
Bruce Weirdan 2022-01-05 19:49:49 +02:00 committed by GitHub
commit b8240e293a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 86 additions and 22 deletions

View File

@ -9,6 +9,7 @@ use DOMAttr;
use DOMDocument;
use DomElement;
use InvalidArgumentException;
use JsonException;
use LogicException;
use OutOfBoundsException;
use Psalm\CodeLocation\Raw;
@ -102,6 +103,7 @@ use function version_compare;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
use const GLOB_NOSORT;
use const JSON_THROW_ON_ERROR;
use const LIBXML_ERR_ERROR;
use const LIBXML_ERR_FATAL;
use const LIBXML_NONET;
@ -2256,7 +2258,13 @@ class Config
$composer_json_path = Composer::getJsonFilePath($this->base_dir);
if (file_exists($composer_json_path)) {
if (!$composer_json = json_decode(file_get_contents($composer_json_path), true)) {
try {
$composer_json = json_decode(file_get_contents($composer_json_path), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$composer_json = null;
}
if (!$composer_json) {
throw new UnexpectedValueException('Invalid composer.json at ' . $composer_json_path);
}
$php_version = $composer_json['require']['php'] ?? null;

View File

@ -2,6 +2,7 @@
namespace Psalm\Config;
use JsonException;
use Psalm\Config;
use Psalm\Exception\ConfigCreationException;
use Psalm\Internal\Analyzer\IssueData;
@ -32,6 +33,7 @@ use function strpos;
use const DIRECTORY_SEPARATOR;
use const GLOB_NOSORT;
use const JSON_THROW_ON_ERROR;
class Creator
{
@ -184,8 +186,19 @@ class Creator
'Problem during config autodiscovery - could not find composer.json during initialization.'
);
}
if (!$composer_json = json_decode(file_get_contents($composer_json_location), true)) {
try {
$composer_json = json_decode(
file_get_contents($composer_json_location),
true,
512,
JSON_THROW_ON_ERROR
);
} catch (JsonException $e) {
throw new ConfigCreationException(
'Invalid composer.json at ' . $composer_json_location . ': ' . $e->getMessage()
);
}
if (!$composer_json) {
throw new ConfigCreationException('Invalid composer.json at ' . $composer_json_location);
}

View File

@ -27,6 +27,8 @@ use function preg_replace;
use function strpos;
use function strtolower;
use const JSON_THROW_ON_ERROR;
class Context
{
/**
@ -770,7 +772,7 @@ class Context
$summary[$k] = $v->getId();
}
return json_encode($summary);
return json_encode($summary, JSON_THROW_ON_ERROR);
}
public function defineGlobals(): void

View File

@ -71,6 +71,8 @@ use function strpos;
use function strtolower;
use function substr;
use const JSON_THROW_ON_ERROR;
/**
* @internal
* This class transform conditions in code into "assertions" that will be reconciled with the type already known of a
@ -149,7 +151,7 @@ class AssertionFinder
if ($var_name) {
if ($candidate_if_types) {
$if_types[$var_name] = [['@' . json_encode($candidate_if_types[0])]];
$if_types[$var_name] = [['@' . json_encode($candidate_if_types[0], JSON_THROW_ON_ERROR)]];
} else {
$if_types[$var_name] = [['!falsy']];
}

View File

@ -19,6 +19,8 @@ use function sort;
use function strpos;
use function substr;
use const JSON_THROW_ON_ERROR;
/**
* @internal
*
@ -110,7 +112,7 @@ class Clause
sort($possibilities[$i]);
}
$this->hash = md5((string) json_encode($possibilities));
$this->hash = md5(json_encode($possibilities, JSON_THROW_ON_ERROR));
}
}

View File

@ -73,6 +73,7 @@ use function substr;
use function version_compare;
use const DIRECTORY_SEPARATOR;
use const JSON_THROW_ON_ERROR;
use const LC_CTYPE;
use const PHP_EOL;
use const PHP_OS;
@ -742,7 +743,10 @@ final class Psalm
$expected_references
);
$type_map_string = json_encode(['files' => $name_file_map, 'references' => $reference_dictionary]);
$type_map_string = json_encode(
['files' => $name_file_map, 'references' => $reference_dictionary],
JSON_THROW_ON_ERROR
);
$providers->file_provider->setContents(
$type_map_location,

View File

@ -4,6 +4,7 @@ namespace Psalm\Internal;
use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use JsonException;
use OutOfBoundsException;
use Phar;
use Psalm\Config;
@ -44,6 +45,7 @@ use function substr_replace;
use function trim;
use const DIRECTORY_SEPARATOR;
use const JSON_THROW_ON_ERROR;
use const PHP_EOL;
use const STDERR;
use const STDIN;
@ -172,8 +174,17 @@ final class CliUtils
if (!file_exists($composer_json_path)) {
return 'vendor';
}
try {
$composer_json = json_decode(file_get_contents($composer_json_path), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
fwrite(
STDERR,
'Invalid composer.json at ' . $composer_json_path . "\n" . $e->getMessage() . "\n"
);
exit(1);
}
if (!$composer_json = json_decode(file_get_contents($composer_json_path), true)) {
if (!$composer_json) {
fwrite(
STDERR,
'Invalid composer.json at ' . $composer_json_path . "\n"

View File

@ -41,6 +41,8 @@ use function sort;
use function strlen;
use function substr;
use const JSON_THROW_ON_ERROR;
/**
* @internal
*/
@ -468,8 +470,8 @@ class TaintFlowGraph extends DataFlowGraph
$new_destination->path_types = array_merge($generated_source->path_types, [$path_type]);
$key = $to_id .
' ' . json_encode($new_destination->specialized_calls) .
' ' . json_encode($new_destination->taints);
' ' . json_encode($new_destination->specialized_calls, JSON_THROW_ON_ERROR) .
' ' . json_encode($new_destination->taints, JSON_THROW_ON_ERROR);
$new_sources[$key] = $new_destination;
}

View File

@ -12,6 +12,8 @@ use function str_replace;
use function strpos;
use function strtotime;
use const JSON_THROW_ON_ERROR;
/**
* Environment variables collector for CI environment.
*
@ -284,7 +286,7 @@ class BuildInfoCollector
if (isset($this->env['GITHUB_EVENT_PATH'])) {
$event_json = file_get_contents((string) $this->env['GITHUB_EVENT_PATH']);
/** @var array */
$event_data = json_decode($event_json, true);
$event_data = json_decode($event_json, true, 512, JSON_THROW_ON_ERROR);
if (isset($event_data['head_commit'])) {
/**

View File

@ -11,6 +11,7 @@ use function rtrim;
use function urlencode;
use const DIRECTORY_SEPARATOR;
use const JSON_THROW_ON_ERROR;
/**
* @internal
@ -71,7 +72,8 @@ class PluginListFactory
'packages' => [],
'packages-dev' => [],
];
$composer_lock_filenames[] = 'data:application/json,' . urlencode(json_encode($stub_composer_lock));
$composer_lock_filenames[] = 'data:application/json,'
. urlencode(json_encode($stub_composer_lock, JSON_THROW_ON_ERROR));
}
return $composer_lock_filenames;

View File

@ -2,6 +2,7 @@
namespace Psalm\Internal\Provider;
use JsonException;
use PhpParser;
use PhpParser\Node\Stmt;
use Psalm\Config;
@ -31,6 +32,7 @@ use function unserialize;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
use const JSON_THROW_ON_ERROR;
use const SCANDIR_SORT_NONE;
/**
@ -193,7 +195,14 @@ class ParserCacheProvider
return [];
}
$hashes_decoded = json_decode($hashes_encoded, true);
try {
$hashes_decoded = json_decode($hashes_encoded, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log('Failed to parse hashes: ' . $e->getMessage());
$this->existing_file_content_hashes = [];
return [];
}
if (!is_array($hashes_decoded)) {
error_log('Unexpected value ' . gettype($hashes_decoded));
@ -281,7 +290,7 @@ class ParserCacheProvider
file_put_contents(
$file_hashes_path,
json_encode($file_content_hashes)
json_encode($file_content_hashes, JSON_THROW_ON_ERROR)
);
}

View File

@ -29,6 +29,7 @@ use const CURLOPT_HTTPHEADER;
use const CURLOPT_POST;
use const CURLOPT_POSTFIELDS;
use const CURLOPT_RETURNTRANSFER;
use const JSON_THROW_ON_ERROR;
use const PHP_EOL;
use const PHP_URL_SCHEME;
use const STDERR;
@ -76,7 +77,7 @@ class Shepherd implements AfterAnalysisInterface
'level' => Config::getInstance()->level
];
$payload = json_encode($data);
$payload = json_encode($data, JSON_THROW_ON_ERROR);
$base_address = $codebase->config->shepherd_host;

View File

@ -57,6 +57,8 @@ use function strpos;
use function strtolower;
use function substr;
use const JSON_THROW_ON_ERROR;
class Reconciler
{
public const RECONCILIATION_OK = 0;
@ -190,11 +192,11 @@ class Reconciler
$nested_negated = !$negated;
/** @var array<string, array<int, array<int, string>>> */
$data = json_decode(substr($new_type_part_part, 2), true);
$data = json_decode(substr($new_type_part_part, 2), true, 512, JSON_THROW_ON_ERROR);
} else {
$nested_negated = $negated;
/** @var array<string, array<int, array<int, string>>> */
$data = json_decode(substr($new_type_part_part, 1), true);
$data = json_decode(substr($new_type_part_part, 1), true, 512, JSON_THROW_ON_ERROR);
}
$existing_types = self::reconcileKeyedTypes(

View File

@ -7,6 +7,8 @@ use RuntimeException;
use function json_encode;
use const JSON_THROW_ON_ERROR;
/** @group PluginManager */
class ComposerLockTest extends TestCase
{
@ -216,6 +218,6 @@ class ComposerLockTest extends TestCase
*/
private function jsonFile($data): string
{
return 'data:application/json,' . json_encode($data);
return 'data:application/json,' . json_encode($data, JSON_THROW_ON_ERROR);
}
}

View File

@ -24,6 +24,8 @@ use function ob_start;
use function preg_replace;
use function unlink;
use const JSON_THROW_ON_ERROR;
class ReportOutputTest extends TestCase
{
public function setUp(): void
@ -666,7 +668,7 @@ echo "Successfully executed the command: " . $prefixedData;';
$this->assertSame(
$issue_data,
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $sarif_report_options), true)
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $sarif_report_options), true, 512, JSON_THROW_ON_ERROR)
);
}
@ -819,7 +821,7 @@ echo $a;';
$this->assertSame(
$issue_data,
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $json_report_options), true)
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $json_report_options), true, 512, JSON_THROW_ON_ERROR)
);
}
@ -853,7 +855,7 @@ echo $a;';
$fixable_issue_counts,
$report_options
);
$this->assertIsArray(json_decode($report->create()));
$this->assertIsArray(json_decode($report->create(), null, 512, JSON_THROW_ON_ERROR));
}
public function testSonarqubeReport(): void
@ -950,7 +952,7 @@ echo $a;';
$this->assertSame(
$issue_data,
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $sonarqube_report_options), true)
json_decode(IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $sonarqube_report_options), true, 512, JSON_THROW_ON_ERROR)
);
}