Skip to content

Commit

Permalink
Always set the JSON_THROW_ON_ERROR flag
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Aug 22, 2023
1 parent 5c3053f commit 141fd3a
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 68 deletions.
4 changes: 2 additions & 2 deletions core-bundle/src/Command/Backup/BackupCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->backupManager->create($config);
} catch (BackupManagerException $e) {
if ($this->isJson($input)) {
$io->writeln(json_encode(['error' => $e->getMessage()]));
$io->writeln(json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR));
} else {
$io->error($e->getMessage());
}
Expand All @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($this->isJson($input)) {
$io->writeln(json_encode($config->getBackup()->toArray()));
$io->writeln(json_encode($config->getBackup()->toArray(), JSON_THROW_ON_ERROR));

return Command::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Command/Backup/BackupListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function formatForJson(array $backups): string
$json[] = $backup->toArray();
}

return json_encode($json);
return json_encode($json, JSON_THROW_ON_ERROR);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Command/Backup/BackupRestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->backupManager->restore($config);
} catch (BackupManagerException $e) {
if ($this->isJson($input)) {
$io->writeln(json_encode(['error' => $e->getMessage()]));
$io->writeln(json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR));
} else {
$io->error($e->getMessage());
}
Expand All @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($this->isJson($input)) {
$io->writeln(json_encode($config->getBackup()->toArray()));
$io->writeln(json_encode($config->getBackup()->toArray(), JSON_THROW_ON_ERROR));

return Command::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Command/CronListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
];
}

$io->writeln(json_encode($list));
$io->writeln(json_encode($list, JSON_THROW_ON_ERROR));

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private function executeMigrations(bool &$dryRun, bool $asJson, string|null $spe
}
}

$actualHash = hash('sha256', json_encode($migrationLabels));
$actualHash = hash('sha256', json_encode($migrationLabels, JSON_THROW_ON_ERROR));

