From b224a3ebbbdf0e28501c237c3e7733167a044bde Mon Sep 17 00:00:00 2001 From: igor-chepurnoi Date: Tue, 22 Nov 2016 16:57:46 +0200 Subject: [PATCH] fix code style --- .php_cs | 23 ++++++++++++++ .travis.yml | 12 ++------ composer.json | 9 +++++- helpers/BaseEnum.php | 61 ++++++++++++++++++++++---------------- helpers/BooleanEnum.php | 3 +- tests/EnumTest.php | 3 +- tests/TestCase.php | 9 +++--- tests/bootstrap.php | 2 +- tests/data/BooleanEnum.php | 5 ++-- 9 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..b810a08 --- /dev/null +++ b/.php_cs @@ -0,0 +1,23 @@ +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; diff --git a/.travis.yml b/.travis.yml index 66e5895..8deeddd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -21,6 +13,7 @@ sudo: false cache: directories: - $HOME/.composer/cache + - vendor install: - travis_retry composer self-update && composer --version @@ -29,4 +22,5 @@ install: - travis_retry composer install --prefer-dist --no-interaction script: - - phpunit --verbose $PHPUNIT_FLAGS \ No newline at end of file + - vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff + - phpunit --verbose $PHPUNIT_FLAGS diff --git a/composer.json b/composer.json index 5c74bdc..9a8a62d 100644 --- a/composer.json +++ b/composer.json @@ -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": [ { @@ -13,6 +17,9 @@ "require": { "yiisoft/yii2": "*" }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~1.7" + }, "autoload": { "psr-4": { "yii2mod\\enum\\": "" diff --git a/helpers/BaseEnum.php b/helpers/BaseEnum.php index 3a9b4b5..97e807f 100755 --- a/helpers/BaseEnum.php +++ b/helpers/BaseEnum.php @@ -9,7 +9,9 @@ /** * Class BaseEnum + * * @author Dmitry Semenov + * * @package yii2mod\enum\helpers */ abstract class BaseEnum @@ -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; @@ -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 = []) { @@ -77,18 +79,18 @@ 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]); @@ -96,30 +98,33 @@ public static function createByName($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); @@ -127,7 +132,9 @@ public static function createByValue($value) /** * Get list data + * * @static + * * @return mixed */ public static function listData() @@ -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) @@ -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() { @@ -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() { @@ -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; } @@ -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() { @@ -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() { @@ -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) { @@ -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) { diff --git a/helpers/BooleanEnum.php b/helpers/BooleanEnum.php index b6489e0..98e8d37 100755 --- a/helpers/BooleanEnum.php +++ b/helpers/BooleanEnum.php @@ -4,6 +4,7 @@ /** * Class BooleanEnum + * * @package yii2mod\enum\helpers */ class BooleanEnum extends BaseEnum @@ -13,6 +14,6 @@ class BooleanEnum extends BaseEnum public static $list = [ self::YES => 'Yes', - self::NO => 'No' + self::NO => 'No', ]; } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 6801005..0e777c2 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -6,6 +6,7 @@ /** * Class EnumTest + * * @package yii2mod\enum\tests */ class EnumTest extends TestCase @@ -26,4 +27,4 @@ public function testValidation() $this->assertTrue(BooleanEnum::isValidValue(1)); $this->assertFalse(BooleanEnum::isValidValue('YES')); } -} \ No newline at end of file +} diff --git a/tests/TestCase.php b/tests/TestCase.php index a38a0ff..c0463e3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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. @@ -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 */ @@ -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)); } @@ -62,4 +63,4 @@ protected function destroyApplication() { Yii::$app = null; } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index effa70f..10cfa10 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,4 +10,4 @@ $_SERVER['SCRIPT_FILENAME'] = __FILE__; require_once(__DIR__ . '/../vendor/autoload.php'); -require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); \ No newline at end of file +require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); diff --git a/tests/data/BooleanEnum.php b/tests/data/BooleanEnum.php index 08411c1..e7c0578 100644 --- a/tests/data/BooleanEnum.php +++ b/tests/data/BooleanEnum.php @@ -6,6 +6,7 @@ /** * Class BooleanEnum + * * @package yii2mod\enum\tests\data */ class BooleanEnum extends BaseEnum @@ -22,6 +23,6 @@ class BooleanEnum extends BaseEnum public static $list = [ self::YES => 'Yes', - self::NO => 'No' + self::NO => 'No', ]; -} \ No newline at end of file +}