Skip to content

Commit

Permalink
Extract some reoccurring checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Jan 9, 2025
1 parent 3e212ac commit 628b242
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Config/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ final class Factory
*
* @var int
*/
private $maxIncludeLevel = 1;
private int $maxIncludeLevel = 1;

/**
* Current level of inclusion
*
* @var int
*/
private $includeLevel = 0;
private int $includeLevel = 0;

/**
* Create a CaptainHook configuration
Expand Down Expand Up @@ -156,7 +156,7 @@ private function loadConfigFromFile(Json $file, array $settings): Config
*/
private function extractSettings(array $json): array
{
return isset($json['config']) && is_array($json['config']) ? $json['config'] : [];
return Util::extractListFromJson($json, 'config');
}

/**
Expand All @@ -167,7 +167,7 @@ private function extractSettings(array $json): array
*/
private function extractConditions(mixed $json): array
{
return isset($json['conditions']) && is_array($json['conditions']) ? $json['conditions'] : [];
return Util::extractListFromJson($json, 'conditions');
}

/**
Expand All @@ -178,7 +178,7 @@ private function extractConditions(mixed $json): array
*/
private function extractOptions(mixed $json): array
{
return isset($json['options']) && is_array($json['options']) ? $json['options'] : [];
return Util::extractListFromJson($json, 'options');
}

/**
Expand Down Expand Up @@ -309,10 +309,7 @@ protected function loadIncludedConfigs(array $json, string $path): array
{
$includes = [];
$directory = dirname($path);
$files = isset($json['config'][Config::SETTING_INCLUDES])
&& is_array($json['config'][Config::SETTING_INCLUDES])
? $json['config'][Config::SETTING_INCLUDES]
: [];
$files = Util::extractListFromJson(Util::extractListFromJson($json, 'config'), Config::SETTING_INCLUDES);

foreach ($files as $file) {
$includes[] = $this->includeConfig($directory . DIRECTORY_SEPARATOR . $file);
Expand Down
12 changes: 12 additions & 0 deletions src/Config/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public static function validateConditionsConfig(array $json): void
}
}

/**
* Extracts a list from a json data struct with the necessary safeguards
*
* @param array<string, mixed> $json
* @param string $value
* @return array<string, mixed>
*/
public static function extractListFromJson(array $json, string $value): array
{
return isset($json[$value]) && is_array($json[$value]) ? $json[$value] : [];
}

/**
* Write the config to disk
*
Expand Down

0 comments on commit 628b242

Please sign in to comment.