Skip to content
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
57 changes: 36 additions & 21 deletions docs/api/classes/Ngmy-Enum-Enum.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h3 id="toc">
<dt class="phpdocumentor-table-of-contents__entry -property -private">
<a href="classes/Ngmy-Enum-Enum.html#property_names">$names</a>
<span>
&nbsp;: <abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, string&gt; </span>
&nbsp;: array&lt;string, <abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, string&gt;&gt; </span>
</dt>
<dd></dd>

Expand Down Expand Up @@ -188,7 +188,7 @@ <h3 id="toc">
<dt class="phpdocumentor-table-of-contents__entry -method -public">
<a href="classes/Ngmy-Enum-Enum.html#method_values">values()</a>
<span>
&nbsp;: <abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, self&gt; </span>
&nbsp;: <abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, static&gt; </span>
</dt>
<dd>Returns all constants of this enum type.</dd>

Expand Down Expand Up @@ -241,7 +241,7 @@ <h4 class="phpdocumentor-element__name" id="property_name">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">18</span>
<span class="phpdocumentor-element-found-in__line">21</span>
</aside>


Expand Down Expand Up @@ -272,19 +272,34 @@ <h4 class="phpdocumentor-element__name" id="property_names">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">15</span>
<span class="phpdocumentor-element-found-in__line">18</span>
</aside>


<code class="phpdocumentor-code phpdocumentor-signature ">
<span class="phpdocumentor-signature__visibility">private</span>
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__type"><abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, string&gt;</span>
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__type">array&lt;string, <abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, string&gt;&gt;</span>
<span class="phpdocumentor-signature__name">$names</span>
</code>
= <span class="phpdocumentor-signature__default-value">[]</span></code>

<section class="phpdocumentor-description"></section>


<h5 class="phpdocumentor-tag-list__heading" id="tags">
Tags
<a href="#tags" class="headerlink"><i class="fas fa-link"></i></a>
</h5>
<dl class="phpdocumentor-tag-list">
<dt class="phpdocumentor-tag-list__entry">
<span class="phpdocumentor-tag__name">phpstan-var</span>
</dt>
<dd class="phpdocumentor-tag-list__definition">

<section class="phpdocumentor-description"><p>array&lt;class-string, list<string>&gt;</p>
</section>

</dd>
</dl>

</article>
</section>
Expand All @@ -307,7 +322,7 @@ <h4 class="phpdocumentor-element__name" id="method___callStatic">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">26</span>
<span class="phpdocumentor-element-found-in__line">29</span>
</aside>

<p class="phpdocumentor-summary">Returns the enum constant of the specified name.</p>
Expand Down Expand Up @@ -359,7 +374,7 @@ <h4 class="phpdocumentor-element__name" id="method___set">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">136</span>
<span class="phpdocumentor-element-found-in__line">140</span>
</aside>


Expand Down Expand Up @@ -410,7 +425,7 @@ <h4 class="phpdocumentor-element__name" id="method___toString">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">96</span>
<span class="phpdocumentor-element-found-in__line">100</span>
</aside>

<p class="phpdocumentor-summary">Returns the name of this enum constant, as contained in the declaration.</p>
Expand Down Expand Up @@ -444,7 +459,7 @@ <h4 class="phpdocumentor-element__name" id="method___wakeup">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">141</span>
<span class="phpdocumentor-element-found-in__line">145</span>
</aside>


Expand Down Expand Up @@ -476,7 +491,7 @@ <h4 class="phpdocumentor-element__name" id="method_equals">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">115</span>
<span class="phpdocumentor-element-found-in__line">119</span>
</aside>

<p class="phpdocumentor-summary">Returns true if the specified object is equal to this enum constant.</p>
Expand Down Expand Up @@ -520,7 +535,7 @@ <h4 class="phpdocumentor-element__name" id="method_hashCode">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">123</span>
<span class="phpdocumentor-element-found-in__line">127</span>
</aside>

<p class="phpdocumentor-summary">Returns the hash code for this enum constant</p>
Expand Down Expand Up @@ -554,7 +569,7 @@ <h4 class="phpdocumentor-element__name" id="method_name">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">88</span>
<span class="phpdocumentor-element-found-in__line">92</span>
</aside>

<p class="phpdocumentor-summary">Returns the name of this enum constant, exactly as declared in its enum declaration.</p>
Expand Down Expand Up @@ -588,7 +603,7 @@ <h4 class="phpdocumentor-element__name" id="method_ordinal">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">106</span>
<span class="phpdocumentor-element-found-in__line">110</span>
</aside>

<p class="phpdocumentor-summary">Returns the ordinal of this enum constant.</p>
Expand Down Expand Up @@ -623,7 +638,7 @@ <h4 class="phpdocumentor-element__name" id="method_valueOf">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">36</span>
<span class="phpdocumentor-element-found-in__line">39</span>
</aside>

<p class="phpdocumentor-summary">Returns the enum constant of the specified name.</p>
Expand Down Expand Up @@ -667,22 +682,22 @@ <h4 class="phpdocumentor-element__name" id="method_values">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">46</span>
<span class="phpdocumentor-element-found-in__line">49</span>
</aside>

<p class="phpdocumentor-summary">Returns all constants of this enum type.</p>

<code class="phpdocumentor-code phpdocumentor-signature ">
<span class="phpdocumentor-signature__visibility">public</span>
<span class="phpdocumentor-signature__final">final</span> <span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">values</span><span>(</span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type"><abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, self&gt;</span></code>
<span class="phpdocumentor-signature__final">final</span> <span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">values</span><span>(</span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type"><abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, static&gt;</span></code>

