Skip to content

Commit

Permalink
Add type hint config classes. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Jan 19, 2024
1 parent a5ffe07 commit a728d63
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 94 deletions.
115 changes: 41 additions & 74 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Foxy package.
*
Expand All @@ -20,28 +22,15 @@
*/
final class Config
{
/**
* @var array
*/
private $config;

/**
* @var array
*/
private $defaults;

/**
* @var array
*/
private $cacheEnv = array();
private array $cacheEnv = [];

/**
* Constructor.
*
* @param array $config The config
* @param array $defaults The default values
* @param array $config The config.
* @param array $defaults The default values.
*/
public function __construct(array $config, array $defaults = array())
public function __construct(private array $config, private array $defaults = [])
{
$this->config = $config;
$this->defaults = $defaults;
Expand All @@ -50,27 +39,23 @@ public function __construct(array $config, array $defaults = array())
/**
* Get the array config value.
*
* @param string $key The config key
* @param array $default The default value
*
* @return array
* @param string $key The config key.
* @param array $default The default value.
*/
public function getArray($key, array $default = array())
public function getArray(string $key, array $default = []): array
{
$value = $this->get($key, null);

return null !== $value ? (array) $value : (array) $default;
return $value ?? $default;
}

/**
* Get the config value.
*
* @param string $key The config key
* @param null|mixed $default The default value
*
* @return null|mixed
* @param string $key The config key.
* @param mixed $default The default value.
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
if (\array_key_exists($key, $this->cacheEnv)) {
return $this->cacheEnv[$key];
Expand All @@ -93,24 +78,20 @@ public function get($key, $default = null)
/**
* Convert the config key into environment variable.
*
* @param string $key The config key
*
* @return string
* @param string $key The config key.
*/
private function convertEnvKey($key)
private function convertEnvKey(string $key): string
{
return 'FOXY__'.strtoupper(str_replace('-', '_', $key));
}

/**
* Convert the value of environment variable into php variable.
*
* @param string $value The value of environment variable
* @param string $environmentVariable The environment variable name
*
* @return array|bool|int|string
* @param string $value The value of environment variable.
* @param string $environmentVariable The environment variable name.
*/
private function convertEnvValue($value, $environmentVariable)
private function convertEnvValue(string $value, string $environmentVariable): array|bool|int|string
{
$value = trim(trim(trim($value, '\''), '"'));

Expand All @@ -128,11 +109,9 @@ private function convertEnvValue($value, $environmentVariable)
/**
* Check if the value of environment variable is a boolean.
*
* @param string $value The value of environment variable
*
* @return bool
* @param string $value The value of environment variable.
*/
private function isBoolean($value)
private function isBoolean(string $value): bool
{
$value = strtolower($value);

Expand All @@ -142,65 +121,57 @@ private function isBoolean($value)
/**
* Convert the value of environment variable into a boolean.
*
* @param string $value The value of environment variable
*
* @return bool
* @param string $value The value of environment variable.
*/
private function convertBoolean($value)
private function convertBoolean(string $value): bool
{
return \in_array($value, array('true', '1', 'yes', 'y'), true);
}

/**
* Check if the value of environment variable is a integer.
*
* @param string $value The value of environment variable
*
* @return bool
* @param string $value The value of environment variable.
*/
private function isInteger($value)
private function isInteger(string $value): bool
{
return ctype_digit(trim($value, '-'));
}

/**
* Convert the value of environment variable into a integer.
*
* @param string $value The value of environment variable
*
* @return bool
* @param string $value The value of environment variable.
*/
private function convertInteger($value)
private function convertInteger(string $value): int
{
return (int) $value;
}

/**
* Check if the value of environment variable is a string JSON.
*
* @param string $value The value of environment variable
*
* @return bool
* @param string $value The value of environment variable.
*/
private function isJson($value)
private function isJson(string $value): bool
{
return 0 === strpos($value, '{') || 0 === strpos($value, '[');
}

/**
* Convert the value of environment variable into a json array.
*
* @param string $value The value of environment variable
* @param string $environmentVariable The environment variable name
*
* @return array
* @param string $value The value of environment variable.
* @param string $environmentVariable The environment variable name.
*/
private function convertJson($value, $environmentVariable)
private function convertJson(string $value, string $environmentVariable): array
{
$value = json_decode($value, true);

if (json_last_error()) {
throw new RuntimeException(sprintf('The "%s" environment variable isn\'t a valid JSON', $environmentVariable));
throw new RuntimeException(
sprintf('The "%s" environment variable isn\'t a valid JSON', $environmentVariable)
);
}

return $value;
Expand All @@ -209,12 +180,10 @@ private function convertJson($value, $environmentVariable)
/**
* Get the configured default value or custom default value.
*
* @param string $key The config key
* @param null|mixed $default The default value
*
* @return null|mixed
* @param string $key The config key.
* @param mixed $default The default value.
*/
private function getDefaultValue($key, $default = null)
private function getDefaultValue(string $key, mixed $default = null): mixed
{
$value = null === $default && \array_key_exists($key, $this->defaults)
? $this->defaults[$key]
Expand All @@ -226,13 +195,11 @@ private function getDefaultValue($key, $default = null)
/**
* Get the value defined by the manager name in the key.
*
* @param string $key The config key
* @param array|mixed $value The value
* @param null|mixed $default The default value
*
* @return null|mixed
* @param string $key The config key.
* @param mixed $value The value.
* @param mixed $default The default value.
*/
private function getByManager($key, $value, $default = null)
private function getByManager(string $key, mixed $value, mixed $default = null): mixed
{
if (0 === strpos($key, 'manager-') && \is_array($value)) {
$manager = $manager = $this->get('manager', '');
Expand Down
34 changes: 14 additions & 20 deletions src/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Foxy package.
*
Expand All @@ -25,13 +27,11 @@ abstract class ConfigBuilder
/**
* Build the config of plugin.
*
* @param Composer $composer The composer
* @param array $defaults The default values
* @param null|IOInterface $io The composer input/output
*
* @return Config
* @param Composer $composer The composer.
* @param array $defaults The default values.
* @param null|IOInterface $io The composer input/output.
*/
public static function build(Composer $composer, array $defaults = array(), $io = null)
public static function build(Composer $composer, array $defaults = [], IOInterface $io = null): Config
{
$config = self::getConfigBase($composer, $io);

Expand All @@ -41,12 +41,10 @@ public static function build(Composer $composer, array $defaults = array(), $io
/**
* Get the base of data.
*
* @param Composer $composer The composer
* @param null|IOInterface $io The composer input/output
*
* @return array
* @param Composer $composer The composer.
* @param null|IOInterface $io The composer input/output.
*/
private static function getConfigBase(Composer $composer, $io = null)
private static function getConfigBase(Composer $composer, IOInterface $io = null): array
{
$globalPackageConfig = self::getGlobalConfig($composer, 'composer', $io);
$globalConfig = self::getGlobalConfig($composer, 'config', $io);
Expand All @@ -61,13 +59,11 @@ private static function getConfigBase(Composer $composer, $io = null)
/**
* Get the data of the global config.
*
* @param Composer $composer The composer
* @param string $filename The filename
* @param null|IOInterface $io The composer input/output
*
* @return array
* @param Composer $composer The composer.
* @param string $filename The filename.
* @param null|IOInterface $io The composer input/output.
*/
private static function getGlobalConfig(Composer $composer, $filename, $io = null)
private static function getGlobalConfig(Composer $composer, string $filename, IOInterface $io = null): array
{
$home = self::getComposerHome($composer);
$file = new JsonFile($home.'/'.$filename.'.json');
Expand All @@ -92,10 +88,8 @@ private static function getGlobalConfig(Composer $composer, $filename, $io = nul
* Get the home directory of composer.
*
* @param Composer $composer The composer
*
* @return string
*/
private static function getComposerHome(Composer $composer)
private static function getComposerHome(Composer $composer): string
{
return null !== $composer->getConfig() && $composer->getConfig()->has('home')
? $composer->getConfig()->get('home')
Expand Down

0 comments on commit a728d63

Please sign in to comment.