Skip to content

General code cleanup #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ composer.lock
vendor/
.php-cs-fixer.cache
.vscode/
.idea/
.phpunit.cache/
coverage/
node_modules
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ parameters:
# paths:
# - tests/*
# - '#should return \$this#'

checkMissingIterableValueType: false
- identifier: missingType.iterableValue
24 changes: 4 additions & 20 deletions src/Comparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace ArchTech\Enums;

use Exception;
use Iterator;
use IteratorAggregate;

trait Comparable
{
public function is(mixed $enum): bool
Expand All @@ -20,30 +16,18 @@ public function isNot(mixed $enum): bool
return ! $this->is($enum);
}

public function in(array|object $enums): bool
public function in(iterable $enums): bool
{
$iterator = $enums;

if (! is_array($enums)) {
if ($enums instanceof Iterator) {
$iterator = $enums;
} elseif ($enums instanceof IteratorAggregate) {
$iterator = $enums->getIterator();
} else {
throw new Exception('in() expects an iterable value');
}
}

foreach ($iterator as $item) {
if ($item === $this) {
foreach ($enums as $item) {
if ($this->is($item)) {
return true;
}
}

return false;
}

public function notIn(array|object $enums): bool
public function notIn(iterable $enums): bool
{
return ! $this->in($enums);
}
Expand Down
2 changes: 1 addition & 1 deletion src/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function tryFrom(string $case): ?static
*/
public static function fromName(string $case): static
{
return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class . '');
return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/InvokableCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __invoke()
}

/** Return the enum's value or name when it's called ::STATICALLY(). */
public static function __callStatic($name, $args)
public static function __callStatic(string $name, array $args)
{
$cases = static::cases();

Expand Down
12 changes: 7 additions & 5 deletions src/Meta/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public static function metaProperties(mixed $enum): array
// Traits except the `Metadata` trait
$traits = array_values(array_filter($reflection->getTraits(), fn (ReflectionClass $class) => $class->getName() !== 'ArchTech\Enums\Metadata'));

foreach ($traits as $trait) {
$metaProperties = array_merge($metaProperties, static::parseMetaProperties($trait));
}
$traitsMeta = array_map(
fn (ReflectionClass $trait) => static::parseMetaProperties($trait),
$traits
);

return $metaProperties;
return array_merge($metaProperties, ...$traitsMeta);
}

/** @param ReflectionClass<object> $reflection */
Expand All @@ -51,6 +52,7 @@ protected static function parseMetaProperties(ReflectionClass $reflection): arra
/**
* Get the value of a meta property on the provided enum.
*
* @param class-string<MetaProperty> $metaProperty
* @param \Enum $enum
*/
public static function metaValue(string $metaProperty, mixed $enum): mixed
Expand All @@ -73,6 +75,6 @@ public static function metaValue(string $metaProperty, mixed $enum): mixed
return $properties[0]->value;
}

return $metaProperty::defaultValue() ?? null;
return $metaProperty::defaultValue();
}
}
2 changes: 1 addition & 1 deletion src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function fromMeta(MetaProperty $metaProperty): static
);
}

public function __call(string $property, $arguments): mixed
public function __call(string $property, array $arguments): mixed
{
$metaProperties = Meta\Reflection::metaProperties($this);

Expand Down
10 changes: 4 additions & 6 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public static function stringOptions(?Closure $callback = null, string $glue = '

if ($firstCase === null) {
return '';
} elseif ($firstCase instanceof BackedEnum) {
// [name => value]
$options = static::options();
} else {
// [name, name]
$options = static::options();
}

// [name, name]
$options = static::options();
if (! $firstCase instanceof BackedEnum) {
// [name => name, name => name]
$options = array_combine($options, $options);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Pest/ComparableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
expect(Status::PENDING->in([Status::PENDING, Status::DONE]))->toBeTrue();
expect(Role::ADMIN->in([Role::ADMIN]))->toBeTrue();

$iterator = new ArrayIterator([Status::PENDING, Status::DONE]);
expect(Status::PENDING->in($iterator))->toBeTrue();
expect(Status::DONE->in($iterator))->toBeTrue();
expect(Status::PENDING->in(new ArrayIterator([Role::ADMIN, Role::GUEST])))->toBeFalse();

expect(Status::PENDING->in([Status::DONE]))->toBeFalse();
expect(Status::PENDING->in([Role::ADMIN, Role::GUEST]))->toBeFalse();
});
Expand Down
Loading