Skip to content
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

Check for member instantiation #31

Merged
merged 1 commit into from
Jan 29, 2024
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
17 changes: 12 additions & 5 deletions src/Enum/EnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@
*/
final public static function getInstances(): array
{
static::resolveMembers();

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }
foreach(static::$members[static::class] as $member => $value) {

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);
static::fromMember($member);

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }
}

return array_values(static::$instances[static::class]);
Expand Down Expand Up @@ -189,15 +189,15 @@
}

/** @param list<string> $members */
final public static function oneOfMembersExists(array $members): bool
final public static function membersExist(array $members): bool
{
static::resolveMembers();

return [] !== array_intersect(array_keys(static::$members[static::class]), $members);
}

/** @param list<int|string> $values */
final public static function oneOfValuesExists(array $values): bool
final public static function valuesExist(array $values): bool
{
static::resolveMembers();

Expand All @@ -216,23 +216,30 @@
}

/** @param list<string> $members */
final public function hasOneOfMembers(array $members): bool
final public function hasMemberIn(array $members): bool
{
return in_array($this->member, $members, true);
}

/** @param list<int|string> $values */
final public function hasOneOfValues(array $values): bool
final public function hasValueIn(array $values): bool
{
return in_array($this->value, $values, true);
}

/** @param list<static> $enums */
final public function isOneOfInstances(array $enums): bool
final public function isIn(array $enums): bool
{
return in_array($this, $enums, true);
}

final public static function isMemberWarm(string $member): bool
{
return self::memberExists($member)
&& array_key_exists(static::class, static::$instances)
&& array_key_exists($member, static::$instances[static::class]);
}

/* --- INFO --- */

/** @return int|string */
Expand Down
68 changes: 41 additions & 27 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,26 @@ public function testHasValue(): void
$this->assertFalse($enum::SECOND()->hasValue(1));
}

public function testHasOneOfMembers(): void
public function testHasMemberIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->hasOneOfMembers(['FIRST', 'SECOND']));
$this->assertTrue($enum::SECOND()->hasOneOfMembers(['SECOND', 'THIRD']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers(['THIRD', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers(['FIRST', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->hasMemberIn(['FIRST', 'SECOND']));
$this->assertTrue($enum::SECOND()->hasMemberIn(['SECOND', 'THIRD']));
$this->assertFalse($enum::SECOND()->hasMemberIn(['THIRD', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasMemberIn(['FIRST', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasMemberIn([1.5, null, 'invalid']));
}

public function testHasOneOfValues(): void
public function testHasValueIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->hasOneOfValues([1, 2]));
$this->assertTrue($enum::SECOND()->hasOneOfValues([2, 3]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([1, 4]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([3, 4]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->hasValueIn([1, 2]));
$this->assertTrue($enum::SECOND()->hasValueIn([2, 3]));
$this->assertFalse($enum::SECOND()->hasValueIn([1, 4]));
$this->assertFalse($enum::SECOND()->hasValueIn([3, 4]));
$this->assertFalse($enum::SECOND()->hasValueIn([1.5, null, 'invalid']));
}

public function testMemberExists(): void
Expand All @@ -303,34 +303,48 @@ public function testValueExists(): void
$this->assertFalse($enum::valueExists(3));
}

public function testOneOfMembersExists(): void
public function testMembersExist(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::oneOfMembersExists(['FIRST', 'SECOND']));
$this->assertTrue($enum::oneOfMembersExists(['FIRST', 'THIRD']));
$this->assertFalse($enum::oneOfMembersExists(['THIRD', 'FOURTH']));
$this->assertFalse($enum::oneOfMembersExists([1.5, null, 'invalid']));
$this->assertTrue($enum::membersExist(['FIRST', 'SECOND']));
$this->assertTrue($enum::membersExist(['FIRST', 'THIRD']));
$this->assertFalse($enum::membersExist(['THIRD', 'FOURTH']));
$this->assertFalse($enum::membersExist([1.5, null, 'invalid']));
}

public function testOneOfValuesExists(): void
public function testValuesExist(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::oneOfValuesExists([1, 2]));
$this->assertTrue($enum::oneOfValuesExists([1, 3]));
$this->assertFalse($enum::oneOfValuesExists([3, 4]));
$this->assertFalse($enum::oneOfValuesExists([1.5, null, 'invalid']));
$this->assertTrue($enum::valuesExist([1, 2]));
$this->assertTrue($enum::valuesExist([1, 3]));
$this->assertFalse($enum::valuesExist([3, 4]));
$this->assertFalse($enum::valuesExist([1.5, null, 'invalid']));
}

public function testIsOneOfInstances(): void
public function testIsIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->isOneOfInstances([$enum::FIRST(), $enum::SECOND()]));
$this->assertTrue($enum::SECOND()->isOneOfInstances([$enum::SECOND(), new \stdClass()]));
$this->assertFalse($enum::SECOND()->isOneOfInstances([new \stdClass(), new \DateTimeImmutable()]));
$this->assertFalse($enum::SECOND()->isOneOfInstances([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->isIn([$enum::FIRST(), $enum::SECOND()]));
$this->assertTrue($enum::SECOND()->isIn([$enum::SECOND(), new \stdClass()]));
$this->assertFalse($enum::SECOND()->isIn([new \stdClass(), new \DateTimeImmutable()]));
$this->assertFalse($enum::SECOND()->isIn([1.5, null, 'invalid']));
}

public function testIsMemberWarm(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertFalse($enum::isMemberWarm('FIRST'));
$this->assertFalse($enum::isMemberWarm('SECOND'));
$enum::FIRST();
$this->assertTrue($enum::isMemberWarm('FIRST'));
$this->assertFalse($enum::isMemberWarm('SECOND'));
$enum::SECOND();
$this->assertTrue($enum::isMemberWarm('FIRST'));
$this->assertTrue($enum::isMemberWarm('SECOND'));
}

/* --- CONVERT --- */
Expand Down
Loading