<section class="phpdocumentor-description"></section>




<h5 class="phpdocumentor-return-value__heading">Return values</h5>
<span class="phpdocumentor-signature__response_type"><abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, self&gt;</span>
<span class="phpdocumentor-signature__response_type"><abbr title="\Ngmy\Enum\list">list</abbr>&lt;string|int, static&gt;</span>
&mdash;
<section class="phpdocumentor-description"></section>

Expand All @@ -701,7 +716,7 @@ <h4 class="phpdocumentor-element__name" id="method___clone">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">149</span>
<span class="phpdocumentor-element-found-in__line">153</span>
</aside>


Expand Down Expand Up @@ -734,7 +749,7 @@ <h4 class="phpdocumentor-element__name" id="method___construct">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">164</span>
<span class="phpdocumentor-element-found-in__line">168</span>
</aside>


Expand Down Expand Up @@ -776,7 +791,7 @@ <h4 class="phpdocumentor-element__name" id="method_validateInheritance">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="src/Enum.php"><a href="files/src-enum.html"><abbr title="src/Enum.php">Enum.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">154</span>
<span class="phpdocumentor-element-found-in__line">158</span>
</aside>


Expand Down
22 changes: 13 additions & 9 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

abstract class Enum
{
/** @var list<string> */
private static $names;
/**
* @var array<string, list<string>>
* @phpstan-var array<class-string, list<string>>
*/
private static $names = [];

/** @var string */
private $name;
Expand Down Expand Up @@ -41,7 +44,7 @@ final public static function valueOf(string $name): self
/**
* Returns all constants of this enum type.
*
* @return list<self>
* @return list<static>
*/
final public static function values(): array
{
Expand All @@ -59,11 +62,12 @@ final public static function values(): array
final public static function names(): array
{
self::validateInheritance();
if (isset(self::$names)) {
return self::$names;
$class = \get_called_class();
if (isset(self::$names[$class])) {
return self::$names[$class];
}
self::$names = [];
$reflectionClass = new ReflectionClass(\get_called_class());
self::$names[$class] = [];
$reflectionClass = new ReflectionClass($class);
$staticProperties = $reflectionClass->getStaticProperties();
foreach (\array_keys($staticProperties) as $propertyName) {
$reflectionProperty = $reflectionClass->getProperty($propertyName);
Expand All @@ -76,10 +80,10 @@ final public static function names(): array
if (!\preg_match('/@[^\s]+/', $line, $mathces) || $mathces[0] != '@enum') {
continue;
}
self::$names[] = $propertyName;
self::$names[$class][] = $propertyName;
}
}
return self::$names;
return self::$names[$class];
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/Data/Enum7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Ngmy\Enum\Tests\Data;

use InvalidArgumentException;
use Ngmy\Enum\Enum;

/**
* @method static self FOO()
* @method static self BAR()
* @method static self BAZ()
*/
class Enum7 extends Enum
{
/**
* @var int
* @enum
*/
private static $FOO = 1;
/**
* @var int
* @enum
*/
private static $BAR = 2;
/**
* @var int
* @enum
*/
private static $BAZ = 3;

public static function getInstance(int $value): self
{
foreach (self::values() as $enum) {
if ($enum->getValue() == $value) {
return $enum;
}
}
throw new InvalidArgumentException('The value "%s" is invalid.');
}

public function getValue(): int
{
return self::${$this->name()};
}
}
25 changes: 25 additions & 0 deletions tests/Data/Enum8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Ngmy\Enum\Tests\Data;

use Ngmy\Enum\Enum;

/**
* @method static self HOGE()
* @method static self FUGA()
*/
class Enum8 extends Enum
{
/**
* @var null
* @enum
*/
private static $HOGE;
/**
* @var null
* @enum
*/
private static $FUGA;
}
38 changes: 38 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public function testNames(string $class, $expected): void
$this->assertSame($expected, $fromCache);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testNamesSameProcessAnotherClass(): void
{
$this->assertSame(['FOO', 'BAR', 'BAZ'], Data\Enum1::names());
$this->assertSame(['HOGE', 'FUGA'], Data\Enum8::names());
}

/**
* @return array<int|string, array<int|string, mixed>>
*/
Expand Down Expand Up @@ -257,6 +267,34 @@ public function testHashCode(Enum $one, Enum $other, bool $expected): void
$this->assertSame($expected, $one->hashCode() === $other->hashCode());
}

/**
* @return array<int|string, array<int|string, mixed>>
*/
public function getInstanceProvider(): array
{
return [
[Data\Enum7::class, 1, Data\Enum7::valueOf('FOO')],
[Data\Enum7::class, 2, Data\Enum7::valueOf('BAR')],
[Data\Enum7::class, 3, Data\Enum7::valueOf('BAZ')],
[Data\Enum7::class, 4, new InvalidArgumentException()]
];
}

/**
* @param mixed $value
* @param Enum|Exception $expected
* @dataProvider getInstanceProvider
*
* @phpstan-param class-string $class
*/
public function testGetInstance(string $class, $value, $expected): void
{
if ($expected instanceof Exception) {
$this->expectException(\get_class($expected));
}
$this->assertEquals($expected, $class::getInstance($value));
}

/**
* @return array<int|string, array<int|string, mixed>>
*/
Expand Down