From e775c82dd8ec73a5a30cb1ee31733694ac8bdc60 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 20 Nov 2013 18:36:05 +0100 Subject: [PATCH] Changed usage of self to static to allow extendibility code style section has been added here: https://github.com/yiisoft/yii2/wiki/Core-framework-code-style#self-vs-static verification code for properties: https://gist.github.com/cebe/7566916 --- apps/basic/models/User.php | 4 +-- extensions/jui/Widget.php | 2 +- framework/yii/BaseYii.php | 64 ++++++++++++++++----------------- framework/yii/helpers/BaseInflector.php | 6 ++-- framework/yii/web/Response.php | 2 +- tests/unit/TestCase.php | 6 ++-- 6 files changed, 42 insertions(+), 42 deletions(-) diff --git a/apps/basic/models/User.php b/apps/basic/models/User.php index 085cda2..af4c42e 100644 --- a/apps/basic/models/User.php +++ b/apps/basic/models/User.php @@ -26,14 +26,14 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface public static function findIdentity($id) { - return isset(self::$users[$id]) ? new self(self::$users[$id]) : null; + return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; } public static function findByUsername($username) { foreach (self::$users as $user) { if (strcasecmp($user['username'], $username) === 0) { - return new self($user); + return new static($user); } } return null; diff --git a/extensions/jui/Widget.php b/extensions/jui/Widget.php index 2bbc9e4..8833fbe 100644 --- a/extensions/jui/Widget.php +++ b/extensions/jui/Widget.php @@ -66,7 +66,7 @@ class Widget extends \yii\base\Widget /** @var \yii\web\AssetBundle $assetBundle */ $assetBundle::register($view); /** @var \yii\web\AssetBundle $themeAsset */ - $themeAsset = self::$theme; + $themeAsset = static::$theme; $themeAsset::register($view); $id = $this->options['id']; diff --git a/framework/yii/BaseYii.php b/framework/yii/BaseYii.php index f11df7f..ee49c8e 100644 --- a/framework/yii/BaseYii.php +++ b/framework/yii/BaseYii.php @@ -147,11 +147,11 @@ class BaseYii $pos = strpos($alias, '/'); $root = $pos === false ? $alias : substr($alias, 0, $pos); - if (isset(self::$aliases[$root])) { - if (is_string(self::$aliases[$root])) { - return $pos === false ? self::$aliases[$root] : self::$aliases[$root] . substr($alias, $pos); + 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 (self::$aliases[$root] as $name => $path) { + foreach (static::$aliases[$root] as $name => $path) { if (strpos($alias . '/', $name . '/') === 0) { return $path . substr($alias, strlen($name)); } @@ -178,11 +178,11 @@ class BaseYii $pos = strpos($alias, '/'); $root = $pos === false ? $alias : substr($alias, 0, $pos); - if (isset(self::$aliases[$root])) { - if (is_string(self::$aliases[$root])) { + if (isset(static::$aliases[$root])) { + if (is_string(static::$aliases[$root])) { return $root; } else { - foreach (self::$aliases[$root] as $name => $path) { + foreach (static::$aliases[$root] as $name => $path) { if (strpos($alias . '/', $name . '/') === 0) { return $name; } @@ -229,30 +229,30 @@ class BaseYii $root = $pos === false ? $alias : substr($alias, 0, $pos); if ($path !== null) { $path = strncmp($path, '@', 1) ? rtrim($path, '\\/') : static::getAlias($path); - if (!isset(self::$aliases[$root])) { + if (!isset(static::$aliases[$root])) { if ($pos === false) { - self::$aliases[$root] = $path; + static::$aliases[$root] = $path; } else { - self::$aliases[$root] = [$alias => $path]; + static::$aliases[$root] = [$alias => $path]; } - } elseif (is_string(self::$aliases[$root])) { + } elseif (is_string(static::$aliases[$root])) { if ($pos === false) { - self::$aliases[$root] = $path; + static::$aliases[$root] = $path; } else { - self::$aliases[$root] = [ + static::$aliases[$root] = [ $alias => $path, - $root => self::$aliases[$root], + $root => static::$aliases[$root], ]; } } else { - self::$aliases[$root][$alias] = $path; - krsort(self::$aliases[$root]); + static::$aliases[$root][$alias] = $path; + krsort(static::$aliases[$root]); } - } elseif (isset(self::$aliases[$root])) { - if (is_array(self::$aliases[$root])) { - unset(self::$aliases[$root][$alias]); + } elseif (isset(static::$aliases[$root])) { + if (is_array(static::$aliases[$root])) { + unset(static::$aliases[$root][$alias]); } elseif ($pos === false) { - unset(self::$aliases[$root]); + unset(static::$aliases[$root]); } } } @@ -278,8 +278,8 @@ class BaseYii */ public static function autoload($className) { - if (isset(self::$classMap[$className])) { - $classFile = self::$classMap[$className]; + if (isset(static::$classMap[$className])) { + $classFile = static::$classMap[$className]; if ($classFile[0] === '@') { $classFile = static::getAlias($classFile); } @@ -362,8 +362,8 @@ class BaseYii $class = ltrim($class, '\\'); - if (isset(self::$objectConfig[$class])) { - $config = array_merge(self::$objectConfig[$class], $config); + if (isset(static::$objectConfig[$class])) { + $config = array_merge(static::$objectConfig[$class], $config); } if (($n = func_num_args()) > 1) { @@ -394,7 +394,7 @@ class BaseYii public static function trace($message, $category = 'application') { if (YII_DEBUG) { - self::$app->getLog()->log($message, Logger::LEVEL_TRACE, $category); + static::$app->getLog()->log($message, Logger::LEVEL_TRACE, $category); } } @@ -407,7 +407,7 @@ class BaseYii */ public static function error($message, $category = 'application') { - self::$app->getLog()->log($message, Logger::LEVEL_ERROR, $category); + static::$app->getLog()->log($message, Logger::LEVEL_ERROR, $category); } /** @@ -419,7 +419,7 @@ class BaseYii */ public static function warning($message, $category = 'application') { - self::$app->getLog()->log($message, Logger::LEVEL_WARNING, $category); + static::$app->getLog()->log($message, Logger::LEVEL_WARNING, $category); } /** @@ -431,7 +431,7 @@ class BaseYii */ public static function info($message, $category = 'application') { - self::$app->getLog()->log($message, Logger::LEVEL_INFO, $category); + static::$app->getLog()->log($message, Logger::LEVEL_INFO, $category); } /** @@ -453,7 +453,7 @@ class BaseYii */ public static function beginProfile($token, $category = 'application') { - self::$app->getLog()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category); + static::$app->getLog()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category); } /** @@ -465,7 +465,7 @@ class BaseYii */ public static function endProfile($token, $category = 'application') { - self::$app->getLog()->log($token, Logger::LEVEL_PROFILE_END, $category); + static::$app->getLog()->log($token, Logger::LEVEL_PROFILE_END, $category); } /** @@ -504,8 +504,8 @@ class BaseYii */ public static function t($category, $message, $params = [], $language = null) { - if (self::$app !== null) { - return self::$app->getI18n()->translate($category, $message, $params, $language ?: self::$app->language); + if (static::$app !== null) { + return static::$app->getI18n()->translate($category, $message, $params, $language ?: static::$app->language); } else { $p = []; foreach ((array) $params as $name => $value) { diff --git a/framework/yii/helpers/BaseInflector.php b/framework/yii/helpers/BaseInflector.php index deb8239..2f4f01b 100644 --- a/framework/yii/helpers/BaseInflector.php +++ b/framework/yii/helpers/BaseInflector.php @@ -280,8 +280,8 @@ class BaseInflector */ public static function pluralize($word) { - if (isset(self::$specials[$word])) { - return self::$specials[$word]; + if (isset(static::$specials[$word])) { + return static::$specials[$word]; } foreach (static::$plurals as $rule => $replacement) { if (preg_match($rule, $word)) { @@ -298,7 +298,7 @@ class BaseInflector */ public static function singularize($word) { - $result = array_search($word, self::$specials, true); + $result = array_search($word, static::$specials, true); if ($result !== false) { return $result; } diff --git a/framework/yii/web/Response.php b/framework/yii/web/Response.php index 1d77d14..bebd792 100644 --- a/framework/yii/web/Response.php +++ b/framework/yii/web/Response.php @@ -254,7 +254,7 @@ class Response extends \yii\base\Response throw new InvalidParamException("The HTTP status code is invalid: $value"); } if ($text === null) { - $this->statusText = isset(self::$httpStatuses[$this->_statusCode]) ? self::$httpStatuses[$this->_statusCode] : ''; + $this->statusText = isset(static::$httpStatuses[$this->_statusCode]) ? static::$httpStatuses[$this->_statusCode] : ''; } else { $this->statusText = $text; } diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index e83605e..c8e5731 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -32,10 +32,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase */ public function getParam($name, $default = null) { - if (self::$params === null) { - self::$params = require(__DIR__ . '/data/config.php'); + if (static::$params === null) { + static::$params = require(__DIR__ . '/data/config.php'); } - return isset(self::$params[$name]) ? self::$params[$name] : $default; + return isset(static::$params[$name]) ? static::$params[$name] : $default; } /**