From 72ad9b4b5b1d4e98a766baf776713e3ccc05ac2c Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 15:15:46 +0100 Subject: [PATCH 1/9] Use array destructuring instead of `list()` --- src/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions.php b/src/functions.php index e7f9be0..d3bfede 100644 --- a/src/functions.php +++ b/src/functions.php @@ -108,12 +108,12 @@ function yield_groups(Traversable $traversable, callable $groupBy): Generator return; } - list($criterion, $v, $k) = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); + [$criterion, $v, $k] = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); $group = [$k ?? $iterator->key() => $v ?? $iterator->current()]; $iterator->next(); for (; $iterator->valid(); $iterator->next()) { - list($c, $v, $k) = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); + [$c, $v, $k] = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); if ($c !== $criterion) { yield $criterion => $group; From 48565d326a04f4af028584500a334b9d3e8c167d Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 21:55:40 +0100 Subject: [PATCH 2/9] Add non-breaking strict type declarations Add strict type declarations to properties, function/method signatures, where types are unambiguous and no inheritance is affected. Remove any now superfluous type checks. PHPDoc adjusted: nullable shorthand, union type ordering, compact format. Raise minimum PHP version to 8.0. Co-authored-by: Eric Lippmann --- composer.json | 2 +- src/BaseFilter.php | 6 +- src/Contract/Filterable.php | 10 +-- src/Contract/PluginLoader.php | 2 +- src/Data.php | 14 ++-- src/Events.php | 8 +- src/Filter.php | 103 +++++++++---------------- src/Filter/Chain.php | 16 ++-- src/Filter/Condition.php | 22 +++--- src/Filter/Equal.php | 6 +- src/Filter/Like.php | 6 +- src/Filter/MetaData.php | 6 +- src/Filter/MetaDataProvider.php | 2 +- src/Filter/Unequal.php | 6 +- src/Filter/Unlike.php | 6 +- src/Filters.php | 14 ++-- src/Loader/AutoloadingPluginLoader.php | 12 +-- src/Messages.php | 10 +-- src/Plugins.php | 23 +++--- src/PriorityQueue.php | 6 +- src/Properties.php | 27 ++++--- src/Seq.php | 16 ++-- src/Str.php | 14 ++-- src/functions.php | 25 ++---- tests/FunctionsTest.php | 4 +- tests/Loader/TestPluginLoader.php | 2 +- 26 files changed, 169 insertions(+), 199 deletions(-) diff --git a/composer.json b/composer.json index cc93968..1a46fa1 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": ">=7.2", + "php": ">=8.0", "ext-openssl": "*", "evenement/evenement": "^3.0.1" } diff --git a/src/BaseFilter.php b/src/BaseFilter.php index e24f514..4e7862f 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; } 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/PluginLoader.php b/src/Contract/PluginLoader.php index 1be779c..d081a47 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): string|false; } 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/Events.php b/src/Events.php index 3405086..3ecae41 100644 --- a/src/Events.php +++ b/src/Events.php @@ -12,13 +12,13 @@ trait Events } /** @var array */ - protected $eventsEmittedOnce = []; + protected array $eventsEmittedOnce = []; /** * @param string $event * @param array $arguments */ - protected function emitOnce($event, array $arguments = []) + protected function emitOnce(string $event, array $arguments = []): void { if (! isset($this->eventsEmittedOnce[$event])) { $this->eventsEmittedOnce[$event] = true; @@ -31,7 +31,7 @@ protected function emitOnce($event, array $arguments = []) * @param callable $listener * @return $this */ - public function on($event, callable $listener) + public function on($event, callable $listener): static { $this->assertValidEvent($event); $this->evenementUnvalidatedOn($event, $listener); @@ -39,7 +39,7 @@ public function on($event, callable $listener) return $this; } - protected function assertValidEvent($event) + protected function assertValidEvent(string $event): void { if (! $this->isValidEvent($event)) { throw new InvalidArgumentException("$event is not a valid event"); diff --git a/src/Filter.php b/src/Filter.php index c5f7902..ee15638 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -2,7 +2,6 @@ namespace ipl\Stdlib; -use InvalidArgumentException; use ipl\Stdlib\Filter\All; use ipl\Stdlib\Filter\Any; use ipl\Stdlib\Filter\Chain; @@ -17,7 +16,6 @@ use ipl\Stdlib\Filter\Rule; use ipl\Stdlib\Filter\Unequal; use ipl\Stdlib\Filter\Unlike; -use Throwable; class Filter { @@ -32,21 +30,14 @@ protected function __construct() * Return whether the given rule matches the given item * * @param Rule $rule - * @param array|object $row + * @param object|array $row * * @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)) { - $row = (object) $row; - } else { - throw new InvalidArgumentException(sprintf( - 'Object or array expected, got %s instead', - get_php_type($row) - )); - } + $row = (object) $row; } return (new self())->performMatch($rule, $row); @@ -59,7 +50,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 +63,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 +81,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 +94,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 +112,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 +125,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)) { @@ -149,11 +140,11 @@ protected function matchNone(None $rules, $row) * Create a rule that matches rows with a column that **equals** the given value * * @param string $column - * @param array|bool|float|int|string $value + * @param mixed $value * * @return Condition */ - public static function equal($column, $value) + public static function equal(string $column, mixed $value): Condition { return new Equal($column, $value); } @@ -166,17 +157,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 +182,11 @@ protected function matchEqual($rule, $row) * Performs a wildcard search if the value contains asterisks. * * @param string $column - * @param string|string[] $value + * @param mixed $value * * @return Condition */ - public static function like($column, $value) + public static function like(string $column, mixed $value): Condition { return new Like($column, $value); } @@ -217,17 +199,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 +227,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 +253,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); @@ -319,11 +292,11 @@ protected function performSimilarityMatch($value, $rowValue, $ignoreCase = false * Create a rule that matches rows with a column that is **unequal** with the given value * * @param string $column - * @param array|bool|float|int|string $value + * @param mixed $value * * @return Condition */ - public static function unequal($column, $value) + public static function unequal(string $column, mixed $value): Condition { return new Unequal($column, $value); } @@ -336,7 +309,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 +320,11 @@ protected function matchUnequal(Unequal $rule, $row) * Performs a wildcard search if the value contains asterisks. * * @param string $column - * @param string|string[] $value + * @param mixed $value * * @return Condition */ - public static function unlike($column, $value) + public static function unlike(string $column, mixed $value): Condition { return new Unlike($column, $value); } @@ -364,7 +337,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); } @@ -373,11 +346,11 @@ protected function matchUnlike(Unlike $rule, $row) * Create a rule that matches rows with a column that is **greater** than the given value * * @param string $column - * @param float|int|string $value + * @param mixed $value * * @return Condition */ - public static function greaterThan($column, $value) + public static function greaterThan(string $column, mixed $value): Condition { return new GreaterThan($column, $value); } @@ -390,7 +363,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(); @@ -402,11 +375,11 @@ protected function matchGreaterThan(GreaterThan $rule, $row) * Create a rule that matches rows with a column that is **less** than the given value * * @param string $column - * @param float|int|string $value + * @param mixed $value * * @return Condition */ - public static function lessThan($column, $value) + public static function lessThan(string $column, mixed $value): Condition { return new LessThan($column, $value); } @@ -419,7 +392,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(); @@ -431,11 +404,11 @@ protected function matchLessThan(LessThan $rule, $row) * Create a rule that matches rows with a column that is **greater** than or **equal** to the given value * * @param string $column - * @param float|int|string $value + * @param mixed $value * * @return Condition */ - public static function greaterThanOrEqual($column, $value) + public static function greaterThanOrEqual(string $column, mixed $value): Condition { return new GreaterThanOrEqual($column, $value); } @@ -448,7 +421,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(); @@ -460,11 +433,11 @@ protected function matchGreaterThanOrEqual(GreaterThanOrEqual $rule, $row) * Create a rule that matches rows with a column that is **less** than or **equal** to the given value * * @param string $column - * @param float|int|string $value + * @param mixed $value * * @return Condition */ - public static function lessThanOrEqual($column, $value) + public static function lessThanOrEqual(string $column, mixed $value): Condition { return new LessThanOrEqual($column, $value); } @@ -477,7 +450,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,7 +466,7 @@ 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: @@ -534,7 +507,7 @@ protected function performMatch(Rule $rule, $row) * * @return mixed */ - protected function extractValue($column, $row) + protected function extractValue(string $column, object $row): mixed { return $row->$column ?? null; } @@ -550,7 +523,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..08e23bb 100644 --- a/src/Loader/AutoloadingPluginLoader.php +++ b/src/Loader/AutoloadingPluginLoader.php @@ -10,18 +10,18 @@ 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 * * @param string $namespace Namespace of the plugins - * @param string $postfix Class name postfix + * @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/Messages.php b/src/Messages.php index b601c1d..b4a41a4 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -5,14 +5,14 @@ trait Messages { /** @var array */ - protected $messages = []; + protected array $messages = []; /** * Get whether there are any messages * * @return bool */ - public function hasMessages() + public function hasMessages(): bool { return ! empty($this->messages); } @@ -34,7 +34,7 @@ public function getMessages() * * @return $this */ - public function setMessages(array $messages) + public function setMessages(array $messages): static { $this->clearMessages(); @@ -71,7 +71,7 @@ public function addMessage($message, ...$args) * * @return $this */ - public function addMessages(array $messages) + public function addMessages(array $messages): static { $this->messages = array_merge($this->messages, $messages); @@ -83,7 +83,7 @@ public function addMessages(array $messages) * * @return $this */ - public function clearMessages() + public function clearMessages(): static { $this->messages = []; diff --git a/src/Plugins.php b/src/Plugins.php index a5dbb77..add62ab 100644 --- a/src/Plugins.php +++ b/src/Plugins.php @@ -8,17 +8,17 @@ trait Plugins { /** @var array Registered plugin loaders by type */ - protected $pluginLoaders = []; + protected array $pluginLoaders = []; /** * Factory for plugin loaders * * @param PluginLoader|string $loaderOrNamespace - * @param string $postfix + * @param string $postfix * * @return PluginLoader */ - public static function wantPluginLoader($loaderOrNamespace, $postfix = '') + public static function wantPluginLoader(PluginLoader|string $loaderOrNamespace, string $postfix = ''): PluginLoader { if ($loaderOrNamespace instanceof PluginLoader) { $loader = $loaderOrNamespace; @@ -36,7 +36,7 @@ public static function wantPluginLoader($loaderOrNamespace, $postfix = '') * * @return bool */ - public function hasPluginLoader($type) + public function hasPluginLoader(string $type): bool { return isset($this->pluginLoaders[$type]); } @@ -44,13 +44,13 @@ public function hasPluginLoader($type) /** * Add a plugin loader for the given type * - * @param string $type + * @param string $type * @param PluginLoader|string $loaderOrNamespace - * @param string $postfix + * @param string $postfix * * @return $this */ - public function addPluginLoader($type, $loaderOrNamespace, $postfix = '') + public function addPluginLoader(string $type, PluginLoader|string $loaderOrNamespace, string $postfix = ''): static { $loader = static::wantPluginLoader($loaderOrNamespace, $postfix); @@ -71,7 +71,7 @@ public function addPluginLoader($type, $loaderOrNamespace, $postfix = '') * * @return string|false */ - public function loadPlugin($type, $name) + public function loadPlugin(string $type, string $name): string|false { if ($this->hasPluginLoader($type)) { /** @var PluginLoader $loader */ @@ -86,8 +86,11 @@ public function loadPlugin($type, $name) return false; } - protected function addDefaultPluginLoader($type, $loaderOrNamespace, $postfix) - { + protected function addDefaultPluginLoader( + string $type, + PluginLoader|string $loaderOrNamespace, + string $postfix + ): static { $this->pluginLoaders[$type][] = static::wantPluginLoader($loaderOrNamespace, $postfix); return $this; diff --git a/src/PriorityQueue.php b/src/PriorityQueue.php index 2c3f448..ef1b46f 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. @@ -27,7 +27,7 @@ class PriorityQueue extends SplPriorityQueue * * @return true */ - public function insert($value, $priority): true + public function insert(mixed $value, mixed $priority): bool { return parent::insert($value, [$priority, $this->serial--]); } @@ -37,7 +37,7 @@ public function insert($value, $priority): true * * @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/Properties.php b/src/Properties.php index 5726af3..cda179b 100644 --- a/src/Properties.php +++ b/src/Properties.php @@ -11,14 +11,14 @@ trait Properties { /** @var array */ - private $properties = []; + private array $properties = []; /** * Get whether this class has any properties * * @return bool */ - public function hasProperties() + public function hasProperties(): bool { return ! empty($this->properties); } @@ -30,7 +30,7 @@ public function hasProperties() * * @return bool */ - public function hasProperty($key) + public function hasProperty(string $key): bool { return array_key_exists($key, $this->properties); } @@ -42,7 +42,7 @@ public function hasProperty($key) * * @return $this */ - public function setProperties(array $properties) + public function setProperties(array $properties): static { foreach ($properties as $key => $value) { $this->setProperty($key, $value); @@ -77,7 +77,7 @@ protected function getProperty($key) * * @return $this */ - protected function setProperty($key, $value) + protected function setProperty(string $key, mixed $value): static { $this->properties[$key] = $value; @@ -103,7 +103,7 @@ public function getIterator(): Traversable * * @return bool */ - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { return isset($this->properties[$offset]); } @@ -115,8 +115,7 @@ public function offsetExists($offset): bool * * @return mixed */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { return $this->getProperty($offset); } @@ -127,7 +126,7 @@ public function offsetGet($offset) * @param mixed $offset * @param mixed $value */ - public function offsetSet($offset, $value): void + public function offsetSet(mixed $offset, mixed $value): void { $this->setProperty($offset, $value); } @@ -137,7 +136,7 @@ public function offsetSet($offset, $value): void * * @param mixed $offset */ - public function offsetUnset($offset): void + public function offsetUnset(mixed $offset): void { unset($this->properties[$offset]); } @@ -153,7 +152,7 @@ public function offsetUnset($offset): void * * @return mixed */ - public function __get($key) + public function __get(string $key): mixed { return $this->getProperty($key); } @@ -168,7 +167,7 @@ public function __get($key) * @param string $key * @param mixed $value */ - public function __set($key, $value) + public function __set(string $key, mixed $value): void { $this->setProperty($key, $value); } @@ -184,7 +183,7 @@ public function __set($key, $value) * * @return bool */ - public function __isset($key) + public function __isset(string $key): bool { return $this->offsetExists($key); } @@ -198,7 +197,7 @@ public function __isset($key) * * @param string $key */ - public function __unset($key) + public function __unset(string $key): void { $this->offsetUnset($key); } 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 b48663a..8907529 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,8 +58,12 @@ 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 +80,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 d3bfede..33a02eb 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); @@ -29,13 +29,11 @@ function get_php_type($subject) /** * Get the array value of the given subject * - * @param array|object|Traversable $subject + * @param iterable|stdClass $subject * * @return array - * - * @throws InvalidArgumentException If subject type is invalid */ -function arrayval($subject) +function arrayval(iterable|stdClass $subject): array { if (is_array($subject)) { return $subject; @@ -45,15 +43,8 @@ function arrayval($subject) return (array) $subject; } - if ($subject instanceof Traversable) { - // Works for generators too - return iterator_to_array($subject); - } - - throw new InvalidArgumentException(sprintf( - 'arrayval expects arrays, objects or instances of Traversable. Got %s instead.', - get_php_type($subject) - )); + // Works for generators too + return iterator_to_array($subject); } /** @@ -61,9 +52,9 @@ function arrayval($subject) * * @param iterable $iterable * - * @return mixed The first key of the iterable if it is not empty, null otherwise + * @return int|string|null The first key of the iterable if it is not empty, null otherwise */ -function iterable_key_first($iterable) +function iterable_key_first(iterable $iterable): int|string|null { foreach ($iterable as $key => $_) { return $key; @@ -79,7 +70,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/FunctionsTest.php b/tests/FunctionsTest.php index e107e66..867df6b 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -4,9 +4,9 @@ use ArrayIterator; use EmptyIterator; -use InvalidArgumentException; use ipl\Stdlib; use stdClass; +use TypeError; class FunctionsTest extends TestCase { @@ -89,7 +89,7 @@ public function testArrayvalWithTraversable() public function testArrayvalException() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(TypeError::class); Stdlib\arrayval(null); } diff --git a/tests/Loader/TestPluginLoader.php b/tests/Loader/TestPluginLoader.php index 1ec8f61..2a5de38 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): string|false { if (! $this->canLoad) { return false; From a58761805ac18240ce715cf6908b30814b72f194 Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 21:56:27 +0100 Subject: [PATCH 3/9] `PriorityQueue::insert()`: Use standalone `true` return type Raise minimum PHP version to 8.2. Co-authored-by: Eric Lippmann --- composer.json | 2 +- src/PriorityQueue.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1a46fa1..2bb9df3 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": ">=8.0", + "php": ">=8.2", "ext-openssl": "*", "evenement/evenement": "^3.0.1" } diff --git a/src/PriorityQueue.php b/src/PriorityQueue.php index ef1b46f..1c10736 100644 --- a/src/PriorityQueue.php +++ b/src/PriorityQueue.php @@ -27,7 +27,7 @@ class PriorityQueue extends SplPriorityQueue * * @return true */ - public function insert(mixed $value, mixed $priority): bool + public function insert(mixed $value, mixed $priority): true { return parent::insert($value, [$priority, $this->serial--]); } From 2d95fc973f65b3b880ddfe37d4c11bf2e49f4034 Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 22:01:50 +0100 Subject: [PATCH 4/9] Replace return type `self` with `static` --- src/BaseFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseFilter.php b/src/BaseFilter.php index 4e7862f..0b0a946 100644 --- a/src/BaseFilter.php +++ b/src/BaseFilter.php @@ -36,7 +36,7 @@ public function getBaseFilter(): ?Rule * * @return $this */ - public function setBaseFilter(?Rule $baseFilter = null): self + public function setBaseFilter(?Rule $baseFilter = null): static { $this->baseFilter = $baseFilter; From 1ca34d8ee673b50810321b18d4c8cfbe5f2a4e3f Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 22:02:52 +0100 Subject: [PATCH 5/9] Replace `switch` statement with `match` expression --- src/Filter.php | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Filter.php b/src/Filter.php index ee15638..8450a98 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -468,35 +468,19 @@ protected function matchLessThanOrEqual(LessThanOrEqual $rule, object $row): boo */ 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( - 'Unable to match filter. Rule type %s is unknown', - get_class($rule) - )); - } + 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), + }; } /** From eb2dcf4e69a904446194eb8375d84e1de63f09ef Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 16:05:53 +0100 Subject: [PATCH 6/9] `Messages::addMessage()`: Add strict type declarations This change requires adjustments to the following consumers: ipl-html: - src/Contract/FormElement.php PHPDoc adjusted: nullable shorthand, union type ordering, compact format. --- src/Messages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Messages.php b/src/Messages.php index b4a41a4..a728c0f 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -49,11 +49,11 @@ public function setMessages(array $messages): static * Add a single message * * @param string $message - * @param mixed ...$args Optional args for sprintf-style messages + * @param mixed ...$args Optional args for sprintf-style messages * * @return $this */ - public function addMessage($message, ...$args) + public function addMessage(string $message, mixed ...$args): static { if (empty($args)) { $this->messages[] = $message; From b3d2fb35f0cb9fa022bead71e402b55b2ee4f210 Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 16:08:52 +0100 Subject: [PATCH 7/9] `Properties::getProperty()`: Add strict type declarations This change requires adjustments to the following consumers: ipl-orm: - src/Common/PropertiesWithDefaults.php --- src/Properties.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Properties.php b/src/Properties.php index cda179b..015f0d3 100644 --- a/src/Properties.php +++ b/src/Properties.php @@ -60,7 +60,7 @@ public function setProperties(array $properties): static * * @throws OutOfBoundsException If the property by the given key does not exist */ - protected function getProperty($key) + protected function getProperty(string $key): mixed { if (array_key_exists($key, $this->properties)) { return $this->properties[$key]; From a26ecd84981df70682114602e2b8d9ef68998f8b Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 15:17:10 +0100 Subject: [PATCH 8/9] `Paginatable`: Add strict type declarations This change requires adjustments to the following consumers: ipl-sql - src/Cursor.php - src/LimitOffset.php PHPDoc adjusted: nullable shorthand, union type ordering, compact format. --- src/Contract/Paginatable.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Contract/Paginatable.php b/src/Contract/Paginatable.php index 3e2a4ee..1a63005 100644 --- a/src/Contract/Paginatable.php +++ b/src/Contract/Paginatable.php @@ -11,46 +11,46 @@ interface Paginatable extends Countable * * @return bool */ - public function hasLimit(); + public function hasLimit(): bool; /** * Get the limit * - * @return int|null + * @return ?int */ - public function getLimit(); + public function getLimit(): ?int; /** * Set the limit * - * @param int|null $limit Maximum number of items to return. If you want to disable the limit, - * it is best practice to use null or a negative value + * @param ?int $limit Maximum number of items to return. If you want to disable the limit, + * it is best practice to use null or a negative value * * @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 + * @return ?int */ - public function getOffset(); + public function getOffset(): ?int; /** * Set the offset * - * @param int|null $offset Start result set after this many rows. If you want to disable the offset, - * it is best practice to use null or a negative value + * @param ?int $offset Start result set after this many rows. If you want to disable the offset, + * it is best practice to use null or a negative value * * @return $this */ - public function offset($offset); + public function offset(?int $offset): static; } From 903490d724b27e535589b4b5e9e376553e4bd4cc Mon Sep 17 00:00:00 2001 From: Jan Schuppik Date: Wed, 4 Mar 2026 15:17:29 +0100 Subject: [PATCH 9/9] `Translator`: Add strict type declarations This change requires adjustments to the following consumers: ipl-i18n - src/GettextTranslator.php - src/NoopTranslator.php PHPDoc adjusted: nullable shorthand, union type ordering, compact format. --- src/Contract/Translator.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Contract/Translator.php b/src/Contract/Translator.php index 85ab515..413d37e 100644 --- a/src/Contract/Translator.php +++ b/src/Contract/Translator.php @@ -11,11 +11,11 @@ interface Translator * Translate a message * * @param string $message - * @param string $context Message context + * @param ?string $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 @@ -24,11 +24,11 @@ public function translate($message, $context = null); * * @param string $domain * @param string $message - * @param string $context Message context + * @param ?string $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 @@ -37,13 +37,13 @@ public function translateInDomain($domain, $message, $context = null); * 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 $plural Plural message + * @param int $number Number to decide between the returned singular and plural forms + * @param ?string $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 @@ -55,11 +55,17 @@ public function translatePlural($singular, $plural, $number, $context = null); * * @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 $plural Plural message + * @param int $number Number to decide between the returned singular and plural forms + * @param ?string $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; }