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

Use PHP native type declarations 🐘 #158

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
41 changes: 13 additions & 28 deletions src/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@ abstract class AbstractConfig implements ArrayAccess, ConfigInterface, Iterator
{
/**
* Stores the configuration data
*
* @var array|null
*/
protected $data = null;
protected ?array $data = null;

/**
* Caches the configuration data
*
* @var array
*/
protected $cache = [];
protected array $cache = [];

/**
* Constructor method and sets default options, if any
*
* @param array $data
*/
public function __construct(array $data)
{
Expand All @@ -44,11 +38,9 @@ public function __construct(array $data)
* Override this method in your own subclass to provide an array of default
* options and values
*
* @return array
*
* @codeCoverageIgnore
*/
protected function getDefaults()
protected function getDefaults(): array
{
return [];
}
Expand All @@ -60,7 +52,7 @@ protected function getDefaults()
/**
* {@inheritDoc}
*/
public function get($key, $default = null)
public function get(string $key, $default = null)
{
if ($this->has($key)) {
return $this->cache[$key];
Expand All @@ -72,7 +64,7 @@ public function get($key, $default = null)
/**
* {@inheritDoc}
*/
public function set($key, $value)
public function set(string $key, $value): void
{
$segs = explode('.', $key);
$root = &$this->data;
Expand Down Expand Up @@ -111,7 +103,7 @@ public function set($key, $value)
/**
* {@inheritDoc}
*/
public function has($key)
public function has(string $key): bool
{
// Check if already cached
if (isset($this->cache[$key])) {
Expand Down Expand Up @@ -139,11 +131,8 @@ public function has($key)

/**
* Merge config from another instance
*
* @param ConfigInterface $config
* @return ConfigInterface
*/
public function merge(ConfigInterface $config)
public function merge(ConfigInterface $config): self
{
$this->data = array_replace_recursive($this->data, $config->all());
$this->cache = [];
Expand All @@ -153,7 +142,7 @@ public function merge(ConfigInterface $config)
/**
* {@inheritDoc}
*/
public function all()
public function all(): array
{
return $this->data;
}
Expand Down Expand Up @@ -183,7 +172,7 @@ public function offsetGet($offset)
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return $this->has($offset);
}
Expand All @@ -197,7 +186,7 @@ public function offsetExists($offset)
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->set($offset, $value);
}
Expand All @@ -210,7 +199,7 @@ public function offsetSet($offset, $value)
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
$this->set($offset, null);
}
Expand Down Expand Up @@ -280,19 +269,15 @@ public function rewind()
* @return bool True if the current index is valid; false otherwise
*/
#[\ReturnTypeWillChange]
public function valid()
public function valid(): bool
{
return (is_array($this->data) ? key($this->data) !== null : false);
}

/**
* Remove a value using the offset as a key
*
* @param string $key
*
* @return void
*/
public function remove($key)
public function remove(string $key): void
{
$this->offsetUnset($key);
}
Expand Down
66 changes: 20 additions & 46 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class Config extends AbstractConfig
{
/**
* All formats supported by Config.
*
* @var array
*/
protected $supportedParsers = [
protected array $supportedParsers = [
'Noodlehaus\Parser\Php',
'Noodlehaus\Parser\Ini',
'Noodlehaus\Parser\Json',
Expand All @@ -37,10 +35,8 @@ class Config extends AbstractConfig

/**
* All formats supported by Config.
*
* @var array
*/
protected $supportedWriters = [
protected array $supportedWriters = [
'Noodlehaus\Writer\Ini',
'Noodlehaus\Writer\Json',
'Noodlehaus\Writer\Xml',
Expand All @@ -52,25 +48,25 @@ class Config extends AbstractConfig
/**
* Static method for loading a Config instance.
*
* @param string|array $values Filenames or string with configuration
* @param ParserInterface $parser Configuration parser
* @param bool $string Enable loading from string
* @param string|array $values Filenames or string with configuration
* @param ParserInterface|null $parser Configuration parser
* @param bool $string Enable loading from string
*
* @return Config
*/
public static function load($values, $parser = null, $string = false)
public static function load($values, ?ParserInterface $parser = null, bool $string = false): self
{
return new static($values, $parser, $string);
}

/**
* Loads a Config instance.
*
* @param string|array $values Filenames or string with configuration
* @param ParserInterface $parser Configuration parser
* @param bool $string Enable loading from string
* @param string|array $values Filenames or string with configuration
* @param ParserInterface|null $parser Configuration parser
* @param bool $string Enable loading from string
*/
public function __construct($values, ParserInterface $parser = null, $string = false)
public function __construct($values, ?ParserInterface $parser = null, bool $string = false)
{
if ($string === true) {
$this->loadFromString($values, $parser);
Expand All @@ -84,12 +80,12 @@ public function __construct($values, ParserInterface $parser = null, $string = f
/**
* Loads configuration from file.
*
* @param string|array $path Filenames or directories with configuration
* @param ParserInterface $parser Configuration parser
* @param string|array $path Filenames or directories with configuration
* @param ParserInterface|null $parser Configuration parser
*
* @throws EmptyDirectoryException If `$path` is an empty directory
*/
protected function loadFromFile($path, ParserInterface $parser = null)
protected function loadFromFile($path, ?ParserInterface $parser = null): void
{
$paths = $this->getValidPath($path);
$this->data = [];
Expand Down Expand Up @@ -124,12 +120,9 @@ protected function loadFromFile($path, ParserInterface $parser = null)
/**
* Writes configuration to file.
*
* @param string $filename Filename to save configuration to
* @param WriterInterface $writer Configuration writer
*
* @throws WriteException if the data could not be written to the file
*/
public function toFile($filename, WriterInterface $writer = null)
public function toFile(string $filename, ?WriterInterface $writer = null): void
{
if ($writer === null) {
// Get file information
Expand Down Expand Up @@ -158,11 +151,8 @@ public function toFile($filename, WriterInterface $writer = null)

/**
* Loads configuration from string.
*
* @param string $configuration String with configuration
* @param ParserInterface $parser Configuration parser
*/
protected function loadFromString($configuration, ParserInterface $parser)
protected function loadFromString(string $configuration, ParserInterface $parser): void
{
$this->data = [];

Expand All @@ -172,25 +162,18 @@ protected function loadFromString($configuration, ParserInterface $parser)

/**
* Writes configuration to string.
*
* @param WriterInterface $writer Configuration writer
* @param boolean $pretty Encode pretty
*/
public function toString(WriterInterface $writer, $pretty = true)
public function toString(WriterInterface $writer, bool $pretty = true): string
{
return $writer->toString($this->all(), $pretty);
}

/**
* Gets a parser for a given file extension.
*
* @param string $extension
*
* @return Noodlehaus\Parser\ParserInterface
*
* @throws UnsupportedFormatException If `$extension` is an unsupported file format
*/
protected function getParser($extension)
protected function getParser(string $extension): ParserInterface
{
foreach ($this->supportedParsers as $parser) {
if (in_array($extension, $parser::getSupportedExtensions())) {
Expand All @@ -205,13 +188,9 @@ protected function getParser($extension)
/**
* Gets a writer for a given file extension.
*
* @param string $extension
*
* @return Noodlehaus\Writer\WriterInterface
*
* @throws UnsupportedFormatException If `$extension` is an unsupported file format
*/
protected function getWriter($extension)
protected function getWriter(string $extension): WriterInterface
{
foreach ($this->supportedWriters as $writer) {
if (in_array($extension, $writer::getSupportedExtensions())) {
Expand All @@ -226,13 +205,9 @@ protected function getWriter($extension)
/**
* Gets an array of paths
*
* @param array $path
*
* @return array
*
* @throws FileNotFoundException If a file is not found at `$path`
*/
protected function getPathFromArray($path)
protected function getPathFromArray(array $path): array
{
$paths = [];

Expand Down Expand Up @@ -270,10 +245,9 @@ protected function getPathFromArray($path)
* @return array
*
* @throws EmptyDirectoryException If `$path` is an empty directory
*
* @throws FileNotFoundException If a file is not found at `$path`
*/
protected function getValidPath($path)
protected function getValidPath($path): array
{
// If `$path` is array
if (is_array($path)) {
Expand Down
14 changes: 4 additions & 10 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface ConfigInterface
*
* @return mixed
*/
public function get($key, $default = null);
public function get(string $key, $default = null);

/**
* Function for setting configuration values, using
Expand All @@ -34,22 +34,16 @@ public function get($key, $default = null);
*
* @return void
*/
public function set($key, $value);
public function set(string $key, $value): void;

/**
* Function for checking if configuration values exist, using
* either simple or nested keys.
*
* @param string $key
*
* @return boolean
*/
public function has($key);
public function has(string $key): bool;

/**
* Get all of the configuration items
*
* @return array
*/
public function all();
public function all(): array;
}
12 changes: 6 additions & 6 deletions src/Exception/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class ParseException extends ErrorException
{
public function __construct(array $error)
{
$message = $error['message'] ?: 'There was an error parsing the file';
$code = isset($error['code']) ? $error['code'] : 0;
$severity = isset($error['type']) ? $error['type'] : 1;
$filename = isset($error['file']) ? $error['file'] : __FILE__;
$lineno = isset($error['line']) ? $error['line'] : __LINE__;
$exception = isset($error['exception']) ? $error['exception'] : null;
$message = $error['message'] ?? 'There was an error parsing the file';
$code = $error['code'] ?? 0;
$severity = $error['type'] ?? 1;
$filename = $error['file'] ?? __FILE__;
$lineno = $error['line'] ?? __LINE__;
$exception = $error['exception'] ?? null;

parent::__construct($message, $code, $severity, $filename, $lineno, $exception);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Exception/WriteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class WriteException extends ErrorException
{
public function __construct(array $error)
{
$message = isset($error['message']) ? $error['message'] : 'There was an error writing the file';
$code = isset($error['code']) ? $error['code'] : 0;
$severity = isset($error['type']) ? $error['type'] : 1;
$filename = isset($error['file']) ? $error['file'] : __FILE__;
$exception = isset($error['exception']) ? $error['exception'] : null;
$message = $error['message'] ?? 'There was an error writing the file';
$code = $error['code'] ?? 0;
$severity = $error['type'] ?? 1;
$filename = $error['file'] ?? __FILE__;
$exception = $error['exception'] ?? null;

parent::__construct($message, $code, $severity, $filename, $exception);
}
Expand Down
Loading