if ($asJson) {
$this->writeNdjson('migration-pending', ['names' => $migrationLabels, 'hash' => $actualHash]);
Expand Down Expand Up @@ -322,7 +322,7 @@ private function executeSchemaDiff(bool $dryRun, bool $asJson, bool $withDeletes
$hasNewCommands = [] !== array_diff($commands, $lastCommands);
$lastCommands = $commands;

$commandsHash = hash('sha256', json_encode($commands));
$commandsHash = hash('sha256', json_encode($commands, JSON_THROW_ON_ERROR));

if ($asJson) {
$this->writeNdjson('schema-pending', [
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Controller/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function checkCookiesAction(): Response
#[Route('/_contao/request_token_script', name: 'contao_frontend_request_token_script')]
public function requestTokenScriptAction(): Response
{
$tokenValue = json_encode($this->container->get('contao.csrf.token_manager')->getDefaultTokenValue());
$tokenValue = json_encode($this->container->get('contao.csrf.token_manager')->getDefaultTokenValue(), JSON_THROW_ON_ERROR);

$response = new Response();
$response->setContent('document.querySelectorAll(\'input[name=REQUEST_TOKEN],input[name$="[REQUEST_TOKEN]"]\').forEach(function(i){i.value='.$tokenValue.'})');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ protected function getResponse(Template $template, ModuleModel $model, Request $
return $response;
}

$template->backupCodes = json_decode((string) $user->backupCodes, true) ?? [];
try {
$template->backupCodes = json_decode((string) $user->backupCodes, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
$template->backupCodes = [];
}

if ('tl_two_factor_generate_backup_codes' === $formId) {
$template->showBackupCodes = true;
Expand Down
11 changes: 7 additions & 4 deletions core-bundle/src/EventListener/Menu/BackendMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ private function buildHeaderMenu(MenuEvent $event, BackendUser $user): void
->setLinkAttribute('data-contao--color-scheme-target', 'label')
->setLinkAttribute(
'data-contao--color-scheme-i18n-value',
json_encode([
'dark' => $this->translator->trans('MSC.darkMode', [], 'contao_default'),
'light' => $this->translator->trans('MSC.lightMode', [], 'contao_default'),
])
json_encode(
[
'dark' => $this->translator->trans('MSC.darkMode', [], 'contao_default'),
'light' => $this->translator->trans('MSC.lightMode', [], 'contao_default'),
],
JSON_THROW_ON_ERROR
)
)
->setExtra('safe_label', true)
->setExtra('translation_domain', false)
Expand Down
19 changes: 11 additions & 8 deletions core-bundle/src/Monolog/ContaoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ public function __construct(
*/
public function __toString(): string
{
return (string) json_encode([
'func' => $this->func,
'action' => $this->action,
'username' => $this->username,
'browser' => $this->browser,
'uri' => $this->uri,
'pageId' => $this->pageId,
]);
return (string) json_encode(
[
'func' => $this->func,
'action' => $this->action,
'username' => $this->username,
'browser' => $this->browser,
'uri' => $this->uri,
'pageId' => $this->pageId,
],
JSON_THROW_ON_ERROR
);
}

public function getFunc(): string
Expand Down
8 changes: 4 additions & 4 deletions core-bundle/src/Picker/PickerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function jsonSerialize(): array
*/
public function urlEncode(): string
{
$data = json_encode($this);
$data = json_encode($this, JSON_THROW_ON_ERROR);

if (\function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
Expand All @@ -111,9 +111,9 @@ public static function urlDecode(string $data): self
$decoded = $uncompressed;
}

$json = @json_decode($decoded, true);

if (null === $json) {
try {
$json = json_decode($decoded, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
throw new \InvalidArgumentException('Invalid JSON data');
}

Expand Down
15 changes: 10 additions & 5 deletions core-bundle/src/Security/TwoFactor/BackupCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function isBackupCode($user, string $code): bool
return false;
}

$backupCodes = json_decode($user->backupCodes, true);

if (null === $backupCodes) {
try {
$backupCodes = json_decode($user->backupCodes, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return false;
}

Expand All @@ -55,7 +55,12 @@ public function invalidateBackupCode($user, string $code): void
}

$codeToInvalidate = false;
$backupCodes = array_values(json_decode($user->backupCodes, true));

try {
$backupCodes = array_values(json_decode($user->backupCodes, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException) {
return;
}

foreach ($backupCodes as $backupCode) {
if (password_verify($code, $backupCode)) {
Expand All @@ -76,7 +81,7 @@ public function invalidateBackupCode($user, string $code): void

unset($backupCodes[$key]);

$user->backupCodes = json_encode(array_values($backupCodes));
$user->backupCodes = json_encode(array_values($backupCodes), JSON_THROW_ON_ERROR);
$user->save();
}

Expand Down
2 changes: 1 addition & 1 deletion core-bundle/tests/Command/MigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,6 @@ private function createBackupManager(bool $backupsEnabled): BackupManager

private function jsonArrayFromNdjson(string $ndjson): array
{
return array_map(static fn (string $line) => json_decode($line, true), explode("\n", trim($ndjson)));
return array_map(static fn (string $line) => json_decode($line, true, 512, JSON_THROW_ON_ERROR), explode("\n", trim($ndjson)));
}
}
4 changes: 2 additions & 2 deletions core-bundle/tests/Command/UserListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testReturnsValidJson(): void
$output = $commandTester->getDisplay();

$this->assertSame(0, $code);
$this->assertNotNull(json_decode($output, true));
$this->assertNotNull(json_decode($output, true, 512, JSON_THROW_ON_ERROR));
}

public function testReturnsValidJsonWithSubset(): void
Expand All @@ -88,7 +88,7 @@ public function testReturnsValidJsonWithSubset(): void
$output = $commandTester->getDisplay();

$this->assertSame(0, $code);
$this->assertNotNull(json_decode($output, true));
$this->assertNotNull(json_decode($output, true, 512, JSON_THROW_ON_ERROR));
}

public function testTakesColumnAsArgument(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testCreatesTheTemplateFromTheClassName(): void
$controller = $this->getTestController();

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_test', $template['templateName']);
}
Expand All @@ -65,7 +65,7 @@ public function testCreatesTheTemplateFromTheTypeFragmentOptions(): void
$controller = $this->getTestController(['type' => 'foo']);

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_foo', $template['templateName']);
}
Expand All @@ -75,7 +75,7 @@ public function testCreatesTheTemplateFromTheTemplateFragmentOption(): void
$controller = $this->getTestController(['template' => 'ce_bar']);

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_bar', $template['templateName']);
}
Expand All @@ -89,7 +89,7 @@ public function testCreatesTheTemplateFromACustomTpl(): void
$model = $this->mockClassWithProperties(ContentModel::class, ['customTpl' => 'ce_bar']);

$response = $controller(new Request(), $model, 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_bar', $template['templateName']);
}
Expand All @@ -108,7 +108,7 @@ public function testDoesNotCreateTheTemplateFromACustomTplInTheBackend(): void
$model = $this->mockClassWithProperties(ContentModel::class, ['customTpl' => 'ce_bar']);

$response = $controller($request, $model, 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_test', $template['templateName']);
}
Expand All @@ -118,7 +118,7 @@ public function testSetsTheClassFromTheType(): void
$controller = $this->getTestController();

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('', $template['cssID']);
$this->assertSame('ce_test', $template['class']);
Expand All @@ -131,7 +131,7 @@ public function testSetsTheHeadlineFromTheModel(): void
$model = $this->mockClassWithProperties(ContentModel::class, ['headline' => serialize(['unit' => 'h6', 'value' => 'foobar'])]);

$response = $controller(new Request(), $model, 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('foobar', $template['headline']);
$this->assertSame('h6', $template['hl']);
Expand All @@ -144,7 +144,7 @@ public function testSetsTheCssIdAndClassFromTheModel(): void
$model = $this->mockClassWithProperties(ContentModel::class, ['cssID' => serialize(['foo', 'bar'])]);

$response = $controller(new Request(), $model, 'main');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame(' id="foo"', $template['cssID']);
$this->assertSame('ce_test bar', $template['class']);
Expand All @@ -155,7 +155,7 @@ public function testSetsTheLayoutSection(): void
$controller = $this->getTestController();

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'left');
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('left', $template['inColumn']);
}
Expand All @@ -165,7 +165,7 @@ public function testSetsTheClasses(): void
$controller = $this->getTestController();

$response = $controller(new Request(), $this->mockClassWithProperties(ContentModel::class), 'main', ['foo', 'bar']);
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('ce_test foo bar', $template['class']);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ public function testSetsTemplateContextForModernFragments(bool $backendScope): v
]);

$response = $controller(new Request(), $model, 'main', ['bar-class', 'baz-class']);
$template = json_decode($response->getContent(), true);
$template = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);

$this->assertSame('text', $template['type']);
$this->assertSame('content_element/text', $template['template']);
Expand Down

0 comments on commit 141fd3a

Please sign in to comment.