Skip to content

Commit

Permalink
Merge pull request #433 from greg0ire/restore-types
Browse files Browse the repository at this point in the history
Add missing types to the codebase
  • Loading branch information
greg0ire authored Oct 16, 2024
2 parents ab5a3cc + 03d4e48 commit 471407e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
6 changes: 0 additions & 6 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,4 @@
<rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps">
<exclude-pattern>tests/ClosureExpressionVisitorTest.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
<exclude-pattern>src/AbstractLazyCollection.php</exclude-pattern>
<exclude-pattern>src/ArrayCollection.php</exclude-pattern>
<exclude-pattern>src/Collection.php</exclude-pattern>
</rule>
</ruleset>
6 changes: 3 additions & 3 deletions src/AbstractLazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
34 changes: 17 additions & 17 deletions src/Expr/ClosureExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)) {
Expand All @@ -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()),
};
}
Expand Down Expand Up @@ -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),
);
Expand All @@ -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),
);
Expand All @@ -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);
}
}

0 comments on commit 471407e

Please sign in to comment.