From b3117d36e47cf8d01a7dd47871d2f924df103942 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Sat, 3 Dec 2022 11:30:34 -0700 Subject: [PATCH] bumped phpstan to level 6 --- phpstan.neon | 2 +- src/Config/Config.php | 33 ++++++++++++--------- src/Config/ConfigInterface.php | 16 ++++++---- src/FlatArray.php | 6 +++- src/FlatArrayInterface.php | 20 ++++++------- src/FlatArrayTrait.php | 50 +++++++++++++++++--------------- src/SelfReferencingFlatArray.php | 35 ++++++++++------------ 7 files changed, 89 insertions(+), 73 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index faa90c7..1711736 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,4 @@ parameters: - level: 1 + level: 6 paths: - src \ No newline at end of file diff --git a/src/Config/Config.php b/src/Config/Config.php index cfb66a7..fa268e8 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -8,9 +8,10 @@ use Spyc; class Config extends SelfReferencingFlatArray implements ConfigInterface { + /** @var bool */ public $strict = false; - public function readDir($dir, string $name = null, bool $overwrite = false) + public function readDir(string $dir, string $name = null, bool $overwrite = false): void { $dir = realpath($dir); if (!$dir || !is_dir($dir)) { @@ -23,7 +24,8 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface } } - protected function parse(string $input, string $format): array + /** @return null|array */ + protected function parse(string $input, string $format): null|array { $fn = 'parse_' . $format; if (!method_exists($this, $fn)) { @@ -39,48 +41,53 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface return array(); } - protected function parse_yaml($input) + /** @return array */ + protected function parse_yaml(string $input): array { return Spyc::YAMLLoadString($input); } - public function json($raw = false): string + public function json(bool $raw = false): string { return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT); } - public function yaml($raw = false): string + public function yaml(bool $raw = false): string { return Spyc::YAMLDump($this->get(null, $raw), 2); } - protected function read_ini($filename) + /** @return array */ + protected function read_ini(string $filename): array { return parse_ini_file($filename, true); } - protected function read_json($filename) + /** @return array */ + protected function read_json(string $filename): array { return json_decode(file_get_contents($filename), true); } - protected function read_yaml($filename) + /** @return array */ + protected function read_yaml(string $filename): array { return Spyc::YAMLLoad($filename); } - protected function read_yml($filename) + /** @return array */ + protected function read_yml(string $filename): array { return $this->read_yaml($filename); } - public function readFile($filename, string $name = null, bool $overwrite = false) + public function readFile(string $filename, string $name = null, bool $overwrite = false): void { if (!is_file($filename) || !is_readable($filename)) { if ($this->strict) { throw new \Exception("Couldn't read config file \"$filename\""); } else { - return null; + return; } } $format = strtolower(preg_replace('/.+\./', '', $filename)); @@ -89,7 +96,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface if ($this->strict) { throw new \Exception("Don't know how to read the format \"$format\""); } else { - return null; + return; } } $data = $this->$fn($filename); @@ -97,7 +104,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface if ($this->strict) { throw new \Exception("Error reading \"" . $filename . "\""); } else { - return null; + return; } } $this->merge($data, $name, $overwrite); diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php index 867ba34..3ad89b4 100644 --- a/src/Config/ConfigInterface.php +++ b/src/Config/ConfigInterface.php @@ -1,12 +1,16 @@ $data + * @return void + */ + public function __construct(null|array $data = null) { $this->merge($data); } diff --git a/src/FlatArrayInterface.php b/src/FlatArrayInterface.php index d8ae79b..53fae50 100644 --- a/src/FlatArrayInterface.php +++ b/src/FlatArrayInterface.php @@ -7,18 +7,18 @@ use ArrayAccess; use Iterator; /** - * @extends ArrayAccess - * @extends Iterator + * @extends ArrayAccess + * @extends Iterator */ interface FlatArrayInterface extends ArrayAccess, Iterator { - public function set(?string $name, $value); - public function get(?string $name = null); - public function unset(?string $name); - public function merge($value, string $name = null, bool $overwrite = false); + public function set(null|string $name, mixed $value): mixed; + public function get(null|string $name = null): mixed; + public function unset(null|string $name): static; + public function merge(mixed $value, string $name = null, bool $overwrite = false): static; - public function push(?string $name, $value); - public function pop(?string $name); - public function unshift(?string $name, $value); - public function shift(?string $name); + public function push(null|string $name, mixed $value): static; + public function pop(null|string $name): mixed; + public function unshift(null|string $name, mixed $value): static; + public function shift(null|string $name): mixed; } diff --git a/src/FlatArrayTrait.php b/src/FlatArrayTrait.php index 8e49847..b4891a0 100644 --- a/src/FlatArrayTrait.php +++ b/src/FlatArrayTrait.php @@ -5,70 +5,76 @@ namespace Flatrr; trait FlatArrayTrait { - private $_arrayData = array(); - private $_flattenCache = array(); + /** @var array */ + private $_arrayData = []; + /** @var array */ + private $_flattenCache = []; - public function push(?string $name, $value) + public function push(null|string $name, mixed $value): static { $arr = $this->flattenSearch($name); if ($arr !== null && !is_array($arr)) { - return; + return $this; } if ($arr === null) { $arr = []; } $arr[] = $value; $this->set($name, $arr); + return $this; } - public function pop(?string $name) + public function pop(null|string $name): mixed { $arr = $this->flattenSearch($name); if ($arr !== null && !is_array($arr)) { - return; + return null; } $out = array_pop($arr); $this->set($name, $arr); return $out; } - public function unshift(?string $name, $value) + public function unshift(null|string $name, mixed $value): static { $arr = $this->flattenSearch($name); if ($arr !== null && !is_array($arr)) { - return; + return $this; } if ($arr === null) { $arr = []; } array_unshift($arr, $value); $this->set($name, $arr); + return $this; } - public function shift(?string $name) + public function shift(null|string $name): mixed { $arr = $this->flattenSearch($name); if ($arr !== null && !is_array($arr)) { - return; + return null; } $out = array_shift($arr); $this->set($name, $arr); return $out; } - public function set(?string $name, $value) + public function set(null|string $name, mixed $value): static { - return $this->flattenSearch($name, $value); + $this->flattenSearch($name, $value); + return $this; } - public function get(?string $name = null) + public function get(null|string $name = null): mixed { return $this->flattenSearch($name); } - function unset(?string $name) + function unset(null|string $name): static { $this->flattenSearch($name, null, true); + return $this; } public function offsetSet($name, $value): void @@ -120,12 +126,11 @@ trait FlatArrayTrait * Recursively set a value, with control over whether existing values or new * values take precedence */ - public function merge($value, string $name = null, bool $overwrite = false) + public function merge(mixed $value, string $name = null, bool $overwrite = false): static { if (!isset($this[$name])) { //easiest possible outcome, old value doesn't exist, so we can just write the value $this->set($name, $value); - return; } elseif (is_array($value) && is_array($this->flattenSearch($name))) { //both new and old values are arrays foreach ($value as $k => $v) { @@ -134,14 +139,13 @@ trait FlatArrayTrait } $this->merge($v, $k, $overwrite); } - return; } else { //old and new values exist, and one or both are not arrays, $overwrite rules the day if ($overwrite) { $this->set($name, $value); } - return; } + return $this; } /** @@ -149,10 +153,10 @@ trait FlatArrayTrait * string. It sets it if $value exists, otherwise it returns the value if it * exists. */ - protected function flattenSearch(?string $name, $value = null, $unset = false) + protected function flattenSearch(null|string $name, mixed $value = null, bool $unset = false): mixed { if ($value !== null || $unset) { - $this->_flattenCache = array(); + $this->_flattenCache = []; } if (!isset($this->_flattenCache[$name])) { $this->_flattenCache[$name] = $this->doFlattenSearch($name, $value, $unset); @@ -160,7 +164,7 @@ trait FlatArrayTrait return $this->_flattenCache[$name]; } - protected function doFlattenSearch(?string $name, $value = null, $unset = false) + protected function doFlattenSearch(null|string $name, mixed $value = null, bool $unset = false): mixed { //check for home strings if ($name == '' || $name === null) { @@ -178,7 +182,7 @@ trait FlatArrayTrait if ($value !== null) { foreach ($name as $part) { if (!isset($parent[$part])) { - $parent[$part] = array(); + $parent[$part] = []; } $parent = &$parent[$part]; } @@ -201,7 +205,7 @@ trait FlatArrayTrait } else { //set the hard way if (!is_array($parent)) { - $parent = array(); + $parent = []; } $parent[$key] = $value; } diff --git a/src/SelfReferencingFlatArray.php b/src/SelfReferencingFlatArray.php index 542d3ed..cff59ef 100644 --- a/src/SelfReferencingFlatArray.php +++ b/src/SelfReferencingFlatArray.php @@ -5,9 +5,10 @@ namespace Flatrr; class SelfReferencingFlatArray extends FlatArray { + /** @var array */ protected $cache = []; - public function get(string $name = null, bool $raw = false, $unescape = true) + public function get(string $name = null, bool $raw = false, bool $unescape = true): mixed { $out = parent::get($name); if ($raw) { @@ -20,28 +21,19 @@ class SelfReferencingFlatArray extends FlatArray return $out; } - public function set(?string $name, $value) + public function set(null|string $name, mixed $value): static { $this->cache = []; - return $this->filter(parent::set($name, $value)); + $this->filter(parent::set($name, $value)); + return $this; } - public function push(?string $name, $value) - { - return $this->filter(parent::push($name, $value)); - } - - public function pop(?string $name) + public function pop(null|string $name): mixed { return $this->filter(parent::pop($name)); } - public function unshift(?string $name, $value) - { - return $this->filter(parent::unshift($name, $value)); - } - - public function shift(?string $name) + public function shift(null|string $name): mixed { return $this->filter(parent::shift($name)); } @@ -51,7 +43,7 @@ class SelfReferencingFlatArray extends FlatArray return $this->filter(parent::current()); } - protected function unescape($value) + protected function unescape(mixed $value): mixed { //map this function onto array values if (is_array($value)) { @@ -78,7 +70,7 @@ class SelfReferencingFlatArray extends FlatArray /** * Recursively replace ${var/name} type strings in string values with */ - protected function filter($value) + protected function filter(mixed $value): mixed { //map this function onto array values if (is_array($value)) { @@ -104,9 +96,14 @@ class SelfReferencingFlatArray extends FlatArray return $value; } - protected function filter_regex($matches) + /** + * @param array $matches + * @return string + */ + protected function filter_regex(array $matches): string { - if (null !== $value = $this->get($matches[1], false, false)) { + $value = $this->get($matches[1], false, false); + if ($value !== null) { if (!is_array($value)) { return $value; }