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

PHP8 compatibility #5

Merged
merged 2 commits into from
Jan 22, 2021
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ final class Fruit extends Enum {
}
```

(The constant values (0s) are meaningless, but required by PHP. In versions before 7.1, that only support public
constant members, it is recommended to not abuse them.)
(The constant values (0s) are meaningless, but required by PHP.)

This will expose the labels as magic static methods `Fruit::APPLE()` and `Fruit::BANANA()`.
They serve as the constructors of the corresponding labels.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
},

"require": {
"php": "^7.2",
"php": "^7.4|^8.0",
"ext-json": "*"
},

"require-dev": {
"phpunit/phpunit": ">=8.5.8"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^0.12.63"
}
}
14 changes: 7 additions & 7 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ abstract class Enum implements JsonSerializable
/**
* @var string[][]
*/
private static $constantsArray = array();
private static $constantsArray = [];

/**
* @var static[][]
*/
private static $instancesArray = array();
private static $instancesArray = [];

/**
* @var bool[]
*/
private static $parentIsEnumArray = array();
private static $parentIsEnumArray = [];

/**
* @param string $name
Expand Down Expand Up @@ -217,7 +217,7 @@ final public static function getEnumBaseClass()
*
* @return string
*/
final public static function getClass()
final public static function getClass(): string
{
return get_called_class();
}
Expand Down Expand Up @@ -323,7 +323,7 @@ final public function when(Enum $a, $b): Enum\Matcher
*
* @return Enum\Matcher
*/
final public function whenDo(Enum $a, callable $b)
final public function whenDo(Enum $a, callable $b): Enum\Matcher
{
$map = new Enum\Matcher($this);

Expand Down Expand Up @@ -351,7 +351,7 @@ final public function whenDo(Enum $a, callable $b)
* @return bool
* @throws InvalidArgumentException If $b is not of the same type as this Enum.
*/
final public function equals(Enum $b)
final public function equals(Enum $b): bool
{
$aClass = get_class($this);
$bClass = get_class($b);
Expand Down Expand Up @@ -443,7 +443,7 @@ final public static function __callStatic($name, array $arguments)
* @return string
* @internal
*/
public function __toString()
public function __toString(): string
{
return static::getClass() . '::' . $this->constName . '()';
}
Expand Down
65 changes: 21 additions & 44 deletions src/Enum/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use InvalidArgumentException;
use Vcn\Lib\Enum;
use Vcn\Lib\Enum\Matcher\Match;

/**
* @template TEnum of Enum
Expand All @@ -16,22 +15,18 @@ final class Matcher
{
/**
* @phpstan-var TEnum
*
* @var Enum
*/
private $subject;
private Enum $subject;

/**
* @phpstan-var array<Match<TResult>>
* @phpstan-var array<callable(): TResult>
*
* @var Match[] Map String Match
* @var callable[] Map String Match
*/
private $matches = array();
private array $callables = [];

/**
* @phpstan-var Match<TResult>
*
* @var Match
* @phpstan-var callable(): TResult
*/
private $surrogate;

Expand All @@ -40,34 +35,27 @@ final class Matcher
*
* @phpstan-param TEnum $subject
*
* @param Enum $subject
*
* @see Enum::when()
*
* @internal
*/
public function __construct(Enum $subject)
{
$this->subject = $subject;
$this->surrogate = new Match\Callback(
function () use ($subject) {
throw new Matcher\Exception\MatchExhausted($subject);
}
);
$this->surrogate = function () use ($subject) {
throw new Matcher\Exception\MatchExhausted($subject);
};
}

/**
* @phpstan-param TEnum $enum
* @phpstan-param Match<TResult> $match
* @phpstan-param callable(): TResult $callable
*
* @phpstan-return Matcher<TEnum, TResult>
*
* @param Enum $enum
* @param Match $match
*
* @return Matcher
*/
private function whenMatch(Enum $enum, Match $match): Matcher
private function whenMatch(Enum $enum, callable $callable): Matcher
{
if (!$enum instanceof $this->subject) {
$classSubject = get_class($this->subject);
Expand All @@ -79,14 +67,13 @@ private function whenMatch(Enum $enum, Match $match): Matcher
);
}

if (array_key_exists($enum->getName(), $this->matches)) {
if (array_key_exists($enum->getName(), $this->callables)) {
throw new InvalidArgumentException(
"This map has already mapped instance {$enum} to a value."
);
}

/** @noinspection PhpInternalEntityUsedInspection */
$this->matches[$enum->getName()] = $match;
$this->callables[$enum->getName()] = $callable;

return $this;
}
Expand Down Expand Up @@ -137,8 +124,7 @@ private function whenMatch(Enum $enum, Match $match): Matcher
*/
public function when(Enum $enum, $value): Matcher
{
/** @noinspection PhpInternalEntityUsedInspection */
return $this->whenMatch($enum, new Match\Value($value));
return $this->whenMatch($enum, fn () => $value);
}

/**
Expand Down Expand Up @@ -189,29 +175,22 @@ public function when(Enum $enum, $value): Matcher
*
* @phpstan-return Matcher<TEnum, TResult>
*
* @param Enum $enum
* @param callable $callable
*
* @return Matcher
*/
public function whenDo(Enum $enum, callable $callable): Matcher
{
return $this->whenMatch($enum, new Match\Callback($callable));
return $this->whenMatch($enum, $callable);
}

/**
* @phpstan-param Match<TResult> $match
* @phpstan-param callable(): TResult $callable
* @phpstan-return TResult
*
* @param Match $match
*
* @return mixed
*/
private function orElseMatch(Match $match)
private function orElseMatch(callable $callable)
{
return array_key_exists($this->subject->getName(), $this->matches)
? $this->matches[$this->subject->getName()]->get()
: $match->get();
return ($this->callables[$this->subject->getName()] ?? $callable)();
}

/**
Expand Down Expand Up @@ -256,7 +235,7 @@ private function orElseMatch(Match $match)
*/
public function orElse($value)
{
return $this->orElseMatch(new Match\Value($value));
return $this->orElseMatch(fn () => $value);
}

/**
Expand Down Expand Up @@ -300,7 +279,7 @@ public function orElse($value)
* It is <strong>discouraged</strong> to throw checked exceptions since PHPStorm can't infer the corresponding
* throws clause on this method.
*
* @phpstan-param callable(): TResult
* @phpstan-param callable(): TResult $callable
* @phpstan-return TResult
*
* @param callable $callable
Expand All @@ -309,7 +288,7 @@ public function orElse($value)
*/
public function orElseDo(callable $callable)
{
return $this->orElseMatch(new Match\Callback($callable));
return $this->orElseMatch($callable);
}

/**
Expand Down Expand Up @@ -368,8 +347,6 @@ function () {
*/
public function get()
{
return array_key_exists($this->subject->getName(), $this->matches)
? $this->matches[$this->subject->getName()]->get()
: $this->surrogate->get();
return $this->orElseDo($this->surrogate);
}
}
17 changes: 0 additions & 17 deletions src/Enum/Matcher/Match.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Enum/Matcher/Match/Callback.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Enum/Matcher/Match/Value.php

This file was deleted.

16 changes: 0 additions & 16 deletions tests/Enum/One.php

This file was deleted.