Skip to content

Commit 0b3ce1d

Browse files
committed
DebugConfigResolverCommand.php: Enhance error handling
1 parent 0fa0622 commit 0b3ce1d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/bundle/Core/Command/DebugConfigResolverCommand.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Ibexa\Bundle\Core\Command;
99

1010
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
11+
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
1112
use Ibexa\Core\MVC\Symfony\SiteAccess;
1213
use Symfony\Component\Console\Attribute\AsCommand;
1314
use Symfony\Component\Console\Command\Command;
@@ -73,7 +74,7 @@ public function configure(): void
7374
'sort',
7475
null,
7576
InputOption::VALUE_REQUIRED,
76-
'Sort list of hashes by this key, ascending. For example: --sort template'
77+
'Sort list of hashes by this key, ascending. For example: --sort position'
7778
);
7879
$this->addOption(
7980
'reverse-sort',
@@ -105,17 +106,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105106
$parameter = $input->getArgument('parameter');
106107
$namespace = $input->getOption('namespace');
107108
$scope = $input->getOption('scope');
109+
$sort = $input->getOption('sort');
108110
$parameterData = $this->configResolver->getParameter($parameter, $namespace, $scope);
109111

110-
if (null !== ($sort = $input->getOption('sort')) && is_array($parameterData) && is_array($parameterData[0]) && array_key_exists($sort, $parameterData[0]) && is_scalar($parameterData[0][$sort])) {
112+
if (null !== $sort && !empty($parameterData)) {
113+
if (!is_array($parameterData)) {
114+
throw new InvalidArgumentException('--sort', "'$parameter' isn't a list. Sort can be used only on list.");
115+
}
116+
if (!array_is_list($parameterData)) {
117+
throw new InvalidArgumentException('--sort', "'$parameter' is a hash or an object. Sort can be used only on list.");
118+
}
119+
if (!array_key_exists($sort, $parameterData[0])) {
120+
throw new InvalidArgumentException('--sort', "'$sort' property doesn't exist on '$parameter' list items.");
121+
}
122+
if (!is_scalar($parameterData[0][$sort])) {
123+
throw new InvalidArgumentException('--sort', "'{$parameter}[n][{$sort}]' properties aren't scalar and can't be sorted.");
124+
}
111125
if ($input->getOption('reverse-sort')) {
112-
usort($parameterData, static function ($a, $b) use ($sort) {
113-
return $b[$sort] <=> $a[$sort];
114-
});
126+
usort($parameterData, static fn (array $a, array $b): int => $b[$sort] <=> $a[$sort]);
115127
} else {
116-
usort($parameterData, static function ($a, $b) use ($sort) {
117-
return $a[$sort] <=> $b[$sort];
118-
});
128+
usort($parameterData, static fn (array $a, array $b): int => $a[$sort] <=> $b[$sort]);
119129
}
120130
}
121131

0 commit comments

Comments
 (0)