Skip to content

Commit 7af030e

Browse files
author
Igor Chepurnoy
committed
use static bindings
1 parent b5552de commit 7af030e

File tree

4 files changed

+32
-73
lines changed

4 files changed

+32
-73
lines changed

.travis.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
- 7.0
4+
- 7.1
85

96
# faster builds on new travis setup not using sudo
107
sudo: false
@@ -17,7 +14,7 @@ cache:
1714

1815
install:
1916
- travis_retry composer self-update && composer --version
20-
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
17+
- travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0"
2118
- export PATH="$HOME/.composer/vendor/bin:$PATH"
2219
- travis_retry composer install --prefer-dist --no-interaction
2320

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
{
1313
"name": "Dmitry Semenov",
1414
"email": "[email protected]"
15+
},
16+
{
17+
"name": "Igor Chepurnoi",
18+
"email": "[email protected]"
1519
}
1620
],
1721
"require": {
22+
"php": ">=5.6",
1823
"yiisoft/yii2": "*"
1924
},
2025
"require-dev": {

helpers/BaseEnum.php

+23-67
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ abstract class BaseEnum
2525
*
2626
* @var array
2727
*/
28-
private static $byName = [];
28+
protected static $byName = [];
2929

3030
/**
3131
* The cached list of constants by value.
3232
*
3333
* @var array
3434
*/
35-
private static $byValue = [];
35+
protected static $byValue = [];
3636

3737
/**
3838
* @var array list of properties
3939
*/
40-
private static $list;
40+
protected static $list = [];
4141

4242
/**
4343
* The value managed by this type instance.
4444
*
4545
* @var mixed
4646
*/
47-
private $_value;
47+
protected $value;
4848

4949
/**
5050
* Sets the value that will be managed by this type instance.
@@ -55,11 +55,11 @@ abstract class BaseEnum
5555
*/
5656
public function __construct($value)
5757
{
58-
if (!self::isValidValue($value)) {
58+
if (!static::isValidValue($value)) {
5959
throw new UnexpectedValueException("Value '{$value}' is not part of the enum " . get_called_class());
6060
}
6161

62-
$this->_value = $value;
62+
$this->value = $value;
6363
}
6464

6565
/**
@@ -73,7 +73,7 @@ public function __construct($value)
7373
*/
7474
public static function createByName($name)
7575
{
76-
$constants = self::getConstantsByName();
76+
$constants = static::getConstantsByName();
7777

7878
if (!array_key_exists($name, $constants)) {
7979
throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class());
@@ -91,9 +91,7 @@ public static function createByName($name)
9191
*/
9292
public static function getValueByName($value)
9393
{
94-
$list = self::listData();
95-
96-
return array_search($value, $list);
94+
return array_search($value, static::listData());
9795
}
9896

9997
/**
@@ -107,9 +105,7 @@ public static function getValueByName($value)
107105
*/
108106
public static function createByValue($value)
109107
{
110-
$constants = self::getConstantsByValue();
111-
112-
if (!array_key_exists($value, $constants)) {
108+
if (!array_key_exists($value, static::getConstantsByValue())) {
113109
throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class());
114110
}
115111

@@ -119,24 +115,13 @@ public static function createByValue($value)
119115
/**
120116
* Get list data
121117
*
122-
* @static
123-
*
124118
* @return mixed
125119
*/
126120
public static function listData()
127121
{
128-
$class = get_called_class();
129-
130-
if (!isset(self::$list[$class])) {
131-
$reflection = new ReflectionClass($class);
132-
self::$list[$class] = $reflection->getStaticPropertyValue('list');
133-
}
134-
135-
$result = ArrayHelper::getColumn(self::$list[$class], function ($value) {
136-
return Yii::t(self::$messageCategory, $value);
122+
return ArrayHelper::getColumn(static::$list, function ($value) {
123+
return Yii::t(static::$messageCategory, $value);
137124
});
138-
139-
return $result;
140125
}
141126

142127
/**
@@ -166,22 +151,12 @@ public static function getConstantsByName()
166151
{
167152
$class = get_called_class();
168153

169-
if (!isset(self::$byName[$class])) {
154+
if (!array_key_exists($class, static::$byName)) {
170155
$reflection = new ReflectionClass($class);
171-
self::$byName[$class] = $reflection->getConstants();
172-
while (false !== ($reflection = $reflection->getParentClass())) {
173-
if (__CLASS__ === $reflection->getName()) {
174-
break;
175-
}
176-
177-
self::$byName[$class] = array_replace(
178-
$reflection->getConstants(),
179-
self::$byName[$class]
180-
);
181-
}
156+
static::$byName[$class] = $reflection->getConstants();
182157
}
183158

184-
return self::$byName[$class];
159+
return static::$byName[$class];
185160
}
186161

187162
/**
@@ -193,26 +168,11 @@ public static function getConstantsByValue()
193168
{
194169
$class = get_called_class();
195170

196-
if (!isset(self::$byValue[$class])) {
197-
self::getConstantsByName();
198-
199-
self::$byValue[$class] = [];
200-
201-
foreach (self::$byName[$class] as $name => $value) {
202-
if (array_key_exists($value, self::$byValue[$class])) {
203-
if (!is_array(self::$byValue[$class][$value])) {
204-
self::$byValue[$class][$value] = [
205-
self::$byValue[$class][$value],
206-
];
207-
}
208-
self::$byValue[$class][$value][] = $name;
209-
} else {
210-
self::$byValue[$class][$value] = $name;
211-
}
212-
}
171+
if (!isset(static::$byValue[$class])) {
172+
static::$byValue[$class] = array_flip(static::getConstantsByName());
213173
}
214174

215-
return self::$byValue[$class];
175+
return static::$byValue[$class];
216176
}
217177

218178
/**
@@ -222,9 +182,9 @@ public static function getConstantsByValue()
222182
*/
223183
public function getName()
224184
{
225-
$constants = self::getConstantsByValue();
185+
$constants = static::getConstantsByValue();
226186

227-
return $constants[$this->_value];
187+
return $constants[$this->value];
228188
}
229189

230190
/**
@@ -234,7 +194,7 @@ public function getName()
234194
*/
235195
public function getValue()
236196
{
237-
return $this->_value;
197+
return $this->value;
238198
}
239199

240200
/**
@@ -247,9 +207,7 @@ public function getValue()
247207
*/
248208
public static function isValidName($name)
249209
{
250-
$constants = self::getConstantsByName();
251-
252-
return array_key_exists($name, $constants);
210+
return array_key_exists($name, static::getConstantsByName());
253211
}
254212

255213
/**
@@ -262,9 +220,7 @@ public static function isValidName($name)
262220
*/
263221
public static function isValidValue($value)
264222
{
265-
$constants = self::getConstantsByValue();
266-
267-
return array_key_exists($value, $constants);
223+
return array_key_exists($value, static::getConstantsByValue());
268224
}
269225

270226
/**
@@ -293,6 +249,6 @@ public static function __callStatic($name, $arguments)
293249
*/
294250
public function __toString()
295251
{
296-
return (string)$this->_value;
252+
return (string) $this->value;
297253
}
298254
}

tests/TestCase.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
/**
99
* This is the base class for all yii framework unit tests.
1010
*/
11-
class TestCase extends \PHPUnit_Framework_TestCase
11+
class TestCase extends \PHPUnit\Framework\TestCase
1212
{
1313
protected function setUp()
1414
{
1515
parent::setUp();
16+
1617
$this->mockApplication();
1718
}
1819

0 commit comments

Comments
 (0)