bumped phpstan to level 6

This commit is contained in:
Joby 2022-12-03 11:30:34 -07:00
parent 2224da3f81
commit b3117d36e4
7 changed files with 89 additions and 73 deletions

View file

@ -1,4 +1,4 @@
parameters: parameters:
level: 1 level: 6
paths: paths:
- src - src

View file

@ -8,9 +8,10 @@ use Spyc;
class Config extends SelfReferencingFlatArray implements ConfigInterface class Config extends SelfReferencingFlatArray implements ConfigInterface
{ {
/** @var bool */
public $strict = false; 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); $dir = realpath($dir);
if (!$dir || !is_dir($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<mixed|mixed> */
protected function parse(string $input, string $format): null|array
{ {
$fn = 'parse_' . $format; $fn = 'parse_' . $format;
if (!method_exists($this, $fn)) { if (!method_exists($this, $fn)) {
@ -39,48 +41,53 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
return array(); return array();
} }
protected function parse_yaml($input) /** @return array<mixed|mixed> */
protected function parse_yaml(string $input): array
{ {
return Spyc::YAMLLoadString($input); 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); 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); return Spyc::YAMLDump($this->get(null, $raw), 2);
} }
protected function read_ini($filename) /** @return array<mixed|mixed> */
protected function read_ini(string $filename): array
{ {
return parse_ini_file($filename, true); return parse_ini_file($filename, true);
} }
protected function read_json($filename) /** @return array<mixed|mixed> */
protected function read_json(string $filename): array
{ {
return json_decode(file_get_contents($filename), true); return json_decode(file_get_contents($filename), true);
} }
protected function read_yaml($filename) /** @return array<mixed|mixed> */
protected function read_yaml(string $filename): array
{ {
return Spyc::YAMLLoad($filename); return Spyc::YAMLLoad($filename);
} }
protected function read_yml($filename) /** @return array<mixed|mixed> */
protected function read_yml(string $filename): array
{ {
return $this->read_yaml($filename); 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 (!is_file($filename) || !is_readable($filename)) {
if ($this->strict) { if ($this->strict) {
throw new \Exception("Couldn't read config file \"$filename\""); throw new \Exception("Couldn't read config file \"$filename\"");
} else { } else {
return null; return;
} }
} }
$format = strtolower(preg_replace('/.+\./', '', $filename)); $format = strtolower(preg_replace('/.+\./', '', $filename));
@ -89,7 +96,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
if ($this->strict) { if ($this->strict) {
throw new \Exception("Don't know how to read the format \"$format\""); throw new \Exception("Don't know how to read the format \"$format\"");
} else { } else {
return null; return;
} }
} }
$data = $this->$fn($filename); $data = $this->$fn($filename);
@ -97,7 +104,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
if ($this->strict) { if ($this->strict) {
throw new \Exception("Error reading \"" . $filename . "\""); throw new \Exception("Error reading \"" . $filename . "\"");
} else { } else {
return null; return;
} }
} }
$this->merge($data, $name, $overwrite); $this->merge($data, $name, $overwrite);

View file

@ -1,12 +1,16 @@
<?php <?php
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */ /* Flatrr | https://github.com/jobyone/flatrr | MIT License */
namespace Flatrr\Config; namespace Flatrr\Config;
interface ConfigInterface extends \ArrayAccess use Flatrr\FlatArrayInterface;
interface ConfigInterface extends FlatArrayInterface
{ {
public function readFile($filename, string $name = null, bool $overwrite = false); public function readDir(string $dir, string $name = null, bool $overwrite = false): void;
public function json($raw = false) : string; public function readFile(string $filename, string $name = null, bool $overwrite = false): void;
public function yaml($raw = false) : string; public function json(bool $raw = false): string;
public function get(string $name = null, bool $raw = false); public function yaml(bool $raw = false): string;
public function merge($value, string $name = null, bool $overwrite = false); public function get(null|string $name = null, bool $raw = false): mixed;
public function merge(mixed $value, string $name = null, bool $overwrite = false): static;
} }

