Skip to content

Commit

Permalink
use static bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Apr 19, 2017
1 parent b5552de commit 7af030e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 73 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

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

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

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
{
"name": "Dmitry Semenov",
"email": "[email protected]"
},
{
"name": "Igor Chepurnoi",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6",
"yiisoft/yii2": "*"
},
"require-dev": {
Expand Down
90 changes: 23 additions & 67 deletions helpers/BaseEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ abstract class BaseEnum
*
* @var array
*/
private static $byName = [];
protected static $byName = [];

/**
* The cached list of constants by value.
*
* @var array
*/
private static $byValue = [];
protected static $byValue = [];

/**
* @var array list of properties
*/
private static $list;
protected static $list = [];

/**
* The value managed by this type instance.
*
* @var mixed
*/
private $_value;
protected $value;

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

$this->_value = $value;
$this->value = $value;
}

/**
Expand All @@ -73,7 +73,7 @@ public function __construct($value)
*/
public static function createByName($name)
{
$constants = self::getConstantsByName();
$constants = static::getConstantsByName();

if (!array_key_exists($name, $constants)) {
throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class());
Expand All @@ -91,9 +91,7 @@ public static function createByName($name)
*/
public static function getValueByName($value)
{
$list = self::listData();

return array_search($value, $list);
return array_search($value, static::listData());
}

/**
Expand All @@ -107,9 +105,7 @@ public static function getValueByName($value)
*/
public static function createByValue($value)
{
$constants = self::getConstantsByValue();

if (!array_key_exists($value, $constants)) {
if (!array_key_exists($value, static::getConstantsByValue())) {
throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class());
}

Expand All @@ -119,24 +115,13 @@ public static function createByValue($value)
/**
* Get list data
*
* @static
*
* @return mixed
*/
public static function listData()
{
$class = get_called_class();

if (!isset(self::$list[$class])) {
$reflection = new ReflectionClass($class);
self::$list[$class] = $reflection->getStaticPropertyValue('list');
}

$result = ArrayHelper::getColumn(self::$list[$class], function ($value) {
return Yii::t(self::$messageCategory, $value);
return ArrayHelper::getColumn(static::$list, function ($value) {
return Yii::t(static::$messageCategory, $value);
});

return $result;
}

/**
Expand Down Expand Up @@ -166,22 +151,12 @@ public static function getConstantsByName()
{
$class = get_called_class();

if (!isset(self::$byName[$class])) {
if (!array_key_exists($class, static::$byName)) {
$reflection = new ReflectionClass($class);
self::$byName[$class] = $reflection->getConstants();
while (false !== ($reflection = $reflection->getParentClass())) {
if (__CLASS__ === $reflection->getName()) {
break;
}

self::$byName[$class] = array_replace(
$reflection->getConstants(),
self::$byName[$class]
);
}
static::$byName[$class] = $reflection->getConstants();
}

return self::$byName[$class];
return static::$byName[$class];
}

/**
Expand All @@ -193,26 +168,11 @@ public static function getConstantsByValue()
{
$class = get_called_class();

if (!isset(self::$byValue[$class])) {
self::getConstantsByName();

self::$byValue[$class] = [];

foreach (self::$byName[$class] as $name => $value) {
if (array_key_exists($value, self::$byValue[$class])) {
if (!is_array(self::$byValue[$class][$value])) {
self::$byValue[$class][$value] = [
self::$byValue[$class][$value],
];
}
self::$byValue[$class][$value][] = $name;
} else {
self::$byValue[$class][$value] = $name;
}
}
if (!isset(static::$byValue[$class])) {
static::$byValue[$class] = array_flip(static::getConstantsByName());
}

return self::$byValue[$class];
return static::$byValue[$class];
}

/**
Expand All @@ -222,9 +182,9 @@ public static function getConstantsByValue()
*/
public function getName()
{
$constants = self::getConstantsByValue();
$constants = static::getConstantsByValue();

return $constants[$this->_value];
return $constants[$this->value];
}

/**
Expand All @@ -234,7 +194,7 @@ public function getName()
*/
public function getValue()
{
return $this->_value;
return $this->value;
}

/**
Expand All @@ -247,9 +207,7 @@ public function getValue()
*/
public static function isValidName($name)
{
$constants = self::getConstantsByName();

return array_key_exists($name, $constants);
return array_key_exists($name, static::getConstantsByName());
}

/**
Expand All @@ -262,9 +220,7 @@ public static function isValidName($name)
*/
public static function isValidValue($value)
{
$constants = self::getConstantsByValue();

return array_key_exists($value, $constants);
return array_key_exists($value, static::getConstantsByValue());
}

/**
Expand Down Expand Up @@ -293,6 +249,6 @@ public static function __callStatic($name, $arguments)
*/
public function __toString()
{
return (string)$this->_value;
return (string) $this->value;
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
/**
* This is the base class for all yii framework unit tests.
*/
class TestCase extends \PHPUnit_Framework_TestCase
class TestCase extends \PHPUnit\Framework\TestCase
{
protected function setUp()
{
parent::setUp();

$this->mockApplication();
}

Expand Down

0 comments on commit 7af030e

Please sign in to comment.