Skip to content

Commit 7303b94

Browse files
committed
Refactor: Update variable names for clarity and consistency in JSON handling
1 parent df28c87 commit 7303b94

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/Command/CompanyAppCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
use MultiFlexi\Application;
1919
use MultiFlexi\RunTemplate;
20+
use Symfony\Component\Console\Command\Command;
2021
use Symfony\Component\Console\Input\InputArgument;
2122
use Symfony\Component\Console\Input\InputInterface;
2223
use Symfony\Component\Console\Input\InputOption;
2324
use Symfony\Component\Console\Output\OutputInterface;
24-
use Symfony\Component\Console\Command\Command;
2525

2626
class CompanyAppCommand extends MultiFlexiCommand
2727
{

src/Command/CredentialProtoTypeCommand.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CredentialProtoTypeCommand extends MultiFlexiCommand
3535
*/
3636
public function validateCredPrototypeJson(string $jsonFile): array
3737
{
38-
return self::validateJson($jsonFile, CredentialProtoType::$credTypeSchema);
38+
return self::validateJson($jsonFile, CredentialProtoType::$credProtoTypeSchema);
3939
}
4040

4141
protected function configure(): void
@@ -404,9 +404,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
404404

405405
return MultiFlexiCommand::FAILURE;
406406
case 'import-json':
407-
$json = $input->getOption('file');
407+
$jsonFile = $input->getOption('file');
408408

409-
if (empty($json) || !file_exists($json)) {
409+
if (empty($jsonFile) || !file_exists($jsonFile)) {
410410
if ($format === 'json') {
411411
$output->writeln(json_encode([
412412
'status' => 'error',
@@ -420,15 +420,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
420420
}
421421

422422
// Load and normalize JSON (flatten localized fields, ensure version)
423-
$rawContent = file_get_contents($json);
423+
$rawContent = file_get_contents($jsonFile);
424424
$decoded = $rawContent !== false ? json_decode($rawContent, true) : null;
425425

426426
if (!\is_array($decoded)) {
427427
if ($format === 'json') {
428428
$output->writeln(json_encode([
429429
'status' => 'error',
430430
'message' => 'Invalid JSON content',
431-
'file' => $json,
431+
'file' => $jsonFile,
432432
], \JSON_PRETTY_PRINT));
433433
} else {
434434
$output->writeln('<error>Invalid JSON content</error>');
@@ -438,7 +438,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
438438
}
439439

440440
$normalized = self::normalizePrototypeJson($decoded, 'en', 'cs');
441-
$normalizedPath = sys_get_temp_dir().'/crprototype.normalized.'.md5($json).'.json';
441+
$normalizedPath = sys_get_temp_dir().'/crprototype.normalized.'.md5($jsonFile).'.json';
442442
file_put_contents($normalizedPath, json_encode($normalized, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
443443

444444
// Validate normalized JSON
@@ -450,13 +450,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
450450
'status' => 'error',
451451
'message' => 'JSON validation failed',
452452
'violations' => $validationResult,
453-
'file' => $json,
453+
'file' => $jsonFile,
454454
'normalized' => $normalizedPath,
455455
'schema' => realpath(CredentialProtoType::$credTypeSchema),
456456
], \JSON_PRETTY_PRINT));
457457
} else {
458-
$output->writeln('<error>JSON validation failed</error>');
458+
$output->writeln('<error>JSON validation failed: '.$jsonFile.'</error>');
459459
$output->writeln('<comment>Schema: '.realpath(CredentialProtoType::$credTypeSchema).'</comment>');
460+
460461
foreach ($validationResult as $violation) {
461462
$output->writeln('<error> '.$violation.' </error>');
462463
}
@@ -475,7 +476,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
475476
$output->writeln(json_encode([
476477
'status' => 'success',
477478
'message' => 'Credential prototype imported successfully',
478-
'file' => $json,
479+
'file' => $jsonFile,
479480
'credential_prototype_id' => $credProto->getMyKey(),
480481
'uuid' => $credProto->getDataValue('uuid'),
481482
'code' => $credProto->getDataValue('code'),
@@ -496,7 +497,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
496497
$output->writeln(json_encode([
497498
'status' => 'error',
498499
'message' => 'Failed to import credential prototype',
499-
'file' => $json,
500+
'file' => $jsonFile,
500501
'imported' => false,
501502
], \JSON_PRETTY_PRINT));
502503
} else {
@@ -509,7 +510,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
509510
$output->writeln(json_encode([
510511
'status' => 'error',
511512
'message' => 'Import failed: '.$e->getMessage(),
512-
'file' => $json,
513+
'file' => $jsonFile,
513514
'imported' => false,
514515
], \JSON_PRETTY_PRINT));
515516
} else {
@@ -520,9 +521,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
520521
}
521522

522523
case 'validate-json':
523-
$json = $input->getOption('file');
524+
$jsonFile = $input->getOption('file');
524525

525-
if (empty($json) || !file_exists($json)) {
526+
if (empty($jsonFile) || !file_exists($jsonFile)) {
526527
if ($format === 'json') {
527528
$output->writeln(json_encode([
528529
'status' => 'error',
@@ -535,20 +536,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
535536
return MultiFlexiCommand::FAILURE;
536537
}
537538

538-
$result = $this->validateCredPrototypeJson($json);
539+
$result = $this->validateCredPrototypeJson($jsonFile);
539540

540541
if ($format === 'json') {
541542
if (empty($result)) {
542543
$output->writeln(json_encode([
543544
'status' => 'success',
544-
'file' => $json,
545+
'file' => $jsonFile,
545546
'schema' => realpath(CredentialProtoType::$credTypeSchema),
546547
'message' => 'JSON is valid',
547548
], \JSON_PRETTY_PRINT));
548549
} else {
549550
$output->writeln(json_encode([
550551
'status' => 'error',
551-
'file' => $json,
552+
'file' => $jsonFile,
552553
'violations' => $result,
553554
'schema' => realpath(CredentialProtoType::$credTypeSchema),
554555
'message' => 'JSON validation failed',
@@ -990,8 +991,9 @@ private static function getLocalizedString(string $key, string $locale = 'cs_CZ'
990991
* - Ensure string values for name/description at root and in fields
991992
* - Ensure version exists (default '1.0')
992993
*
993-
* @param array<string,mixed> $data
994-
* @return array<string,mixed>
994+
* @param array<string, mixed> $data
995+
*
996+
* @return array<string, mixed>
995997
*/
996998
private static function normalizePrototypeJson(array $data, string $primaryLocale = 'en', string $fallbackLocale = 'cs'): array
997999
{
@@ -1006,14 +1008,17 @@ private static function normalizePrototypeJson(array $data, string $primaryLocal
10061008
if (isset($value[$primaryLocale]) && \is_string($value[$primaryLocale])) {
10071009
return $value[$primaryLocale];
10081010
}
1011+
10091012
if (isset($value[$fallbackLocale]) && \is_string($value[$fallbackLocale])) {
10101013
return $value[$fallbackLocale];
10111014
}
1015+
10121016
foreach ($value as $v) {
10131017
if (\is_string($v)) {
10141018
return $v;
10151019
}
10161020
}
1021+
10171022
return '';
10181023
}
10191024

@@ -1024,6 +1029,7 @@ private static function normalizePrototypeJson(array $data, string $primaryLocal
10241029
if (isset($data['name'])) {
10251030
$data['name'] = $extract($data['name']);
10261031
}
1032+
10271033
if (isset($data['description'])) {
10281034
$data['description'] = $extract($data['description']);
10291035
}
@@ -1034,6 +1040,7 @@ private static function normalizePrototypeJson(array $data, string $primaryLocal
10341040
if (isset($field['name'])) {
10351041
$data['fields'][$idx]['name'] = $extract($field['name']);
10361042
}
1043+
10371044
if (isset($field['description'])) {
10381045
$data['fields'][$idx]['description'] = $extract($field['description']);
10391046
}

src/Command/CredentialTypeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
219219
'file' => $file,
220220
], \JSON_PRETTY_PRINT));
221221
} else {
222-
$output->writeln('<error>JSON validation failed</error>');
222+
$output->writeln('<error>JSON '.$jsonFile.' validation failed</error>');
223223

224224
foreach ($validationResult as $violation) {
225225
$output->writeln('<error> '.$violation.' </error>');

0 commit comments

Comments
 (0)