View file

@ -6,7 +6,11 @@ class FlatArray implements FlatArrayInterface
{ {
use FlatArrayTrait; use FlatArrayTrait;
public function __construct(array $data = null) /**
* @param null|array<string|mixed> $data
* @return void
*/
public function __construct(null|array $data = null)
{ {
$this->merge($data); $this->merge($data);
} }

View file

@ -7,18 +7,18 @@ use ArrayAccess;
use Iterator; use Iterator;
/** /**
* @extends ArrayAccess<string|mixed> * @extends ArrayAccess<string,mixed>
* @extends Iterator<string|mixed> * @extends Iterator<string,mixed>
*/ */
interface FlatArrayInterface extends ArrayAccess, Iterator interface FlatArrayInterface extends ArrayAccess, Iterator
{ {
public function set(?string $name, $value); public function set(null|string $name, mixed $value): mixed;
public function get(?string $name = null); public function get(null|string $name = null): mixed;
public function unset(?string $name); public function unset(null|string $name): static;
public function merge($value, string $name = null, bool $overwrite = false); public function merge(mixed $value, string $name = null, bool $overwrite = false): static;
public function push(?string $name, $value); public function push(null|string $name, mixed $value): static;
public function pop(?string $name); public function pop(null|string $name): mixed;
public function unshift(?string $name, $value); public function unshift(null|string $name, mixed $value): static;
public function shift(?string $name); public function shift(null|string $name): mixed;
} }

View file

@ -5,70 +5,76 @@ namespace Flatrr;
trait FlatArrayTrait trait FlatArrayTrait
{ {
private $_arrayData = array(); /** @var array<string|mixed> */
private $_flattenCache = array(); private $_arrayData = [];
/** @var array<string|mixed> */
private $_flattenCache = [];
public function push(?string $name, $value) public function push(null|string $name, mixed $value): static
{ {
$arr = $this->flattenSearch($name); $arr = $this->flattenSearch($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return $this;
} }
if ($arr === null) { if ($arr === null) {
$arr = []; $arr = [];
} }
$arr[] = $value; $arr[] = $value;
$this->set($name, $arr); $this->set($name, $arr);
return $this;
} }
public function pop(?string $name) public function pop(null|string $name): mixed
{ {
$arr = $this->flattenSearch($name); $arr = $this->flattenSearch($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return null;
} }
$out = array_pop($arr); $out = array_pop($arr);
$this->set($name, $arr); $this->set($name, $arr);
return $out; return $out;
} }
public function unshift(?string $name, $value) public function unshift(null|string $name, mixed $value): static
{ {
$arr = $this->flattenSearch($name); $arr = $this->flattenSearch($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return $this;
} }
if ($arr === null) { if ($arr === null) {
$arr = []; $arr = [];
} }
array_unshift($arr, $value); array_unshift($arr, $value);
$this->set($name, $arr); $this->set($name, $arr);
return $this;
} }
public function shift(?string $name) public function shift(null|string $name): mixed
{ {
$arr = $this->flattenSearch($name); $arr = $this->flattenSearch($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return null;
} }
$out = array_shift($arr); $out = array_shift($arr);
$this->set($name, $arr); $this->set($name, $arr);
return $out; 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); return $this->flattenSearch($name);
} }
function unset(?string $name) function unset(null|string $name): static
{ {
$this->flattenSearch($name, null, true); $this->flattenSearch($name, null, true);
return $this;
} }
public function offsetSet($name, $value): void public function offsetSet($name, $value): void
@ -120,12 +126,11 @@ trait FlatArrayTrait
* Recursively set a value, with control over whether existing values or new * Recursively set a value, with control over whether existing values or new
* values take precedence * 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])) { if (!isset($this[$name])) {
//easiest possible outcome, old value doesn't exist, so we can just write the value //easiest possible outcome, old value doesn't exist, so we can just write the value
$this->set($name, $value); $this->set($name, $value);
return;
} elseif (is_array($value) && is_array($this->flattenSearch($name))) { } elseif (is_array($value) && is_array($this->flattenSearch($name))) {
//both new and old values are arrays //both new and old values are arrays
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
@ -134,14 +139,13 @@ trait FlatArrayTrait
} }
$this->merge($v, $k, $overwrite); $this->merge($v, $k, $overwrite);
} }
return;
} else { } else {
//old and new values exist, and one or both are not arrays, $overwrite rules the day //old and new values exist, and one or both are not arrays, $overwrite rules the day
if ($overwrite) { if ($overwrite) {
$this->set($name, $value); $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 * string. It sets it if $value exists, otherwise it returns the value if it
* exists. * 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) { if ($value !== null || $unset) {
$this->_flattenCache = array(); $this->_flattenCache = [];
} }
if (!isset($this->_flattenCache[$name])) { if (!isset($this->_flattenCache[$name])) {
$this->_flattenCache[$name] = $this->doFlattenSearch($name, $value, $unset); $this->_flattenCache[$name] = $this->doFlattenSearch($name, $value, $unset);
@ -160,7 +164,7 @@ trait FlatArrayTrait
return $this->_flattenCache[$name]; 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 //check for home strings
if ($name == '' || $name === null) { if ($name == '' || $name === null) {
@ -178,7 +182,7 @@ trait FlatArrayTrait
if ($value !== null) { if ($value !== null) {
foreach ($name as $part) { foreach ($name as $part) {
if (!isset($parent[$part])) { if (!isset($parent[$part])) {
$parent[$part] = array(); $parent[$part] = [];
} }
$parent = &$parent[$part]; $parent = &$parent[$part];
} }
@ -201,7 +205,7 @@ trait FlatArrayTrait
} else { } else {
//set the hard way //set the hard way
if (!is_array($parent)) { if (!is_array($parent)) {
$parent = array(); $parent = [];
} }
$parent[$key] = $value; $parent[$key] = $value;
} }

View file

@ -5,9 +5,10 @@ namespace Flatrr;
class SelfReferencingFlatArray extends FlatArray class SelfReferencingFlatArray extends FlatArray
{ {
/** @var array<string,string> */
protected $cache = []; 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); $out = parent::get($name);
if ($raw) { if ($raw) {
@ -20,28 +21,19 @@ class SelfReferencingFlatArray extends FlatArray
return $out; return $out;
} }
public function set(?string $name, $value) public function set(null|string $name, mixed $value): static
{ {
$this->cache = []; $this->cache = [];
return $this->filter(parent::set($name, $value)); $this->filter(parent::set($name, $value));
return $this;
} }
public function push(?string $name, $value) public function pop(null|string $name): mixed
{
return $this->filter(parent::push($name, $value));
}
public function pop(?string $name)
{ {
return $this->filter(parent::pop($name)); return $this->filter(parent::pop($name));
} }
public function unshift(?string $name, $value) public function shift(null|string $name): mixed
{
return $this->filter(parent::unshift($name, $value));
}
public function shift(?string $name)
{ {
return $this->filter(parent::shift($name)); return $this->filter(parent::shift($name));
} }
@ -51,7 +43,7 @@ class SelfReferencingFlatArray extends FlatArray
return $this->filter(parent::current()); return $this->filter(parent::current());
} }
protected function unescape($value) protected function unescape(mixed $value): mixed
{ {
//map this function onto array values //map this function onto array values
if (is_array($value)) { if (is_array($value)) {
@ -78,7 +70,7 @@ class SelfReferencingFlatArray extends FlatArray
/** /**
* Recursively replace ${var/name} type strings in string values with * 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 //map this function onto array values
if (is_array($value)) { if (is_array($value)) {
@ -104,9 +96,14 @@ class SelfReferencingFlatArray extends FlatArray
return $value; return $value;
} }
protected function filter_regex($matches) /**
* @param array<int,null|string> $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)) { if (!is_array($value)) {
return $value; return $value;
} }