Skip to content

Commit 829ef9e

Browse files
authored
Merge pull request #6 from dbalabka/code-cleanup
Code cleanup
2 parents 7453456 + f5340dc commit 829ef9e

29 files changed

+137
-91
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and [Python Enums](https://docs.python.org/3/library/enum.html).
1919
A basic way to declare a named Enumeration class:
2020
```php
2121
<?php
22-
use Dbalabka\Enumeration;
22+
use Dbalabka\Enumeration\Enumeration;
2323

2424
final class Action extends Enumeration
2525
{
@@ -126,14 +126,16 @@ multiple Enumeration classes.
126126
> On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations...
127127
> (from [Python Enum documentation](https://docs.python.org/3/library/enum.html#restricted-enum-subclassing))
128128
2. Constructor should always be declared as non-public (`private` or `protected`) to avoid unwanted class instantiation.
129-
3. Implementation is based on assumption that all class static properties are elements of Enum. If there is a need to declare
130-
any static property that isn't an Enum element then you should override the `\Dbalabka\Enumeration::getStaticVars()` method.
131-
4. The method `Dbalabka\Enumeration::initialize()` should be called after each Enumeration class declaration. Please use the
129+
3. Implementation is based on assumption that all class static properties are elements of Enum. <!-- If there is a need to declare
130+
any static property that isn't an Enum element then you should override the `\Dbalabka\Enumeration\Enumeration::$notEnumVars` method. -->
131+
4. The method `Dbalabka\Enumeration\Enumeration::initialize()` should be called after each Enumeration class declaration. Please use the
132132
[vladimmi/construct-static](https://github.com/vladimmi/construct-static) custom loader to avoid boilerplate code.
133133

134134
## Usage
135135
```php
136136
<?php
137+
use Dbalabka\Enumeration\Examples\Enum\Action;
138+
137139
$viewAction = Action::$view;
138140

139141
// it is possible to compare Enum elements
@@ -147,6 +149,7 @@ assert($editAction === Action::$edit);
147149
foreach (Action::values() as $name => $action) {
148150
assert($action instanceof Action);
149151
assert($name === (string) $action);
152+
assert($name === $action->name());
150153
}
151154
```
152155

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
},
2424
"autoload": {
2525
"psr-4": {
26-
"Dbalabka\\": "src"
26+
"Dbalabka\\Enumeration\\": "src"
2727
}
2828
},
2929
"autoload-dev": {
3030
"psr-4": {
31-
"Dbalabka\\Examples\\": "examples",
32-
"Dbalabka\\Tests\\": "tests"
31+
"Dbalabka\\Enumeration\\Examples\\": "examples",
32+
"Dbalabka\\Enumeration\\Tests\\": "tests"
3333
}
3434
}
3535
}

examples/Enum/Action.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class Action extends Enumeration
99
{

examples/Enum/CardType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class CardType extends Enumeration
99
{

examples/Enum/Color.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class Color extends Enumeration
99
{

examples/Enum/Day.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class Day extends Enumeration
99
{

examples/Enum/Flag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class Flag extends Enumeration
99
{

examples/Enum/Planet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Enum;
4+
namespace Dbalabka\Enumeration\Examples\Enum;
55

6-
use Dbalabka\Enumeration;
6+
use Dbalabka\Enumeration\Enumeration;
77

88
final class Planet extends Enumeration
99
{

examples/card_type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\CardType;
4+
use Dbalabka\Enumeration\Examples\Enum\CardType;
55

66
require_once(__DIR__ . '/../vendor/autoload.php');
77

examples/class_static_construct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\Color;
4+
use Dbalabka\Enumeration\Examples\Enum\Color;
55

66
$composer = require_once(__DIR__ . '/../vendor/autoload.php');
77
$loader = new ConstructStatic\Loader($composer);

examples/day.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\Day;
4+
use Dbalabka\Enumeration\Examples\Enum\Day;
55

6-
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
6+
if (version_compare(PHP_VERSION, '7.4.0beta', '<')) {
77
trigger_error('This code requires PHP >= 7.4', E_USER_NOTICE);
88
return;
99
}

examples/flag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\Flag;
4+
use Dbalabka\Enumeration\Examples\Enum\Flag;
55

66
require_once(__DIR__ . '/../vendor/autoload.php');
77

examples/php-enum_comparision.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\Action;
4+
use Dbalabka\Enumeration\Examples\Enum\Action;
55

6-
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
6+
if (version_compare(PHP_VERSION, '7.4.0beta', '<')) {
77
trigger_error('This code requires PHP >= 7.4', E_USER_NOTICE);
88
return;
99
}

examples/planet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Examples\Enum\Planet;
4+
use Dbalabka\Enumeration\Examples\Enum\Planet;
55

6-
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
6+
if (version_compare(PHP_VERSION, '7.4.0beta', '<')) {
77
trigger_error('This code requires PHP >= 7.4', E_USER_NOTICE);
88
return;
99
}

examples/serialization_php74.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\EnumerationException;
5-
use Dbalabka\Examples\Enum\Color;
4+
use Dbalabka\Enumeration\Exception\EnumerationException;
5+
use Dbalabka\Enumeration\Examples\Enum\Color;
66

7-
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
7+
if (version_compare(PHP_VERSION, '7.4.0beta', '<')) {
88
trigger_error('This code requires PHP >= 7.4', E_USER_NOTICE);
99
return;
1010
}

src/Enumeration.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka;
4+
namespace Dbalabka\Enumeration;
55

6-
use ArrayAccessible;
7-
use Dbalabka\Exception\InitializationException;
8-
use Exception;
9-
use function get_class;
6+
use Dbalabka\Enumeration\Exception\EnumerationException;
7+
use Dbalabka\Enumeration\Exception\InvalidArgumentException;
8+
use function array_search;
109
use function get_class_vars;
1110
use function sprintf;
1211

@@ -53,19 +52,23 @@ final public static function initialize(): void
5352
static::initializeOrdinals();
5453
}
5554

56-
protected static function initializeOrdinals() : void
55+
final protected static function initializeOrdinals() : void
5756
{
5857
$ordinal = static::INITIAL_ORDINAL;
5958
foreach (static::values() as $value) {
6059
$value->ordinal = $ordinal++;
6160
}
6261
}
6362

63+
/**
64+
* Override this method to manually initialize Enum values. Useful when __construct() accepts at least one argument.
65+
* Enum objects does not have any properties by default.
66+
*/
6467
protected static function initializeValues() : void
6568
{
6669
$firstEnumItem = new static();
6770

68-
$staticVars = static::getStaticVars($firstEnumItem);
71+
$staticVars = static::getEnumStaticVars($firstEnumItem);
6972

7073
$firstEnumName = key($staticVars);
7174
static::$$firstEnumName = $firstEnumItem;
@@ -76,7 +79,7 @@ protected static function initializeValues() : void
7679
}
7780
}
7881

79-
private static function getStaticVars(Enumeration $enum): array
82+
final protected static function getEnumStaticVars(Enumeration $enum): array
8083
{
8184
$nonStaticVars = get_object_vars($enum);
8285
$allVars = get_class_vars(static::class);
@@ -88,7 +91,7 @@ private static function getStaticVars(Enumeration $enum): array
8891
/**
8992
* @return static[]
9093
*/
91-
public static function values() : array
94+
final public static function values() : array
9295
{
9396
return array_filter(
9497
get_class_vars(static::class),
@@ -101,24 +104,27 @@ static function ($value) {
101104
/**
102105
* @return static
103106
*/
104-
public static function valueOf(string $name)
107+
final public static function valueOf(string $name)
105108
{
106109
if ($value = static::values()[$name] ?? null) {
107110
return $value;
108111
}
109112
throw new InvalidArgumentException(sprintf('Invalid "%s" enum item name', $name));
110113
}
111114

115+
/**
116+
* Override default constructor to set object properties for each Enum value.
117+
*/
112118
protected function __construct()
113119
{
114120
}
115121

116-
public function ordinal() : int
122+
final public function ordinal() : int
117123
{
118124
if (null === $this->ordinal) {
119125
// When we call ordinal() in constructor the ordinal isn't initialized yet.
120126
// It is the only one case when ordinal isn't initialized.
121-
$staticVars = static::getStaticVars($this);
127+
$staticVars = static::getEnumStaticVars($this);
122128
$ordinal = static::INITIAL_ORDINAL;
123129
foreach ($staticVars as $var) {
124130
if ($var === null || $var === $this) {
@@ -131,19 +137,23 @@ public function ordinal() : int
131137
return $this->ordinal;
132138
}
133139

134-
public function compareTo(self $enum) : int
140+
final public function compareTo(self $enum) : int
135141
{
136142
return $this->ordinal() - $enum->ordinal();
137143
}
138144

145+
/**
146+
* Override this method to custom "programmer-friendly" string representation of Enum value.
147+
* It returns Enum name by default.
148+
*/
139149
public function __toString() : string
140150
{
141151
return $this->name();
142152
}
143153

144-
public function name() : string
154+
final public function name() : string
145155
{
146-
return \array_search($this, static::values(), true);
156+
return array_search($this, static::values(), true);
147157
}
148158

149159
final public function __clone()

src/EnumerationException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Dbalabka\Enumeration\Exception;
5+
6+
use Exception;
7+
8+
/**
9+
* @author Dmitry Balabka <[email protected]>
10+
*/
11+
class EnumerationException extends Exception
12+
{
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Dbalabka\Enumeration\Exception;
5+
6+
use InvalidArgumentException as BaseInvalidArgumentException;
7+
8+
/**
9+
* @author Dmitry Balabka <[email protected]>
10+
*/
11+
class InvalidArgumentException extends BaseInvalidArgumentException
12+
{
13+
14+
}

src/InvalidArgumentException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/EnumerationTest.php

985 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)