Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be more specific with typehints #250

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(private string $directory, Filesystem|null $filesyst
* @param string $subKeys keys that will be used to find the config
* @uses self::$loadedConfiguration
*/
public function get(string $mainKey, string ...$subKeys) : mixed
public function get(string $mainKey, string ...$subKeys) : int|float|string|bool|array|null
{
try {
// fetch data from json if not loaded already
Expand Down Expand Up @@ -130,11 +130,11 @@ public function getAsString(string $mainKey, string ...$subKeys) : string
{
$data = $this->get($mainKey, ...$subKeys);

if (is_array($data)) {
if (\is_array($data)) {
throw new ConversionException("cannot convert array to string");
}

return strval($data);
return \strval($data);
}

/**
Expand All @@ -151,11 +151,11 @@ public function getAsInt(string $mainKey, string ...$subKeys) : int
{
$data = $this->get($mainKey, ...$subKeys);

if (is_array($data)) {
if (\is_array($data)) {
throw new ConversionException("cannot convert array to int");
}

return intval($data);
return \intval($data);
}

/**
Expand All @@ -172,11 +172,11 @@ public function getAsFloat(string $mainKey, string ...$subKeys) : float
{
$data = $this->get($mainKey, ...$subKeys);

if (is_array($data)) {
if (\is_array($data)) {
throw new ConversionException("cannot convert array to float");
}

return floatval($data);
return \floatval($data);
}

/**
Expand All @@ -193,11 +193,11 @@ public function getAsBool(string $mainKey, string ...$subKeys) : bool
{
$data = $this->get($mainKey, ...$subKeys);

if (is_array($data)) {
if (\is_array($data)) {
throw new ConversionException("cannot convert array to bool");
}

return boolval($data);
return \boolval($data);
}

/**
Expand All @@ -214,7 +214,7 @@ public function getAsArray(string $mainKey, string ...$subKeys) : array
{
$data = $this->get($mainKey, ...$subKeys);

if (!is_array($data)) {
if (!\is_array($data)) {
throw new ConversionException("given data cannot be returned as an array");
}

Expand All @@ -226,22 +226,22 @@ public function getAsArray(string $mainKey, string ...$subKeys) : array
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param mixed $data data to search through
* @param string $subKeys keys that will be used to find the config
* @throws KeyMismatchException if given key cannot be found
* @param int|float|string|bool|array|null $data data to search through
* @param string $subKeys keys that will be used to find the config
* @throws KeyMismatchException if given key cannot be found
*/
private function getSubKeys(mixed $data, string ...$subKeys) : mixed
private function getSubKeys(int|float|string|bool|array|null $data, string ...$subKeys) : int|float|string|bool|array|null
{
// return data if not subkeys are given
if (count($subKeys) === 0) {
if (\count($subKeys) === 0) {
return $data;
}

// extract first key and remove it from subkeys
$firstKey = array_shift($subKeys);
$firstKey = \array_shift($subKeys);

// throw if given key does not exist
if (!is_array($data) || !array_key_exists($firstKey, $data)) {
if (!\is_array($data) || !\array_key_exists($firstKey, $data)) {
throw new KeyMismatchException("configuration mismatch. check you configuration");
}

Expand Down Expand Up @@ -340,7 +340,7 @@ private function loadFile(string $file) : array
$config = $parser->parse($fileContent);

// run $this->replace() to fetch env variables
array_walk_recursive($config, [ $this, "replace" ]);
\array_walk_recursive($config, [ $this, "replace" ]);

return $config;
}
Expand All @@ -356,8 +356,8 @@ private function loadFile(string $file) : array
*/
private function replace(mixed &$item, int|string $key) : void
{
if (is_string($item) && strtolower(substr($item, 0, 4)) === "env:") {
$item = getenv(substr($item, 4));
if (\is_string($item) && \strtolower(\substr($item, 0, 4)) === "env:") {
$item = \getenv(\substr($item, 4));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(string $directory, Filesystem|null $filesystem);
* @param string $mainKey the main key of the configuration. will be used as filename
* @param string $subKeys keys that will be used to find the config
*/
public function get(string $mainKey, string ...$subKeys) : mixed;
public function get(string $mainKey, string ...$subKeys) : int|float|string|bool|array|null;

/**
* returns the required configuration as a string
Expand Down
8 changes: 4 additions & 4 deletions src/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(private string $directory, private Filesystem|null $
* @param string $subKeys keys that will be used to find the config
* @uses self::$payload
*/
public function get(string $mainKey, string ...$subKeys) : mixed
public function get(string $mainKey, string ...$subKeys) : int|float|string|bool|array|null
{
$filePath = $this->directory.$mainKey.".json";
if (!isset($this->payload[$mainKey])) {
Expand Down Expand Up @@ -184,11 +184,11 @@ public function getAsArray(string $mainKey, string ...$subKeys) : array
*
* @author David Lienhard <github@lienhard.win>
* @copyright David Lienhard
* @param mixed $data data to search through
* @param string $subKeys keys that will be used to find the config
* @param int|float|string|bool|array|null $data data to search through
* @param string $subKeys keys that will be used to find the config
* @throws KeyMismatchException if given key cannot be found
*/
private function getSubKeys(mixed $data, string ...$subKeys) : mixed
private function getSubKeys(int|float|string|bool|array|null $data, string ...$subKeys) : int|float|string|bool|array|null
{
// return data if not subkeys are given
if (count($subKeys) === 0) {
Expand Down
Loading