Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -25,7 +25,7 @@ public static function from(string $case): static
*
* 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);
}
Expand All @@ -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
2 changes: 2 additions & 0 deletions tests/PHPStan/InvokableCases/InvokableCasesTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;

use PHPStan\Analyser\OutOfClassScope;
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/InvokableCasesTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/Pest/ComparableTest.php
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -23,6 +25,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
2 changes: 2 additions & 0 deletions tests/Pest/FromTest.php
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/InvokableCasesTest.php
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/NamesTest.php
Original file line number Diff line number Diff line change
@@ -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']);
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/TraitTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use ArchTech\Enums\Meta\Meta;
use ArchTech\Enums\Meta\MetaProperty;
use ArchTech\Enums\Metadata;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/ValuesTest.php
Original file line number Diff line number Diff line change
@@ -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]);
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ArchTech\Enums\Tests;

use Orchestra\Testbench\TestCase as TestbenchTestCase;
Expand Down
Loading