From 03d4e48465e7683f9e800bcf014cadbb3eb2cb06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 14 Oct 2024 23:06:06 +0200 Subject: [PATCH] Add missing types to the codebase --- phpcs.xml.dist | 6 ----- src/AbstractLazyCollection.php | 6 ++--- src/ArrayCollection.php | 8 +++---- src/Expr/ClosureExpressionVisitor.php | 34 +++++++++++++-------------- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index b9aa1c9c..cb81b501 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -37,10 +37,4 @@ tests/ClosureExpressionVisitorTest.php - - - src/AbstractLazyCollection.php - src/ArrayCollection.php - src/Collection.php - diff --git a/src/AbstractLazyCollection.php b/src/AbstractLazyCollection.php index c926b6d8..295f036e 100644 --- a/src/AbstractLazyCollection.php +++ b/src/AbstractLazyCollection.php @@ -112,7 +112,7 @@ public function getValues(): array /** * {@inheritDoc} */ - public function set($key, $value): void + public function set(string|int $key, mixed $value): void { $this->initialize(); $this->collection->set($key, $value); @@ -207,7 +207,7 @@ public function map(Closure $func): Collection /** * {@inheritDoc} */ - public function reduce(Closure $func, $initial = null): mixed + public function reduce(Closure $func, mixed $initial = null): mixed { $this->initialize(); @@ -229,7 +229,7 @@ public function partition(Closure $p): array * * @template TMaybeContained */ - public function indexOf($element): string|int|false + public function indexOf(mixed $element): string|int|false { $this->initialize(); diff --git a/src/ArrayCollection.php b/src/ArrayCollection.php index 3635cb1e..844a84b4 100644 --- a/src/ArrayCollection.php +++ b/src/ArrayCollection.php @@ -150,7 +150,7 @@ public function removeElement(mixed $element): bool * * @param TKey $offset */ - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { return $this->containsKey($offset); } @@ -219,7 +219,7 @@ public function exists(Closure $p): bool * * @template TMaybeContained */ - public function indexOf($element): int|string|false + public function indexOf(mixed $element): int|string|false { return array_search($element, $this->elements, true); } @@ -254,7 +254,7 @@ public function count(): int /** * {@inheritDoc} */ - public function set($key, $value): void + public function set(string|int $key, mixed $value): void { $this->elements[$key] = $value; } @@ -306,7 +306,7 @@ public function map(Closure $func): Collection /** * {@inheritDoc} */ - public function reduce(Closure $func, $initial = null): mixed + public function reduce(Closure $func, mixed $initial = null): mixed { return array_reduce($this->elements, $func, $initial); } diff --git a/src/Expr/ClosureExpressionVisitor.php b/src/Expr/ClosureExpressionVisitor.php index bb06a106..aa81effb 100644 --- a/src/Expr/ClosureExpressionVisitor.php +++ b/src/Expr/ClosureExpressionVisitor.php @@ -81,7 +81,7 @@ public static function getObjectFieldValue(object|array $object, string $field): } // camelcase field name to support different variable naming conventions - $ccField = preg_replace_callback('/_(.?)/', static fn ($matches) => strtoupper((string) $matches[1]), $field); + $ccField = preg_replace_callback('/_(.?)/', static fn (array $matches) => strtoupper((string) $matches[1]), $field); foreach ($accessors as $accessor) { $accessor .= $ccField; @@ -103,7 +103,7 @@ public static function sortByField(string $name, int $orientation = 1, Closure|n $next = static fn (): int => 0; } - return static function ($a, $b) use ($name, $next, $orientation): int { + return static function (mixed $a, mixed $b) use ($name, $next, $orientation): int { $aValue = ClosureExpressionVisitor::getObjectFieldValue($a, $name); $bValue = ClosureExpressionVisitor::getObjectFieldValue($b, $name); @@ -122,24 +122,24 @@ public function walkComparison(Comparison $comparison): Closure $value = $comparison->getValue()->getValue(); return match ($comparison->getOperator()) { - Comparison::EQ => static fn ($object): bool => self::getObjectFieldValue($object, $field) === $value, - Comparison::NEQ => static fn ($object): bool => self::getObjectFieldValue($object, $field) !== $value, - Comparison::LT => static fn ($object): bool => self::getObjectFieldValue($object, $field) < $value, - Comparison::LTE => static fn ($object): bool => self::getObjectFieldValue($object, $field) <= $value, - Comparison::GT => static fn ($object): bool => self::getObjectFieldValue($object, $field) > $value, - Comparison::GTE => static fn ($object): bool => self::getObjectFieldValue($object, $field) >= $value, - Comparison::IN => static function ($object) use ($field, $value): bool { + Comparison::EQ => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) === $value, + Comparison::NEQ => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) !== $value, + Comparison::LT => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) < $value, + Comparison::LTE => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) <= $value, + Comparison::GT => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) > $value, + Comparison::GTE => static fn (object|array $object): bool => self::getObjectFieldValue($object, $field) >= $value, + Comparison::IN => static function (object|array $object) use ($field, $value): bool { $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); return in_array($fieldValue, $value, is_scalar($fieldValue)); }, - Comparison::NIN => static function ($object) use ($field, $value): bool { + Comparison::NIN => static function (object|array $object) use ($field, $value): bool { $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); return ! in_array($fieldValue, $value, is_scalar($fieldValue)); }, - Comparison::CONTAINS => static fn ($object): bool => str_contains((string) self::getObjectFieldValue($object, $field), (string) $value), - Comparison::MEMBER_OF => static function ($object) use ($field, $value): bool { + Comparison::CONTAINS => static fn (object|array $object): bool => str_contains((string) self::getObjectFieldValue($object, $field), (string) $value), + Comparison::MEMBER_OF => static function (object|array $object) use ($field, $value): bool { $fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field); if (! is_array($fieldValues)) { @@ -148,8 +148,8 @@ public function walkComparison(Comparison $comparison): Closure return in_array($value, $fieldValues, true); }, - Comparison::STARTS_WITH => static fn ($object): bool => str_starts_with((string) self::getObjectFieldValue($object, $field), (string) $value), - Comparison::ENDS_WITH => static fn ($object): bool => str_ends_with((string) self::getObjectFieldValue($object, $field), (string) $value), + Comparison::STARTS_WITH => static fn (object|array $object): bool => str_starts_with((string) self::getObjectFieldValue($object, $field), (string) $value), + Comparison::ENDS_WITH => static fn (object|array $object): bool => str_ends_with((string) self::getObjectFieldValue($object, $field), (string) $value), default => throw new RuntimeException('Unknown comparison operator: ' . $comparison->getOperator()), }; } @@ -178,7 +178,7 @@ public function walkCompositeExpression(CompositeExpression $expr): Closure /** @param callable[] $expressions */ private function andExpressions(array $expressions): Closure { - return static fn ($object): bool => array_all( + return static fn (object $object): bool => array_all( $expressions, static fn (callable $expression): bool => (bool) $expression($object), ); @@ -187,7 +187,7 @@ private function andExpressions(array $expressions): Closure /** @param callable[] $expressions */ private function orExpressions(array $expressions): Closure { - return static fn ($object): bool => array_any( + return static fn (object $object): bool => array_any( $expressions, static fn (callable $expression): bool => (bool) $expression($object), ); @@ -196,6 +196,6 @@ private function orExpressions(array $expressions): Closure /** @param callable[] $expressions */ private function notExpression(array $expressions): Closure { - return static fn ($object) => ! $expressions[0]($object); + return static fn (object $object) => ! $expressions[0]($object); } }