Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-chepurnoi committed Nov 22, 2016
1 parent 32cdfa6 commit b224a3e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 44 deletions.
23 changes: 23 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$finder = Symfony\CS\Finder::create()
->exclude('vendor')
->in([__DIR__]);

$config = Symfony\CS\Config::create()
->fixers([
'-phpdoc_params',
'-phpdoc_short_description',
'-phpdoc_inline_tag',
'-pre_increment',
'-heredoc_to_nowdoc',
'-spaces_cast',
'-include',
'-phpdoc_no_package',
'concat_with_spaces',
'ordered_use',
'short_array_syntax',
])
->finder($finder);

return $config;
12 changes: 3 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ php:
- 5.5
- 5.6
- 7.0
- hhvm

# run build against hhvm but allow them to fail
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
matrix:
fast_finish: true
allow_failures:
- php: hhvm

# faster builds on new travis setup not using sudo
sudo: false
Expand All @@ -21,6 +13,7 @@ sudo: false
cache:
directories:
- $HOME/.composer/cache
- vendor

install:
- travis_retry composer self-update && composer --version
Expand All @@ -29,4 +22,5 @@ install:
- travis_retry composer install --prefer-dist --no-interaction

script:
- phpunit --verbose $PHPUNIT_FLAGS
- vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff
- phpunit --verbose $PHPUNIT_FLAGS
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "yii2mod/yii2-enum",
"description": "Yii2 Enumerable helpers",
"type": "yii2-extension",
"keywords": ["yii2", "extension"],
"keywords": [
"yii2",
"yii2 enum",
"enumerable helper"
],
"license": "MIT",
"authors": [
{
Expand All @@ -13,6 +17,9 @@
"require": {
"yiisoft/yii2": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~1.7"
},
"autoload": {
"psr-4": {
"yii2mod\\enum\\": ""
Expand Down
61 changes: 36 additions & 25 deletions helpers/BaseEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

/**
* Class BaseEnum
*
* @author Dmitry Semenov <[email protected]>
*
* @package yii2mod\enum\helpers
*/
abstract class BaseEnum
Expand Down Expand Up @@ -48,14 +50,14 @@ abstract class BaseEnum
/**
* Sets the value that will be managed by this type instance.
*
* @param mixed $value The value to be managed.
* @param mixed $value The value to be managed
*
* @throws BadRequestHttpException If the value is not valid.
* @throws BadRequestHttpException If the value is not valid
*/
public function __construct($value)
{
if (!self::isValidValue($value)) {
throw new BadRequestHttpException;
throw new BadRequestHttpException();
}

$this->value = $value;
Expand All @@ -64,10 +66,10 @@ public function __construct($value)
/**
* Creates a new type instance for a called name.
*
* @param string $name The name of the value.
* @param array $arguments An ignored list of arguments.
* @param string $name The name of the value
* @param array $arguments An ignored list of arguments
*
* @return $this The new type instance.
* @return $this The new type instance
*/
public static function __callStatic($name, array $arguments = [])
{
Expand All @@ -77,57 +79,62 @@ public static function __callStatic($name, array $arguments = [])
/**
* Creates a new type instance using the name of a value.
*
* @param string $name The name of a value.
* @param string $name The name of a value
*
* @throws \yii\web\BadRequestHttpException
* @return $this The new type instance.
*
* @return $this The new type instance
*/
public static function createByName($name)
{
$constants = self::getConstantsByName();

if (!array_key_exists($name, $constants)) {
throw new BadRequestHttpException;
throw new BadRequestHttpException();
}

return new static($constants[$name]);
}

/**
* get constant key by value(label)
*
* @param $value
*
* @return mixed
*/
public static function getValueByName($value)
{
$list = self::listData();

return array_search($value, $list);
}

/**
* Creates a new type instance using the value.
*
* @param mixed $value The value.
* @param mixed $value The value
*
* @throws \yii\web\BadRequestHttpException
* @return $this The new type instance.
*
* @return $this The new type instance
*/
public static function createByValue($value)
{
$constants = self::getConstantsByValue();

if (!array_key_exists($value, $constants)) {
throw new BadRequestHttpException;
throw new BadRequestHttpException();
}

return new static($value);
}

/**
* Get list data
*
* @static
*
* @return mixed
*/
public static function listData()
Expand All @@ -140,12 +147,15 @@ public static function listData()
$result = ArrayHelper::getColumn(self::$list[$class], function ($value) {
return Yii::t(self::$messageCategory, $value);
});

return $result;
}

/**
/**
* Get label by value
*
* @var string value
*
* @return string label
*/
public static function getLabel($value)
Expand All @@ -154,13 +164,14 @@ public static function getLabel($value)
if (isset($list[$value])) {
return Yii::t(static::$messageCategory, $list[$value]);
}

return null;
}

/**
* Returns the list of constants (by name) for this type.
*
* @return array The list of constants by name.
* @return array The list of constants by name
*/
public static function getConstantsByName()
{
Expand All @@ -187,7 +198,7 @@ public static function getConstantsByName()
/**
* Returns the list of constants (by value) for this type.
*
* @return array The list of constants by value.
* @return array The list of constants by value
*/
public static function getConstantsByValue()
{
Expand All @@ -202,10 +213,10 @@ public static function getConstantsByValue()
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],
];
}
self::$byValue[$class][$value][] = $name;;
self::$byValue[$class][$value][] = $name;
} else {
self::$byValue[$class][$value] = $name;
}
Expand All @@ -218,7 +229,7 @@ public static function getConstantsByValue()
/**
* Returns the name of the value.
*
* @return array|string The name, or names, of the value.
* @return array|string The name, or names, of the value
*/
public function getName()
{
Expand All @@ -230,7 +241,7 @@ public function getName()
/**
* Unwraps the type and returns the raw value.
*
* @return mixed The raw value managed by the type instance.
* @return mixed The raw value managed by the type instance
*/
public function getValue()
{
Expand All @@ -240,10 +251,10 @@ public function getValue()
/**
* Checks if a name is valid for this type.
*
* @param string $name The name of the value.
* @param string $name The name of the value
*
* @return boolean If the name is valid for this type, `true` is returned.
* Otherwise, the name is not valid and `false` is returned.
* @return bool If the name is valid for this type, `true` is returned.
* Otherwise, the name is not valid and `false` is returned
*/
public static function isValidName($name)
{
Expand All @@ -255,10 +266,10 @@ public static function isValidName($name)
/**
* Checks if a value is valid for this type.
*
* @param string $value The value.
* @param string $value The value
*
* @return boolean If the value is valid for this type, `true` is returned.
* Otherwise, the value is not valid and `false` is returned.
* @return bool If the value is valid for this type, `true` is returned.
* Otherwise, the value is not valid and `false` is returned
*/
public static function isValidValue($value)
{
Expand Down
3 changes: 2 additions & 1 deletion helpers/BooleanEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class BooleanEnum
*
* @package yii2mod\enum\helpers
*/
class BooleanEnum extends BaseEnum
Expand All @@ -13,6 +14,6 @@ class BooleanEnum extends BaseEnum

public static $list = [
self::YES => 'Yes',
self::NO => 'No'
self::NO => 'No',
];
}
3 changes: 2 additions & 1 deletion tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Class EnumTest
*
* @package yii2mod\enum\tests
*/
class EnumTest extends TestCase
Expand All @@ -26,4 +27,4 @@ public function testValidation()
$this->assertTrue(BooleanEnum::isValidValue(1));
$this->assertFalse(BooleanEnum::isValidValue('YES'));
}
}
}
9 changes: 5 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace yii2mod\enum\tests;

use yii\helpers\ArrayHelper;
use Yii;
use yii\helpers\ArrayHelper;

/**
* This is the base class for all yii framework unit tests.
Expand All @@ -24,6 +24,7 @@ protected function tearDown()
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
*
* @param array $config The application configuration, if needed
* @param string $appClass name of the application class to create
*/
Expand All @@ -39,11 +40,11 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@app/messages', // if advanced application, set @frontend/messages
'sourceLanguage' => 'en'
'sourceLanguage' => 'en',
],
],
],
]
],
], $config));
}

Expand All @@ -62,4 +63,4 @@ protected function destroyApplication()
{
Yii::$app = null;
}
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
5 changes: 3 additions & 2 deletions tests/data/BooleanEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Class BooleanEnum
*
* @package yii2mod\enum\tests\data
*/
class BooleanEnum extends BaseEnum
Expand All @@ -22,6 +23,6 @@ class BooleanEnum extends BaseEnum

public static $list = [
self::YES => 'Yes',
self::NO => 'No'
self::NO => 'No',
];
}
}

0 comments on commit b224a3e

Please sign in to comment.