Browse Source

Fixes #6589: updated code style guide to mention else after return, adjusted code involved in the issue

tags/2.0.2
Alexander Makarov 10 years ago
parent
commit
f66b8ba571
  1. 23
      docs/internals/core-code-style.md
  2. 14
      framework/validators/Validator.php

23
docs/internals/core-code-style.md

@ -262,6 +262,29 @@ if (!$model && null === $event)
throw new Exception('test'); throw new Exception('test');
``` ```
Prefer avoiding `else` after `return` where it makes sense.
Use [guard conditions](http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html).
```php
$result = $this->getResult();
if (empty($result)) {
return true;
} else {
// process result
}
```
is better as
```php
$result = $this->getResult();
if (empty($result)) {
return true;
}
// process result
```
#### switch #### switch
Use the following formatting for switch: Use the following formatting for switch:

14
framework/validators/Validator.php

@ -267,14 +267,14 @@ class Validator extends Component
$result = $this->validateValue($value); $result = $this->validateValue($value);
if (empty($result)) { if (empty($result)) {
return true; return true;
} else {
list($message, $params) = $result;
$params['attribute'] = Yii::t('yii', 'the input value');
$params['value'] = is_array($value) ? 'array()' : $value;
$error = Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
return false;
} }
list($message, $params) = $result;
$params['attribute'] = Yii::t('yii', 'the input value');
$params['value'] = is_array($value) ? 'array()' : $value;
$error = Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
return false;
} }
/** /**

Loading…
Cancel
Save