Skip to content

Commit

Permalink
Code cleanup (yiisoft#13113)
Browse files Browse the repository at this point in the history
* Simplified code branching, named variable better

* More simple code cleanup

* More consistent line breaks before return statemetns
  • Loading branch information
samdark authored Dec 2, 2016
1 parent 9c0e94e commit 954c771
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 116 deletions.
44 changes: 22 additions & 22 deletions framework/BaseYii.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,20 @@ public static function getAlias($alias, $throwException = true)
if (isset(static::$aliases[$root])) {
if (is_string(static::$aliases[$root])) {
return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
} else {
foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) {
return $path . substr($alias, strlen($name));
}
}

foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) {
return $path . substr($alias, strlen($name));
}
}
}

if ($throwException) {
throw new InvalidParamException("Invalid path alias: $alias");
} else {
return false;
}

return false;
}

/**
Expand All @@ -170,11 +170,11 @@ public static function getRootAlias($alias)
if (isset(static::$aliases[$root])) {
if (is_string(static::$aliases[$root])) {
return $root;
} else {
foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) {
return $name;
}
}

foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) {
return $name;
}
}
}
Expand Down Expand Up @@ -346,9 +346,9 @@ public static function createObject($type, array $params = [])
return static::$container->invoke($type, $params);
} elseif (is_array($type)) {
throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
} else {
throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));
}

throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));
}

private static $_logger;
Expand All @@ -360,9 +360,9 @@ public static function getLogger()
{
if (self::$_logger !== null) {
return self::$_logger;
} else {
return self::$_logger = static::createObject('yii\log\Logger');
}

return self::$_logger = static::createObject('yii\log\Logger');
}

/**
Expand Down Expand Up @@ -499,14 +499,14 @@ public static function t($category, $message, $params = [], $language = null)
{
if (static::$app !== null) {
return static::$app->getI18n()->translate($category, $message, $params, $language ?: static::$app->language);
} else {
$p = [];
foreach ((array) $params as $name => $value) {
$p['{' . $name . '}'] = $value;
}
}

return ($p === []) ? $message : strtr($message, $p);
$placeholders = [];
foreach ((array) $params as $name => $value) {
$placeholders['{' . $name . '}'] = $value;
}

return ($placeholders === []) ? $message : strtr($message, $placeholders);
}

/**
Expand Down
110 changes: 57 additions & 53 deletions framework/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,21 @@ public function __get($name)
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
} else {
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}

// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}

if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

/**
Expand Down Expand Up @@ -182,22 +183,22 @@ public function __set($name, $value)
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));

return;
} else {
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
}

return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}

if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

/**
Expand All @@ -219,15 +220,16 @@ public function __isset($name)
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
} else {
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}

// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}

return false;
}

Expand All @@ -250,16 +252,17 @@ public function __unset($name)
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
} else {
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}

// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}

throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}

Expand Down Expand Up @@ -503,19 +506,19 @@ public function off($name, $handler = null)
if ($handler === null) {
unset($this->_events[$name]);
return true;
} else {
$removed = false;
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
}

$removed = false;
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
return $removed;
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
}
return $removed;
}

/**
Expand Down Expand Up @@ -621,9 +624,9 @@ public function detachBehavior($name)
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
} else {
return null;
}

return null;
}

/**
Expand Down Expand Up @@ -666,13 +669,14 @@ private function attachBehaviorInternal($name, $behavior)
if (is_int($name)) {
$behavior->attach($this);
$this->_behaviors[] = $behavior;
} else {
if (isset($this->_behaviors[$name])) {
$this->_behaviors[$name]->detach();
}
$behavior->attach($this);
$this->_behaviors[$name] = $behavior;
}

if (isset($this->_behaviors[$name])) {
$this->_behaviors[$name]->detach();
}
$behavior->attach($this);
$this->_behaviors[$name] = $behavior;

return $behavior;
}
}
6 changes: 2 additions & 4 deletions framework/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ public function run($route, $params = [])
return $this->runAction($route, $params);
} elseif ($pos > 0) {
return $this->module->runAction($route, $params);
} else {
return Yii::$app->runAction(ltrim($route, '/'), $params);
}
return Yii::$app->runAction(ltrim($route, '/'), $params);
}

/**
Expand Down Expand Up @@ -393,9 +392,8 @@ public function renderContent($content)
$layoutFile = $this->findLayoutFile($this->getView());
if ($layoutFile !== false) {
return $this->getView()->renderFile($layoutFile, ['content' => $content], $this);
} else {
return $content;
}
return $content;
}

/**
Expand Down
23 changes: 11 additions & 12 deletions framework/base/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,19 @@ public static function off($class, $name, $handler = null)
if ($handler === null) {
unset(self::$_events[$name][$class]);
return true;
} else {
$removed = false;
foreach (self::$_events[$name][$class] as $i => $event) {
if ($event[0] === $handler) {
unset(self::$_events[$name][$class][$i]);
$removed = true;
}
}
if ($removed) {
self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
}
}

return $removed;
$removed = false;
foreach (self::$_events[$name][$class] as $i => $event) {
if ($event[0] === $handler) {
unset(self::$_events[$name][$class][$i]);
$removed = true;
}
}
if ($removed) {
self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
}
return $removed;
}

/**
Expand Down
21 changes: 9 additions & 12 deletions framework/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,8 @@ public function getErrors($attribute = null)
{
if ($attribute === null) {
return $this->_errors === null ? [] : $this->_errors;
} else {
return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : [];
}
return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : [];
}

/**
Expand All @@ -578,16 +577,15 @@ public function getFirstErrors()
{
if (empty($this->_errors)) {
return [];
} else {
$errors = [];
foreach ($this->_errors as $name => $es) {
if (!empty($es)) {
$errors[$name] = reset($es);
}
}
}

return $errors;
$errors = [];
foreach ($this->_errors as $name => $es) {
if (!empty($es)) {
$errors[$name] = reset($es);
}
}
return $errors;
}

/**
Expand Down Expand Up @@ -829,9 +827,8 @@ public function load($data, $formName = null)
$this->setAttributes($data[$scope]);

return true;
} else {
return false;
}
return false;
}

/**
Expand Down
Loading

0 comments on commit 954c771

Please sign in to comment.