Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 1a50d16

Browse files
author
Petr Knap
committed
Items renamed to constants and key renamed to constantName
1 parent da45936 commit 1a50d16

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

src/Enum/AbstractEnum.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,36 @@ abstract class AbstractEnum
2121
/**
2222
* @var mixed[][]
2323
*/
24-
private static $items = [];
24+
private static $constants = [];
2525

2626
/**
2727
* @var mixed
2828
*/
29-
private $key;
29+
private $constantName;
3030

3131
/**
3232
* @var mixed
3333
*/
34-
private $value;
34+
private $constantValue;
3535

3636
/**
37-
* @param mixed $key
37+
* @param mixed $constantName
3838
* @throws EnumException
3939
*/
40-
protected function __construct($key)
40+
protected function __construct($constantName)
4141
{
42-
$this->key = $key;
43-
$this->value = $this->get($key);
42+
$this->constantName = $constantName;
43+
$this->constantValue = $this->get($constantName);
4444
}
4545

4646
/**
4747
* Creates magical factories for easier access to enum
4848
*
49-
* @param mixed $key enum key
49+
* @param mixed $constantName enum key
5050
* @param array $args ignored
5151
* @return mixed
5252
*/
53-
public static function __callStatic($key, array $args)
53+
public static function __callStatic($constantName, array $args)
5454
{
5555
$className = get_called_class();
5656

@@ -60,66 +60,66 @@ public static function __callStatic($key, array $args)
6060
$instances = [];
6161
}
6262

63-
$instance = &$instances[$key];
63+
$instance = &$instances[$constantName];
6464

6565
if (!($instance instanceof $className)) {
66-
$instance = new $className($key);
66+
$instance = new $className($constantName);
6767
}
6868

6969
return $instance;
7070
}
7171

7272
/**
73-
* @return mixed
73+
* @return string
7474
*/
75-
public function getKey()
75+
public function getName()
7676
{
77-
return $this->key;
77+
return $this->constantName;
7878
}
7979

8080
/**
8181
* @return mixed
8282
*/
8383
public function getValue()
8484
{
85-
return $this->value;
85+
return $this->constantValue;
8686
}
8787

8888
/**
89-
* @param mixed[] $items
89+
* @param mixed[] $constants
9090
*/
91-
protected static function setItems(array $items)
91+
protected static function setConstants(array $constants)
9292
{
93-
self::$items[get_called_class()] = $items;
93+
self::$constants[get_called_class()] = $constants;
9494
}
9595

9696
/**
97-
* @param mixed $key
97+
* @param string $constantName
9898
* @return bool
9999
*/
100-
private function exists($key)
100+
private function exists($constantName)
101101
{
102-
return array_key_exists($key, self::$items[get_called_class()]);
102+
return array_key_exists($constantName, self::$constants[get_called_class()]);
103103
}
104104

105105
/**
106-
* @param mixed $key
106+
* @param string $constantName
107107
* @return mixed
108108
* @throws EnumException
109109
*/
110-
private function get($key)
110+
private function get($constantName)
111111
{
112-
if (!$this->exists($key)) {
112+
if (!$this->exists($constantName)) {
113113
throw new EnumException(
114114
sprintf(
115115
"%s does not exists in %s",
116-
$key,
116+
$constantName,
117117
get_called_class()
118118
),
119119
EnumException::OUT_OF_RANGE
120120
);
121121
}
122122

123-
return self::$items[get_called_class()][$key];
123+
return self::$constants[get_called_class()][$constantName];
124124
}
125125
}

tests/Enum/EnumTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,31 @@ public function wrongKeyProvider()
1919

2020
/**
2121
* @dataProvider goodKeyProvider
22-
* @param string $key
23-
* @param string $value
22+
* @param string $name
23+
* @param mixed $value
2424
*/
25-
public function testEnumMagicConstruction_GoodKey($key, $value)
25+
public function testEnumMagicConstruction_GoodKey($name, $value)
2626
{
2727
/** @var EnumMock $enum */
28-
$enum = EnumMock::$key();
28+
$enum = EnumMock::$name();
2929

3030
$this->assertInstanceOf(EnumMock::getClass(), $enum);
31-
$this->assertSame($key, $enum->getKey());
31+
$this->assertSame($name, $enum->getName());
3232
$this->assertSame($value, $enum->getValue());
3333
}
3434

3535
/**
3636
* @dataProvider wrongKeyProvider
37-
* @param string $key
37+
* @param string $name
3838
*/
39-
public function testEnumMagicConstruction_WrongKey($key)
39+
public function testEnumMagicConstruction_WrongKey($name)
4040
{
4141
$this->setExpectedException(
4242
get_class(new EnumException()),
4343
"",
4444
EnumException::OUT_OF_RANGE
4545
);
4646

47-
EnumMock::$key();
47+
EnumMock::$name();
4848
}
4949
}

tests/Enum/EnumTest/EnumMock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EnumMock extends AbstractEnum
1212
{
1313
protected function __construct($key)
1414
{
15-
self::setItems([
15+
self::setConstants([
1616
"A" => "a",
1717
"B" => "b"
1818
]);

0 commit comments

Comments
 (0)