diff --git a/README.md b/README.md index 98d7ee3..1956ca8 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Concerns/Comparison.php b/src/Concerns/Comparison.php index 071d9ba..e793d35 100644 --- a/src/Concerns/Comparison.php +++ b/src/Concerns/Comparison.php @@ -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); diff --git a/tests/Fixtures/EnhancedUnitEnum.php b/tests/Fixtures/EnhancedUnitEnum.php index cafc3f5..7819519 100644 --- a/tests/Fixtures/EnhancedUnitEnum.php +++ b/tests/Fixtures/EnhancedUnitEnum.php @@ -7,6 +7,7 @@ /** * @method static self anotherMappedEnum() + * * @method static self ENUM() * @method isAnother_Enum() * @method isEnum() * @method isNotEnum() @@ -18,4 +19,9 @@ enum EnhancedUnitEnum case ENUM; case ANOTHER_ENUM; case THIRD_ENUM; + + public function isEnumFunction(): bool + { + return $this->equals(self::ENUM()); + } } diff --git a/tests/Unit/Concerns/ComparisonTest.php b/tests/Unit/Concerns/ComparisonTest.php index 499c251..4e82c80 100644 --- a/tests/Unit/Concerns/ComparisonTest.php +++ b/tests/Unit/Concerns/ComparisonTest.php @@ -180,4 +180,8 @@ public function testShouldThrowExceptionWhenMethodNotExistsMagicFunction() { $this->expectException(BadMethodCallException::class); EnhancedUnitEnum::ENUM->doesNotExist(); } + + public function testShouldWorkWithoutIssuesCallingSelf() { + $this->assertTrue(EnhancedUnitEnum::ENUM->isEnumFunction()); + } }