Browse Source

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
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
e775c82dd8
  1. 4
      apps/basic/models/User.php
  2. 2
      extensions/jui/Widget.php
  3. 64
      framework/yii/BaseYii.php
  4. 6
      framework/yii/helpers/BaseInflector.php
  5. 2
      framework/yii/web/Response.php
  6. 6
      tests/unit/TestCase.php

4
apps/basic/models/User.php

@ -26,14 +26,14 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface
public static function findIdentity($id) 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) public static function findByUsername($username)
{ {
foreach (self::$users as $user) { foreach (self::$users as $user) {
if (strcasecmp($user['username'], $username) === 0) { if (strcasecmp($user['username'], $username) === 0) {
return new self($user); return new static($user);
} }
} }
return null; return null;

2
extensions/jui/Widget.php

@ -66,7 +66,7 @@ class Widget extends \yii\base\Widget
/** @var \yii\web\AssetBundle $assetBundle */ /** @var \yii\web\AssetBundle $assetBundle */
$assetBundle::register($view); $assetBundle::register($view);
/** @var \yii\web\AssetBundle $themeAsset */ /** @var \yii\web\AssetBundle $themeAsset */
$themeAsset = self::$theme; $themeAsset = static::$theme;
$themeAsset::register($view); $themeAsset::register($view);
$id = $this->options['id']; $id = $this->options['id'];

64
framework/yii/BaseYii.php

@ -147,11 +147,11 @@ class BaseYii
$pos = strpos($alias, '/'); $pos = strpos($alias, '/');
$root = $pos === false ? $alias : substr($alias, 0, $pos); $root = $pos === false ? $alias : substr($alias, 0, $pos);
if (isset(self::$aliases[$root])) { if (isset(static::$aliases[$root])) {
if (is_string(self::$aliases[$root])) { if (is_string(static::$aliases[$root])) {
return $pos === false ? self::$aliases[$root] : self::$aliases[$root] . substr($alias, $pos); return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
} else { } else {
foreach (self::$aliases[$root] as $name => $path) { foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) { if (strpos($alias . '/', $name . '/') === 0) {
return $path . substr($alias, strlen($name)); return $path . substr($alias, strlen($name));
} }
@ -178,11 +178,11 @@ class BaseYii
$pos = strpos($alias, '/'); $pos = strpos($alias, '/');
$root = $pos === false ? $alias : substr($alias, 0, $pos); $root = $pos === false ? $alias : substr($alias, 0, $pos);
if (isset(self::$aliases[$root])) { if (isset(static::$aliases[$root])) {
if (is_string(self::$aliases[$root])) { if (is_string(static::$aliases[$root])) {
return $root; return $root;
} else { } else {
foreach (self::$aliases[$root] as $name => $path) { foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) { if (strpos($alias . '/', $name . '/') === 0) {
return $name; return $name;
} }
@ -229,30 +229,30 @@ class BaseYii
$root = $pos === false ? $alias : substr($alias, 0, $pos); $root = $pos === false ? $alias : substr($alias, 0, $pos);
if ($path !== null) { if ($path !== null) {
$path = strncmp($path, '@', 1) ? rtrim($path, '\\/') : static::getAlias($path); $path = strncmp($path, '@', 1) ? rtrim($path, '\\/') : static::getAlias($path);
if (!isset(self::$aliases[$root])) { if (!isset(static::$aliases[$root])) {
if ($pos === false) { if ($pos === false) {
self::$aliases[$root] = $path; static::$aliases[$root] = $path;
} else { } 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) { if ($pos === false) {
self::$aliases[$root] = $path; static::$aliases[$root] = $path;
} else { } else {
self::$aliases[$root] = [ static::$aliases[$root] = [
$alias => $path, $alias => $path,
$root => self::$aliases[$root], $root => static::$aliases[$root],
]; ];
} }
} else { } else {
self::$aliases[$root][$alias] = $path; static::$aliases[$root][$alias] = $path;
krsort(self::$aliases[$root]); krsort(static::$aliases[$root]);
} }
} elseif (isset(self::$aliases[$root])) { } elseif (isset(static::$aliases[$root])) {
if (is_array(self::$aliases[$root])) { if (is_array(static::$aliases[$root])) {
unset(self::$aliases[$root][$alias]); unset(static::$aliases[$root][$alias]);
} elseif ($pos === false) { } elseif ($pos === false) {
unset(self::$aliases[$root]); unset(static::$aliases[$root]);
} }
} }
} }
@ -278,8 +278,8 @@ class BaseYii
*/ */
public static function autoload($className) public static function autoload($className)
{ {
if (isset(self::$classMap[$className])) { if (isset(static::$classMap[$className])) {
$classFile = self::$classMap[$className]; $classFile = static::$classMap[$className];
if ($classFile[0] === '@') { if ($classFile[0] === '@') {
$classFile = static::getAlias($classFile); $classFile = static::getAlias($classFile);
} }
@ -362,8 +362,8 @@ class BaseYii
$class = ltrim($class, '\\'); $class = ltrim($class, '\\');
if (isset(self::$objectConfig[$class])) { if (isset(static::$objectConfig[$class])) {
$config = array_merge(self::$objectConfig[$class], $config); $config = array_merge(static::$objectConfig[$class], $config);
} }
if (($n = func_num_args()) > 1) { if (($n = func_num_args()) > 1) {
@ -394,7 +394,7 @@ class BaseYii
public static function trace($message, $category = 'application') public static function trace($message, $category = 'application')
{ {
if (YII_DEBUG) { 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') 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') 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') 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') 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') 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) public static function t($category, $message, $params = [], $language = null)
{ {
if (self::$app !== null) { if (static::$app !== null) {
return self::$app->getI18n()->translate($category, $message, $params, $language ?: self::$app->language); return static::$app->getI18n()->translate($category, $message, $params, $language ?: static::$app->language);
} else { } else {
$p = []; $p = [];
foreach ((array) $params as $name => $value) { foreach ((array) $params as $name => $value) {

6
framework/yii/helpers/BaseInflector.php

@ -280,8 +280,8 @@ class BaseInflector
*/ */
public static function pluralize($word) public static function pluralize($word)
{ {
if (isset(self::$specials[$word])) { if (isset(static::$specials[$word])) {
return self::$specials[$word]; return static::$specials[$word];
} }
foreach (static::$plurals as $rule => $replacement) { foreach (static::$plurals as $rule => $replacement) {
if (preg_match($rule, $word)) { if (preg_match($rule, $word)) {
@ -298,7 +298,7 @@ class BaseInflector
*/ */
public static function singularize($word) public static function singularize($word)
{ {
$result = array_search($word, self::$specials, true); $result = array_search($word, static::$specials, true);
if ($result !== false) { if ($result !== false) {
return $result; return $result;
} }

2
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"); throw new InvalidParamException("The HTTP status code is invalid: $value");
} }
if ($text === null) { 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 { } else {
$this->statusText = $text; $this->statusText = $text;
} }

6
tests/unit/TestCase.php

@ -32,10 +32,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*/ */
public function getParam($name, $default = null) public function getParam($name, $default = null)
{ {
if (self::$params === null) { if (static::$params === null) {
self::$params = require(__DIR__ . '/data/config.php'); 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;
} }
/** /**

Loading…
Cancel
Save