bumped phpstan to level 6
This commit is contained in:
parent
2224da3f81
commit
b3117d36e4
7 changed files with 89 additions and 73 deletions
|
@ -1,4 +1,4 @@
|
|||
parameters:
|
||||
level: 1
|
||||
level: 6
|
||||
paths:
|
||||
- src
|
|
@ -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<mixed|mixed> */
|
||||
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<mixed|mixed> */
|
||||
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<mixed|mixed> */
|
||||
protected function read_ini(string $filename): array
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
protected function read_yaml($filename)
|
||||
/** @return array<mixed|mixed> */
|
||||
protected function read_yaml(string $filename): array
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
<?php
|
||||
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
|
||||
|
||||
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 json($raw = false) : string;
|
||||
public function yaml($raw = false) : string;
|
||||
public function get(string $name = null, bool $raw = false);
|
||||
public function merge($value, string $name = null, bool $overwrite = false);
|
||||
public function readDir(string $dir, string $name = null, bool $overwrite = false): void;
|
||||
public function readFile(string $filename, string $name = null, bool $overwrite = false): void;
|
||||
public function json(bool $raw = false): string;
|
||||
public function yaml(bool $raw = false): string;
|
||||
public function get(null|string $name = null, bool $raw = false): mixed;
|
||||
public function merge(mixed $value, string $name = null, bool $overwrite = false): static;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,11 @@ class FlatArray implements FlatArrayInterface
|
|||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -7,18 +7,18 @@ use ArrayAccess;
|
|||
use Iterator;
|
||||
|
||||
/**
|
||||
* @extends ArrayAccess<string|mixed>
|
||||
* @extends Iterator<string|mixed>
|
||||
* @extends ArrayAccess<string,mixed>
|
||||
* @extends Iterator<string,mixed>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -5,70 +5,76 @@ namespace Flatrr;
|
|||
|
||||
trait FlatArrayTrait
|
||||
{
|
||||
private $_arrayData = array();
|
||||
private $_flattenCache = array();
|
||||
/** @var array<string|mixed> */
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -5,9 +5,10 @@ namespace Flatrr;
|
|||
|
||||
class SelfReferencingFlatArray extends FlatArray
|
||||
{
|
||||
/** @var array<string,string> */
|
||||
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<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)) {
|
||||
return $value;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue