1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 03:54:03 +00:00

General code cleanup (#28)

* Add .idea folder to .gitignore

* Remove useless concatenation

* Add phpunit cache directory to .gitignore

* Use iterable type instead of unnecessary checks

* Remove unnecessary null coalescing

If the return value is `null`, it will return `null` anyway. If the method doesn't exist, it will throw an `Error` because of the undefined method.

* Add typehint

* Merge meta properties in one go instead of in a loop

* Simplify control flow

* Use non-deprecated phpstan configuration value

* Add missing types

* Fix style with php-cs-fixer

* Add tests that satisfy the ArrayIterator branch

* Use is() for comparison in in()

* from(int|string), tryFrom(int|string)

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
Márk Magyar 2025-06-07 01:11:53 +02:00 committed by GitHub
parent 6a0399823f
commit df3681965c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 52 additions and 38 deletions

2
.gitignore vendored
View file

@ -4,5 +4,7 @@ composer.lock
vendor/
.php-cs-fixer.cache
.vscode/
.idea/
.phpunit.cache/
coverage/
node_modules

View file

@ -25,5 +25,4 @@ parameters:
# paths:
# - tests/*
# - '#should return \$this#'
checkMissingIterableValueType: false
- identifier: missingType.iterableValue

View file

@ -4,10 +4,6 @@ declare(strict_types=1);
namespace ArchTech\Enums;
use Exception;
use Iterator;
use IteratorAggregate;
trait Comparable
{
public function is(mixed $enum): bool
@ -20,22 +16,10 @@ trait Comparable
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;
}
}
@ -43,7 +27,7 @@ trait Comparable
return false;
}
public function notIn(array|object $enums): bool
public function notIn(iterable $enums): bool
{
return ! $this->in($enums);
}

View file

@ -15,7 +15,7 @@ trait From
*
* @throws ValueError
*/
public static function from(string $case): static
public static function from(int|string $case): static
{
return static::fromName($case);
}
@ -25,7 +25,7 @@ trait From
*
* This will not override the `tryFrom()` method on BackedEnums
*/
public static function tryFrom(string $case): ?static
public static function tryFrom(int|string $case): ?static
{
return static::tryFromName($case);
}
@ -37,7 +37,7 @@ trait From
*/
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);
}
/**

View file

@ -15,7 +15,7 @@ trait InvokableCases
}
/** 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();

View file

@ -25,11 +25,12 @@ class Reflection
// 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 */
@ -51,6 +52,7 @@ class Reflection
/**
* 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
@ -73,6 +75,6 @@ class Reflection
return $properties[0]->value;
}
return $metaProperty::defaultValue() ?? null;
return $metaProperty::defaultValue();
}
}

View file

@ -30,7 +30,7 @@ trait Metadata
);
}
public function __call(string $property, $arguments): mixed
public function __call(string $property, array $arguments): mixed
{
$metaProperties = Meta\Reflection::metaProperties($this);

View file

@ -29,13 +29,11 @@ trait Options
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);
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
use PHPStan\Analyser\OutOfClassScope;

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
use ArchTech\Enums\{Comparable, InvokableCases, Options, Names, Values, From, Metadata};
use ArchTech\Enums\Meta\Meta;
use ArchTech\Enums\Meta\MetaProperty;

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
test('the is method checks for equality', function () {
expect(Status::PENDING->is(Status::PENDING))->toBeTrue();
expect(Status::PENDING->is(Status::DONE))->toBeFalse();
@ -23,6 +25,11 @@ it('the in method checks for presence in an array', function () {
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();
});

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
it('does not override the default BackedEnum from method')
->expect(Status::from(0))
->toBe(Status::PENDING);

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
use ArchTech\Enums\Exceptions\UndefinedCaseError;
it('can be used as a static method with backed enums', function () {

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
test('pure enums can have metadata on cases', function () {
expect(Role::ADMIN->color())->toBe('indigo');
expect(Role::GUEST->color())->toBe('gray');

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
it('can return an array of case names from backed enums')
->expect(Status::names())
->toBe(['PENDING', 'DONE']);

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
it('can return an associative array of options from a backed enum')
->expect(Status::options())->toBe([
'PENDING' => 0,

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
use ArchTech\Enums\Meta\Meta;
use ArchTech\Enums\Meta\MetaProperty;
use ArchTech\Enums\Metadata;

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
it('can return an array of case values from a backed enum')
->expect(Status::values())
->toBe([0, 1]);

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace ArchTech\Enums\Tests;
use Orchestra\Testbench\TestCase as TestbenchTestCase;