Skip to content

Commit

Permalink
allow self reference when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 10, 2022
1 parent e00102e commit caab592
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
[![Total Downloads](https://img.shields.io/packagist/dt/henzeb/enumhancer.svg?style=flat-square)](https://packagist.org/packages/henzeb/enumhancer)

This package is your Swiss Army knife when it comes to PHP 8.1's native enums.
In this package you will find a lot of tools for the most common use cases
In this package you will find a lot of tools for the most common use cases,
and more will be added in the future.

If you have an idea, or you miss something that needs to be added,
just let me know.

This package currently supports the following:

Note: While functionality that also exists in Spatie's PHP Enum is made backwards
compatible to allow for an easy migration to PHP native enums, currently this is
not the case for the PHPUnit assertions or Faker Provider.

While functionality that also exists in Spatie's PHP Enum is made backwards
compatible to allow for an easy migration to PHP native enums, currently this is
not the case for the PHPUnit assertions or Faker Provider.

## Installation

You can install the package via composer:
Expand Down
15 changes: 10 additions & 5 deletions src/Concerns/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ final public function equals(self|string|int ...$equals): bool
return EnumCompare::equals($this, ...$equals);
}

final public function __call(string $name, array $arguments): bool
final public function __call(string $name, array $arguments): self|bool
{
if ((! str_starts_with($name, 'is') && ! str_starts_with($name, 'isNot')) || count($arguments)) {
$nameIsEnum = !EnumMakers::tryMake(self::class, $name);
if (((!str_starts_with($name, 'is') && !str_starts_with($name, 'isNot')) || count($arguments)) && $nameIsEnum) {
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s(...)', $this::class, $name));
}

$value = substr($name, str_starts_with($name, 'isNot')?5:2);
if (!$nameIsEnum) {
return self::__callStatic($name, []);
}

$value = substr($name, str_starts_with($name, 'isNot') ? 5 : 2);

if(!EnumMakers::tryMake(self::class, $value)) {
if (!EnumMakers::tryMake(self::class, $value)) {
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s(...)', $this::class, $name));
}
if(str_starts_with($name, 'isNot')) {
if (str_starts_with($name, 'isNot')) {
return !$this->equals($value);
}
return $this->equals($value);
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixtures/EnhancedUnitEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @method static self anotherMappedEnum()
* * @method static self ENUM()
* @method isAnother_Enum()
* @method isEnum()
* @method isNotEnum()
Expand All @@ -18,4 +19,9 @@ enum EnhancedUnitEnum
case ENUM;
case ANOTHER_ENUM;
case THIRD_ENUM;

public function isEnumFunction(): bool
{
return $this->equals(self::ENUM());
}
}
4 changes: 4 additions & 0 deletions tests/Unit/Concerns/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,8 @@ public function testShouldThrowExceptionWhenMethodNotExistsMagicFunction() {
$this->expectException(BadMethodCallException::class);
EnhancedUnitEnum::ENUM->doesNotExist();
}

public function testShouldWorkWithoutIssuesCallingSelf() {
$this->assertTrue(EnhancedUnitEnum::ENUM->isEnumFunction());
}
}

0 comments on commit caab592

Please sign in to comment.