Skip to content

Commit df36819

Browse files
xHeavenstancl
andauthored
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 <[email protected]>
1 parent 6a03998 commit df36819

20 files changed

+52
-38
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ composer.lock
44
vendor/
55
.php-cs-fixer.cache
66
.vscode/
7+
.idea/
8+
.phpunit.cache/
79
coverage/
810
node_modules

phpstan.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ parameters:
2525
# paths:
2626
# - tests/*
2727
# - '#should return \$this#'
28-
29-
checkMissingIterableValueType: false
28+
- identifier: missingType.iterableValue

src/Comparable.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
namespace ArchTech\Enums;
66

7-
use Exception;
8-
use Iterator;
9-
use IteratorAggregate;
10-
117
trait Comparable
128
{
139
public function is(mixed $enum): bool
@@ -20,30 +16,18 @@ public function isNot(mixed $enum): bool
2016
return ! $this->is($enum);
2117
}
2218

23-
public function in(array|object $enums): bool
19+
public function in(iterable $enums): bool
2420
{
25-
$iterator = $enums;
26-
27-
if (! is_array($enums)) {
28-
if ($enums instanceof Iterator) {
29-
$iterator = $enums;
30-
} elseif ($enums instanceof IteratorAggregate) {
31-
$iterator = $enums->getIterator();
32-
} else {
33-
throw new Exception('in() expects an iterable value');
34-
}
35-
}
36-
37-
foreach ($iterator as $item) {
38-
if ($item === $this) {
21+
foreach ($enums as $item) {
22+
if ($this->is($item)) {
3923
return true;
4024
}
4125
}
4226

4327
return false;
4428
}
4529

46-
public function notIn(array|object $enums): bool
30+
public function notIn(iterable $enums): bool
4731
{
4832
return ! $this->in($enums);
4933
}

src/From.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait From
1515
*
1616
* @throws ValueError
1717
*/
18-
public static function from(string $case): static
18+
public static function from(int|string $case): static
1919
{
2020
return static::fromName($case);
2121
}
@@ -25,7 +25,7 @@ public static function from(string $case): static
2525
*
2626
* This will not override the `tryFrom()` method on BackedEnums
2727
*/
28-
public static function tryFrom(string $case): ?static
28+
public static function tryFrom(int|string $case): ?static
2929
{
3030
return static::tryFromName($case);
3131
}
@@ -37,7 +37,7 @@ public static function tryFrom(string $case): ?static
3737
*/
3838
public static function fromName(string $case): static
3939
{
40-
return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class . '');
40+
return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class);
4141
}
4242

4343
/**

src/InvokableCases.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __invoke()
1515
}
1616

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

src/Meta/Reflection.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public static function metaProperties(mixed $enum): array
2525
// Traits except the `Metadata` trait
2626
$traits = array_values(array_filter($reflection->getTraits(), fn (ReflectionClass $class) => $class->getName() !== 'ArchTech\Enums\Metadata'));
2727

28-
foreach ($traits as $trait) {
29-
$metaProperties = array_merge($metaProperties, static::parseMetaProperties($trait));
30-
}
28+
$traitsMeta = array_map(
29+
fn (ReflectionClass $trait) => static::parseMetaProperties($trait),
30+
$traits
31+
);
3132

32-
return $metaProperties;
33+
return array_merge($metaProperties, ...$traitsMeta);
3334
}
3435

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

76-
return $metaProperty::defaultValue() ?? null;
78+
return $metaProperty::defaultValue();
7779
}
7880
}

src/Metadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function fromMeta(MetaProperty $metaProperty): static
3030
);
3131
}
3232

33-
public function __call(string $property, $arguments): mixed
33+
public function __call(string $property, array $arguments): mixed
3434
{
3535
$metaProperties = Meta\Reflection::metaProperties($this);
3636

src/Options.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ public static function stringOptions(?Closure $callback = null, string $glue = '
2929

3030
if ($firstCase === null) {
3131
return '';
32-
} elseif ($firstCase instanceof BackedEnum) {
33-
// [name => value]
34-
$options = static::options();
35-
} else {
36-
// [name, name]
37-
$options = static::options();
32+
}
3833

34+
// [name, name]
35+
$options = static::options();
36+
if (! $firstCase instanceof BackedEnum) {
3937
// [name => name, name => name]
4038
$options = array_combine($options, $options);
4139
}

tests/PHPStan/InvokableCases/InvokableCasesTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
46

57
use PHPStan\Analyser\OutOfClassScope;

tests/PHPStan/InvokableCasesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
46
use PHPStan\Type\IntegerType;
57
use PHPStan\Type\StringType;

0 commit comments

Comments
 (0)