diff --git a/phpstan.neon b/phpstan.neon index b95427a..ef6cdd2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -11,9 +11,6 @@ parameters: paths: - src - scanDirectories: - - /usr/share/icinga-php - ignoreErrors: - messages: diff --git a/src/BaseFilter.php b/src/BaseFilter.php index 267decb..0b0a946 100644 --- a/src/BaseFilter.php +++ b/src/BaseFilter.php @@ -6,8 +6,8 @@ trait BaseFilter { - /** @var Rule Base filter */ - private $baseFilter; + /** @var ?Rule Base filter */ + private ?Rule $baseFilter = null; /** * Get whether a base filter has been set @@ -24,7 +24,7 @@ public function hasBaseFilter(): bool * * @return ?Rule */ - public function getBaseFilter() + public function getBaseFilter(): ?Rule { return $this->baseFilter; } @@ -32,11 +32,11 @@ public function getBaseFilter() /** * Set the base filter * - * @param Rule $baseFilter + * @param ?Rule $baseFilter * * @return $this */ - public function setBaseFilter(Rule $baseFilter = null): self + public function setBaseFilter(?Rule $baseFilter = null): static { $this->baseFilter = $baseFilter; diff --git a/src/Contract/Filterable.php b/src/Contract/Filterable.php index 2a6316a..a6fcdb3 100644 --- a/src/Contract/Filterable.php +++ b/src/Contract/Filterable.php @@ -11,7 +11,7 @@ interface Filterable * * @return Filter\Chain */ - public function getFilter(); + public function getFilter(): Filter\Chain; /** * Add a filter to the query @@ -23,7 +23,7 @@ public function getFilter(); * * @return $this */ - public function filter(Filter\Rule $filter); + public function filter(Filter\Rule $filter): static; /** * Add a filter to the query @@ -35,7 +35,7 @@ public function filter(Filter\Rule $filter); * * @return $this */ - public function orFilter(Filter\Rule $filter); + public function orFilter(Filter\Rule $filter): static; /** * Add a filter to the query @@ -47,7 +47,7 @@ public function orFilter(Filter\Rule $filter); * * @return $this */ - public function notFilter(Filter\Rule $filter); + public function notFilter(Filter\Rule $filter): static; /** * Add a filter to the query @@ -59,5 +59,5 @@ public function notFilter(Filter\Rule $filter); * * @return $this */ - public function orNotFilter(Filter\Rule $filter); + public function orNotFilter(Filter\Rule $filter): static; } diff --git a/src/Contract/Paginatable.php b/src/Contract/Paginatable.php index 3e2a4ee..07470af 100644 --- a/src/Contract/Paginatable.php +++ b/src/Contract/Paginatable.php @@ -11,14 +11,14 @@ interface Paginatable extends Countable * * @return bool */ - public function hasLimit(); + public function hasLimit(): bool; /** * Get the limit * * @return int|null */ - public function getLimit(); + public function getLimit(): ?int; /** * Set the limit @@ -28,21 +28,21 @@ public function getLimit(); * * @return $this */ - public function limit($limit); + public function limit(?int $limit): static; /** * Get whether an offset is set * * @return bool */ - public function hasOffset(); + public function hasOffset(): bool; /** * Get the offset * * @return int|null */ - public function getOffset(); + public function getOffset(): ?int; /** * Set the offset @@ -52,5 +52,5 @@ public function getOffset(); * * @return $this */ - public function offset($offset); + public function offset(?int $offset): static; } diff --git a/src/Contract/PluginLoader.php b/src/Contract/PluginLoader.php index 1be779c..5c5bdd7 100644 --- a/src/Contract/PluginLoader.php +++ b/src/Contract/PluginLoader.php @@ -17,5 +17,5 @@ interface PluginLoader * * @return string|false FQN of the plugin's class if found, false otherwise */ - public function load($name); + public function load(string $name): false|string; } diff --git a/src/Contract/Translator.php b/src/Contract/Translator.php index 85ab515..15ae8f6 100644 --- a/src/Contract/Translator.php +++ b/src/Contract/Translator.php @@ -10,25 +10,25 @@ interface Translator /** * Translate a message * - * @param string $message - * @param string $context Message context + * @param string $message + * @param string|null $context Message context * * @return string Translated message or original message if no translation is found */ - public function translate($message, $context = null); + public function translate(string $message, ?string $context = null): string; /** * Translate a message in the given domain * * If no translation is found in the specified domain, the translation is also searched for in the default domain. * - * @param string $domain - * @param string $message - * @param string $context Message context + * @param string $domain + * @param string $message + * @param string|null $context Message context * * @return string Translated message or original message if no translation is found */ - public function translateInDomain($domain, $message, $context = null); + public function translateInDomain(string $domain, string $message, ?string $context = null): string; /** * Translate a plural message @@ -36,14 +36,14 @@ public function translateInDomain($domain, $message, $context = null); * The returned message is based on the given number to decide between the singular and plural forms. * That is also the case if no translation is found. * - * @param string $singular Singular message - * @param string $plural Plural message - * @param int $number Number to decide between the returned singular and plural forms - * @param string $context Message context + * @param string $singular Singular message + * @param string $plural Plural message + * @param int $number Number to decide between the returned singular and plural forms + * @param string|null $context Message context * * @return string Translated message or original message if no translation is found */ - public function translatePlural($singular, $plural, $number, $context = null); + public function translatePlural(string $singular, string $plural, int $number, ?string $context = null): string; /** * Translate a plural message in the given domain @@ -53,13 +53,13 @@ public function translatePlural($singular, $plural, $number, $context = null); * The returned message is based on the given number to decide between the singular and plural forms. * That is also the case if no translation is found. * - * @param string $domain - * @param string $singular Singular message - * @param string $plural Plural message - * @param int $number Number to decide between the returned singular and plural forms - * @param string $context Message context + * @param string $domain + * @param string $singular Singular message + * @param string $plural Plural message + * @param int $number Number to decide between the returned singular and plural forms + * @param string|null $context Message context * * @return string Translated message or original message if no translation is found */ - public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null); + public function translatePluralInDomain(string $domain, string $singular, string $plural, int $number, ?string $context = null): string; } diff --git a/src/Data.php b/src/Data.php index b12306c..d54d598 100644 --- a/src/Data.php +++ b/src/Data.php @@ -5,14 +5,14 @@ class Data { /** @var array */ - protected $data = []; + protected array $data = []; /** * Check whether there's any data * * @return bool */ - public function isEmpty() + public function isEmpty(): bool { return empty($this->data); } @@ -24,7 +24,7 @@ public function isEmpty() * * @return bool */ - public function has($name) + public function has(string $name): bool { return array_key_exists($name, $this->data); } @@ -37,7 +37,7 @@ public function has($name) * * @return mixed */ - public function get($name, $default = null) + public function get(string $name, mixed $default = null): mixed { if ($this->has($name)) { return $this->data[$name]; @@ -54,7 +54,7 @@ public function get($name, $default = null) * * @return $this */ - public function set($name, $value) + public function set(string $name, mixed $value): static { $this->data[$name] = $value; @@ -68,7 +68,7 @@ public function set($name, $value) * * @return $this */ - public function merge(self $with) + public function merge(self $with): static { $this->data = array_merge($this->data, $with->data); @@ -80,7 +80,7 @@ public function merge(self $with) * * @return $this */ - public function clear() + public function clear(): static { $this->data = []; diff --git a/src/Filter.php b/src/Filter.php index 3bbdd36..c8b9afd 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -36,7 +36,7 @@ protected function __construct() * * @return bool */ - public static function match(Rule $rule, $row) + public static function match(Rule $rule, array|object $row): bool { if (! is_object($row)) { if (is_array($row)) { @@ -59,7 +59,7 @@ public static function match(Rule $rule, $row) * * @return Chain */ - public static function all(Rule ...$rules) + public static function all(Rule ...$rules): Chain { return new All(...$rules); } @@ -72,7 +72,7 @@ public static function all(Rule ...$rules) * * @return bool */ - protected function matchAll(All $rules, $row) + protected function matchAll(All $rules, object $row): bool { foreach ($rules as $rule) { if (! $this->performMatch($rule, $row)) { @@ -90,7 +90,7 @@ protected function matchAll(All $rules, $row) * * @return Chain */ - public static function any(Rule ...$rules) + public static function any(Rule ...$rules): Chain { return new Any(...$rules); } @@ -103,7 +103,7 @@ public static function any(Rule ...$rules) * * @return bool */ - protected function matchAny(Any $rules, $row) + protected function matchAny(Any $rules, object $row): bool { foreach ($rules as $rule) { if ($this->performMatch($rule, $row)) { @@ -121,7 +121,7 @@ protected function matchAny(Any $rules, $row) * * @return Chain */ - public static function none(Rule ...$rules) + public static function none(Rule ...$rules): Chain { return new None(...$rules); } @@ -134,7 +134,7 @@ public static function none(Rule ...$rules) * * @return bool */ - protected function matchNone(None $rules, $row) + protected function matchNone(None $rules, object $row): bool { foreach ($rules as $rule) { if ($this->performMatch($rule, $row)) { @@ -153,7 +153,7 @@ protected function matchNone(None $rules, $row) * * @return Condition */ - public static function equal($column, $value) + public static function equal(string $column, $value): Condition { return new Equal($column, $value); } @@ -166,17 +166,8 @@ public static function equal($column, $value) * * @return bool */ - protected function matchEqual($rule, $row) + protected function matchEqual(Equal|Unequal $rule, object $row): bool { - if (! $rule instanceof Equal && ! $rule instanceof Unequal) { - throw new InvalidArgumentException(sprintf( - 'Rule must be of type %s or %s, got %s instead', - Equal::class, - Unequal::class, - get_php_type($rule) - )); - } - $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); $this->normalizeTypes($rowValue, $value); @@ -200,11 +191,11 @@ protected function matchEqual($rule, $row) * Performs a wildcard search if the value contains asterisks. * * @param string $column - * @param string|string[] $value + * @param string|string[]|null $value * * @return Condition */ - public static function like($column, $value) + public static function like(string $column, string|array|null $value): Condition { return new Like($column, $value); } @@ -217,17 +208,8 @@ public static function like($column, $value) * * @return bool */ - protected function matchSimilar($rule, $row) + protected function matchSimilar(Like|Unlike $rule, object $row): bool { - if (! $rule instanceof Like && ! $rule instanceof Unlike) { - throw new InvalidArgumentException(sprintf( - 'Rule must be of type %s or %s, got %s instead', - Like::class, - Unlike::class, - get_php_type($rule) - )); - } - $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); $this->normalizeTypes($rowValue, $value); @@ -254,7 +236,7 @@ protected function matchSimilar($rule, $row) * * @return bool */ - protected function performEqualityMatch($value, $rowValue, $ignoreCase = false) + protected function performEqualityMatch(mixed $value, mixed $rowValue, bool $ignoreCase = false): bool { if ($ignoreCase && is_string($rowValue)) { $rowValue = strtolower($rowValue); @@ -280,7 +262,7 @@ protected function performEqualityMatch($value, $rowValue, $ignoreCase = false) * * @return bool */ - protected function performSimilarityMatch($value, $rowValue, $ignoreCase = false) + protected function performSimilarityMatch(mixed $value, mixed $rowValue, bool $ignoreCase = false): bool { if ($ignoreCase && is_string($rowValue)) { $rowValue = strtolower($rowValue); @@ -323,7 +305,7 @@ protected function performSimilarityMatch($value, $rowValue, $ignoreCase = false * * @return Condition */ - public static function unequal($column, $value) + public static function unequal(string $column, $value): Condition { return new Unequal($column, $value); } @@ -336,7 +318,7 @@ public static function unequal($column, $value) * * @return bool */ - protected function matchUnequal(Unequal $rule, $row) + protected function matchUnequal(Unequal $rule, object $row): bool { return ! $this->matchEqual($rule, $row); } @@ -347,11 +329,11 @@ protected function matchUnequal(Unequal $rule, $row) * Performs a wildcard search if the value contains asterisks. * * @param string $column - * @param string|string[] $value + * @param string|string[]|null $value * * @return Condition */ - public static function unlike($column, $value) + public static function unlike(string $column, string|array|null $value): Condition { return new Unlike($column, $value); } @@ -364,7 +346,7 @@ public static function unlike($column, $value) * * @return bool */ - protected function matchUnlike(Unlike $rule, $row) + protected function matchUnlike(Unlike $rule, object $row): bool { return ! $this->matchSimilar($rule, $row); } @@ -377,7 +359,7 @@ protected function matchUnlike(Unlike $rule, $row) * * @return Condition */ - public static function greaterThan($column, $value) + public static function greaterThan(string $column, $value): Condition { return new GreaterThan($column, $value); } @@ -390,7 +372,7 @@ public static function greaterThan($column, $value) * * @return bool */ - protected function matchGreaterThan(GreaterThan $rule, $row) + protected function matchGreaterThan(GreaterThan $rule, object $row): bool { $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); @@ -406,7 +388,7 @@ protected function matchGreaterThan(GreaterThan $rule, $row) * * @return Condition */ - public static function lessThan($column, $value) + public static function lessThan(string $column, $value): Condition { return new LessThan($column, $value); } @@ -419,7 +401,7 @@ public static function lessThan($column, $value) * * @return bool */ - protected function matchLessThan(LessThan $rule, $row) + protected function matchLessThan(LessThan $rule, object $row): bool { $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); @@ -435,7 +417,7 @@ protected function matchLessThan(LessThan $rule, $row) * * @return Condition */ - public static function greaterThanOrEqual($column, $value) + public static function greaterThanOrEqual(string $column, $value): Condition { return new GreaterThanOrEqual($column, $value); } @@ -448,7 +430,7 @@ public static function greaterThanOrEqual($column, $value) * * @return bool */ - protected function matchGreaterThanOrEqual(GreaterThanOrEqual $rule, $row) + protected function matchGreaterThanOrEqual(GreaterThanOrEqual $rule, object $row): bool { $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); @@ -464,7 +446,7 @@ protected function matchGreaterThanOrEqual(GreaterThanOrEqual $rule, $row) * * @return Condition */ - public static function lessThanOrEqual($column, $value) + public static function lessThanOrEqual(string $column, $value): Condition { return new LessThanOrEqual($column, $value); } @@ -477,7 +459,7 @@ public static function lessThanOrEqual($column, $value) * * @return bool */ - protected function matchLessThanOrEqual(LessThanOrEqual $rule, $row) + protected function matchLessThanOrEqual(LessThanOrEqual $rule, object $row): bool { $rowValue = $this->extractValue($rule->getColumn(), $row); $value = $rule->getValue(); @@ -493,37 +475,27 @@ protected function matchLessThanOrEqual(LessThanOrEqual $rule, $row) * * @return bool */ - protected function performMatch(Rule $rule, $row) + protected function performMatch(Rule $rule, object $row): bool { - switch (true) { - case $rule instanceof All: - return $this->matchAll($rule, $row); - case $rule instanceof Any: - return $this->matchAny($rule, $row); - case $rule instanceof Like: - return $this->matchSimilar($rule, $row); - case $rule instanceof Equal: - return $this->matchEqual($rule, $row); - case $rule instanceof GreaterThan: - return $this->matchGreaterThan($rule, $row); - case $rule instanceof GreaterThanOrEqual: - return $this->matchGreaterThanOrEqual($rule, $row); - case $rule instanceof LessThan: - return $this->matchLessThan($rule, $row); - case $rule instanceof LessThanOrEqual: - return $this->matchLessThanOrEqual($rule, $row); - case $rule instanceof None: - return $this->matchNone($rule, $row); - case $rule instanceof Unequal: - return $this->matchUnequal($rule, $row); - case $rule instanceof Unlike: - return $this->matchUnlike($rule, $row); - default: - throw new InvalidArgumentException(sprintf( + return match (true) { + $rule instanceof All => $this->matchAll($rule, $row), + $rule instanceof Any => $this->matchAny($rule, $row), + $rule instanceof Like => $this->matchSimilar($rule, $row), + $rule instanceof Equal => $this->matchEqual($rule, $row), + $rule instanceof GreaterThan => $this->matchGreaterThan($rule, $row), + $rule instanceof GreaterThanOrEqual => $this->matchGreaterThanOrEqual($rule, $row), + $rule instanceof LessThan => $this->matchLessThan($rule, $row), + $rule instanceof LessThanOrEqual => $this->matchLessThanOrEqual($rule, $row), + $rule instanceof None => $this->matchNone($rule, $row), + $rule instanceof Unequal => $this->matchUnequal($rule, $row), + $rule instanceof Unlike => $this->matchUnlike($rule, $row), + default => throw new InvalidArgumentException( + sprintf( 'Unable to match filter. Rule type %s is unknown', get_class($rule) - )); - } + ) + ), + }; } /** @@ -534,13 +506,9 @@ protected function performMatch(Rule $rule, $row) * * @return mixed */ - protected function extractValue($column, $row) + protected function extractValue(string $column, object $row): mixed { - try { - return $row->{$column}; - } catch (Throwable $_) { - return null; - } + return $row->$column ?? null; } /** @@ -554,7 +522,7 @@ protected function extractValue($column, $row) * * @return void */ - protected function normalizeTypes($rowValue, &$value) + protected function normalizeTypes(mixed $rowValue, mixed &$value): void { if ($rowValue === null || $value === null) { return; diff --git a/src/Filter/Chain.php b/src/Filter/Chain.php index c0060e6..de153f3 100644 --- a/src/Filter/Chain.php +++ b/src/Filter/Chain.php @@ -14,7 +14,7 @@ abstract class Chain implements Rule, MetaDataProvider, IteratorAggregate, Count use MetaData; /** @var array */ - protected $rules = []; + protected array $rules = []; /** * Create a new Chain @@ -59,7 +59,7 @@ public function getIterator(): Traversable * * @return $this */ - public function add(Rule $rule) + public function add(Rule $rule): static { $this->rules[] = $rule; @@ -75,7 +75,7 @@ public function add(Rule $rule) * @throws OutOfBoundsException In case no existing rule is found * @return $this */ - public function insertBefore(Rule $rule, Rule $before) + public function insertBefore(Rule $rule, Rule $before): static { $ruleAt = array_search($before, $this->rules, true); if ($ruleAt === false) { @@ -96,7 +96,7 @@ public function insertBefore(Rule $rule, Rule $before) * @throws OutOfBoundsException In case no existing rule is found * @return $this */ - public function insertAfter(Rule $rule, Rule $after) + public function insertAfter(Rule $rule, Rule $after): static { $ruleAt = array_search($after, $this->rules, true); if ($ruleAt === false) { @@ -115,7 +115,7 @@ public function insertAfter(Rule $rule, Rule $after) * * @return bool */ - public function has(Rule $rule) + public function has(Rule $rule): bool { return array_search($rule, $this->rules, true) !== false; } @@ -129,7 +129,7 @@ public function has(Rule $rule) * @throws OutOfBoundsException In case no existing rule is found * @return $this */ - public function replace(Rule $rule, Rule $replacement) + public function replace(Rule $rule, Rule $replacement): static { $ruleAt = array_search($rule, $this->rules, true); if ($ruleAt === false) { @@ -148,7 +148,7 @@ public function replace(Rule $rule, Rule $replacement) * * @return $this */ - public function remove(Rule $rule) + public function remove(Rule $rule): static { $ruleAt = array_search($rule, $this->rules, true); if ($ruleAt !== false) { @@ -163,7 +163,7 @@ public function remove(Rule $rule) * * @return bool */ - public function isEmpty() + public function isEmpty(): bool { return empty($this->rules); } diff --git a/src/Filter/Condition.php b/src/Filter/Condition.php index cc35610..dfb160a 100644 --- a/src/Filter/Condition.php +++ b/src/Filter/Condition.php @@ -6,19 +6,19 @@ abstract class Condition implements Rule, MetaDataProvider { use MetaData; - /** @var string */ - protected $column; + /** @var string|string[] */ + protected string|array $column = []; /** @var mixed */ - protected $value; + protected mixed $value = null; /** * Create a new Condition * - * @param string $column + * @param string|string[] $column * @param mixed $value */ - public function __construct($column, $value) + public function __construct(string|array $column, mixed $value) { $this->setColumn($column) ->setValue($value); @@ -37,11 +37,11 @@ public function __clone() /** * Set this condition's column * - * @param string $column + * @param string|string[] $column * * @return $this */ - public function setColumn($column) + public function setColumn(string|array $column): static { $this->column = $column; @@ -51,9 +51,9 @@ public function setColumn($column) /** * Get this condition's column * - * @return string + * @return string|string[] */ - public function getColumn() + public function getColumn(): string|array { return $this->column; } @@ -65,7 +65,7 @@ public function getColumn() * * @return $this */ - public function setValue($value) + public function setValue(mixed $value): static { $this->value = $value; @@ -77,7 +77,7 @@ public function setValue($value) * * @return mixed */ - public function getValue() + public function getValue(): mixed { return $this->value; } diff --git a/src/Filter/Equal.php b/src/Filter/Equal.php index 71da490..f5f22de 100644 --- a/src/Filter/Equal.php +++ b/src/Filter/Equal.php @@ -5,14 +5,14 @@ class Equal extends Condition { /** @var bool */ - protected $ignoreCase = false; + protected bool $ignoreCase = false; /** * Ignore case on both sides of the equation * * @return $this */ - public function ignoreCase() + public function ignoreCase(): static { $this->ignoreCase = true; @@ -24,7 +24,7 @@ public function ignoreCase() * * @return bool */ - public function ignoresCase() + public function ignoresCase(): bool { return $this->ignoreCase; } diff --git a/src/Filter/Like.php b/src/Filter/Like.php index 7a06279..d720c71 100644 --- a/src/Filter/Like.php +++ b/src/Filter/Like.php @@ -5,14 +5,14 @@ class Like extends Condition { /** @var bool */ - protected $ignoreCase = false; + protected bool $ignoreCase = false; /** * Ignore case on both sides of the equation * * @return $this */ - public function ignoreCase() + public function ignoreCase(): static { $this->ignoreCase = true; @@ -24,7 +24,7 @@ public function ignoreCase() * * @return bool */ - public function ignoresCase() + public function ignoresCase(): bool { return $this->ignoreCase; } diff --git a/src/Filter/MetaData.php b/src/Filter/MetaData.php index 6fe2523..700b2ff 100644 --- a/src/Filter/MetaData.php +++ b/src/Filter/MetaData.php @@ -6,10 +6,10 @@ trait MetaData { - /** @var Data */ - protected $metaData; + /** @var ?Data */ + protected ?Data $metaData = null; - public function metaData() + public function metaData(): Data { if ($this->metaData === null) { $this->metaData = new Data(); diff --git a/src/Filter/MetaDataProvider.php b/src/Filter/MetaDataProvider.php index ef9557e..c4f0ab9 100644 --- a/src/Filter/MetaDataProvider.php +++ b/src/Filter/MetaDataProvider.php @@ -11,5 +11,5 @@ interface MetaDataProvider * * @return Data */ - public function metaData(); + public function metaData(): Data; } diff --git a/src/Filter/Unequal.php b/src/Filter/Unequal.php index 5e37cbd..7742dfe 100644 --- a/src/Filter/Unequal.php +++ b/src/Filter/Unequal.php @@ -5,14 +5,14 @@ class Unequal extends Condition { /** @var bool */ - protected $ignoreCase = false; + protected bool $ignoreCase = false; /** * Ignore case on both sides of the equation * * @return $this */ - public function ignoreCase() + public function ignoreCase(): static { $this->ignoreCase = true; @@ -24,7 +24,7 @@ public function ignoreCase() * * @return bool */ - public function ignoresCase() + public function ignoresCase(): bool { return $this->ignoreCase; } diff --git a/src/Filter/Unlike.php b/src/Filter/Unlike.php index 16b9fb3..df8936d 100644 --- a/src/Filter/Unlike.php +++ b/src/Filter/Unlike.php @@ -5,14 +5,14 @@ class Unlike extends Condition { /** @var bool */ - protected $ignoreCase = false; + protected bool $ignoreCase = false; /** * Ignore case on both sides of the equation * * @return $this */ - public function ignoreCase() + public function ignoreCase(): static { $this->ignoreCase = true; @@ -24,7 +24,7 @@ public function ignoreCase() * * @return bool */ - public function ignoresCase() + public function ignoresCase(): bool { return $this->ignoreCase; } diff --git a/src/Filters.php b/src/Filters.php index defff43..c4b03ad 100644 --- a/src/Filters.php +++ b/src/Filters.php @@ -4,15 +4,15 @@ trait Filters { - /** @var Filter\Chain */ - protected $filter; + /** @var ?Filter\Chain */ + protected ?Filter\Chain $filter = null; - public function getFilter() + public function getFilter(): Filter\Chain { return $this->filter ?: Filter::all(); } - public function filter(Filter\Rule $filter) + public function filter(Filter\Rule $filter): static { $currentFilter = $this->getFilter(); if ($currentFilter instanceof Filter\All) { @@ -27,7 +27,7 @@ public function filter(Filter\Rule $filter) return $this; } - public function orFilter(Filter\Rule $filter) + public function orFilter(Filter\Rule $filter): static { $currentFilter = $this->getFilter(); if ($currentFilter instanceof Filter\Any) { @@ -42,14 +42,14 @@ public function orFilter(Filter\Rule $filter) return $this; } - public function notFilter(Filter\Rule $filter) + public function notFilter(Filter\Rule $filter): static { $this->filter(Filter::none($filter)); return $this; } - public function orNotFilter(Filter\Rule $filter) + public function orNotFilter(Filter\Rule $filter): static { $this->orFilter(Filter::none($filter)); diff --git a/src/Loader/AutoloadingPluginLoader.php b/src/Loader/AutoloadingPluginLoader.php index ba195c6..a9de176 100644 --- a/src/Loader/AutoloadingPluginLoader.php +++ b/src/Loader/AutoloadingPluginLoader.php @@ -10,10 +10,10 @@ class AutoloadingPluginLoader implements PluginLoader { /** @var string Namespace of the plugins */ - protected $namespace; + protected string $namespace; /** @var string Class name postfix */ - protected $postfix; + protected string $postfix; /** * Create a new autoloading plugin loader @@ -21,7 +21,7 @@ class AutoloadingPluginLoader implements PluginLoader * @param string $namespace Namespace of the plugins * @param string $postfix Class name postfix */ - public function __construct($namespace, $postfix = '') + public function __construct(string $namespace, string $postfix = '') { $this->namespace = $namespace; $this->postfix = $postfix; @@ -34,12 +34,12 @@ public function __construct($namespace, $postfix = '') * * @return string */ - protected function getFqn($name) + protected function getFqn(string $name): string { return $this->namespace . '\\' . ucfirst($name) . $this->postfix; } - public function load($name) + public function load(string $name): false|string { $class = $this->getFqn($name); diff --git a/src/Plugins.php b/src/Plugins.php index a5dbb77..5e9c12b 100644 --- a/src/Plugins.php +++ b/src/Plugins.php @@ -23,7 +23,7 @@ public static function wantPluginLoader($loaderOrNamespace, $postfix = '') if ($loaderOrNamespace instanceof PluginLoader) { $loader = $loaderOrNamespace; } else { - $loader = new AutoloadingPluginLoader($loaderOrNamespace, $postfix); + $loader = new AutoloadingPluginLoader($loaderOrNamespace, $postfix ?? ''); } return $loader; diff --git a/src/PriorityQueue.php b/src/PriorityQueue.php index 1992ec5..68856d4 100644 --- a/src/PriorityQueue.php +++ b/src/PriorityQueue.php @@ -15,7 +15,7 @@ class PriorityQueue extends SplPriorityQueue { /** @var int */ - protected $serial = PHP_INT_MAX; + protected int $serial = PHP_INT_MAX; /** * Inserts an element in the queue by sifting it up. @@ -25,9 +25,9 @@ class PriorityQueue extends SplPriorityQueue * @param TValue $value * @param TPriority $priority * - * @return bool + * @return true */ - public function insert($value, $priority): bool + public function insert($value, $priority): true { return parent::insert($value, [$priority, $this->serial--]); } @@ -37,7 +37,7 @@ public function insert($value, $priority): bool * * @return Generator */ - public function yieldAll() + public function yieldAll(): Generator { // Clone queue because the SplPriorityQueue acts as a heap and thus items are removed upon iteration $queue = clone $this; diff --git a/src/Seq.php b/src/Seq.php index dbae277..14cb755 100644 --- a/src/Seq.php +++ b/src/Seq.php @@ -12,13 +12,13 @@ class Seq /** * Check if the traversable contains the given needle * - * @param array|iterable $traversable + * @param iterable $traversable * @param mixed $needle Might also be a closure * @param bool $caseSensitive Whether strings should be compared case-sensitive * * @return bool */ - public static function contains($traversable, $needle, $caseSensitive = true) + public static function contains(iterable $traversable, mixed $needle, bool $caseSensitive = true): bool { return self::find($traversable, $needle, $caseSensitive)[0] !== null; } @@ -26,14 +26,14 @@ public static function contains($traversable, $needle, $caseSensitive = true) /** * Search in the traversable for the given needle and return its key and value * - * @param array|iterable $traversable + * @param iterable $traversable * @param mixed $needle Might also be a closure * @param bool $caseSensitive Whether strings should be compared case-sensitive * * @return array An array with two entries, the first is the key, then the value. * Both are null if nothing is found. */ - public static function find($traversable, $needle, $caseSensitive = true) + public static function find(iterable $traversable, mixed $needle, bool $caseSensitive = true): array { $usesCallback = $needle instanceof Closure; if (! $usesCallback && $caseSensitive && is_array($traversable)) { @@ -66,13 +66,13 @@ public static function find($traversable, $needle, $caseSensitive = true) /** * Search in the traversable for the given needle and return its key * - * @param array|iterable $traversable + * @param iterable $traversable * @param mixed $needle Might also be a closure * @param bool $caseSensitive Whether strings should be compared case-sensitive * * @return mixed|null Null if nothing is found */ - public static function findKey($traversable, $needle, $caseSensitive = true) + public static function findKey(iterable $traversable, mixed $needle, bool $caseSensitive = true): mixed { return self::find($traversable, $needle, $caseSensitive)[0]; } @@ -80,13 +80,13 @@ public static function findKey($traversable, $needle, $caseSensitive = true) /** * Search in the traversable for the given needle and return its value * - * @param array|iterable $traversable + * @param iterable $traversable * @param mixed $needle Might also be a closure * @param bool $caseSensitive Whether strings should be compared case-sensitive * * @return mixed|null Null if nothing is found */ - public static function findValue($traversable, $needle, $caseSensitive = true) + public static function findValue(iterable $traversable, mixed $needle, bool $caseSensitive = true): mixed { $usesCallback = $needle instanceof Closure; if (! $usesCallback && $caseSensitive && is_array($traversable)) { diff --git a/src/Str.php b/src/Str.php index b1cd19c..338ca4a 100644 --- a/src/Str.php +++ b/src/Str.php @@ -16,7 +16,7 @@ class Str * * @return string */ - public static function camel(?string $subject) + public static function camel(?string $subject): string { if ($subject === null) { return ''; @@ -36,7 +36,7 @@ public static function camel(?string $subject) * * @return bool */ - public static function startsWith(?string $subject, string $start, bool $caseSensitive = true) + public static function startsWith(?string $subject, string $start, bool $caseSensitive = true): bool { $subject = $subject ?? ''; if (! $caseSensitive) { @@ -58,7 +58,7 @@ public static function startsWith(?string $subject, string $start, bool $caseSen * * @return array */ - public static function symmetricSplit(?string $subject, string $delimiter, int $limit, ?string $default = null) + public static function symmetricSplit(?string $subject, string $delimiter, int $limit, ?string $default = null): array { if ($subject === null || empty($delimiter)) { return array_pad([], $limit, $default); @@ -76,7 +76,7 @@ public static function symmetricSplit(?string $subject, string $delimiter, int $ * * @return array */ - public static function trimSplit(?string $subject, string $delimiter = ',', int $limit = null) + public static function trimSplit(?string $subject, string $delimiter = ',', ?int $limit = null): array { if ($subject === null || empty($delimiter)) { return []; diff --git a/src/functions.php b/src/functions.php index e7f9be0..b455580 100644 --- a/src/functions.php +++ b/src/functions.php @@ -17,7 +17,7 @@ * * @return string */ -function get_php_type($subject) +function get_php_type(mixed $subject): string { if (is_object($subject)) { return get_class($subject); @@ -35,7 +35,7 @@ function get_php_type($subject) * * @throws InvalidArgumentException If subject type is invalid */ -function arrayval($subject) +function arrayval($subject): array { if (is_array($subject)) { return $subject; @@ -63,7 +63,7 @@ function arrayval($subject) * * @return mixed The first key of the iterable if it is not empty, null otherwise */ -function iterable_key_first($iterable) +function iterable_key_first(iterable $iterable): mixed { foreach ($iterable as $key => $_) { return $key; @@ -79,7 +79,7 @@ function iterable_key_first($iterable) * * @return ?mixed */ -function iterable_value_first($iterable) +function iterable_value_first(iterable $iterable): mixed { foreach ($iterable as $_ => $value) { return $value; diff --git a/tests/Loader/TestPluginLoader.php b/tests/Loader/TestPluginLoader.php index 1ec8f61..8ce4651 100644 --- a/tests/Loader/TestPluginLoader.php +++ b/tests/Loader/TestPluginLoader.php @@ -16,7 +16,7 @@ public function __construct($canLoad = true) $this->canLoad = $canLoad; } - public function load($name) + public function load(string $name): false|string { if (! $this->canLoad) { return false;