Commit bb855088 by Alexander Mohorev

PHP type casting

parent bfb62167
...@@ -269,14 +269,14 @@ Use the following formatting for switch: ...@@ -269,14 +269,14 @@ Use the following formatting for switch:
```php ```php
switch ($this->phpType) { switch ($this->phpType) {
case 'string': case 'string':
$a = (string)$value; $a = (string) $value;
break; break;
case 'integer': case 'integer':
case 'int': case 'int':
$a = (integer)$value; $a = (int) $value;
break; break;
case 'boolean': case 'boolean':
$a = (boolean)$value; $a = (bool) $value;
break; break;
default: default:
$a = null; $a = null;
......
...@@ -309,7 +309,7 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -309,7 +309,7 @@ class ActiveField extends \yii\widgets\ActiveField
*/ */
public function inline($value = true) public function inline($value = true)
{ {
$this->inline = (bool)$value; $this->inline = (bool) $value;
return $this; return $this;
} }
......
...@@ -139,7 +139,7 @@ class LogTarget extends Target ...@@ -139,7 +139,7 @@ class LogTarget extends Target
$summary = [ $summary = [
'tag' => $this->tag, 'tag' => $this->tag,
'url' => $request->getAbsoluteUrl(), 'url' => $request->getAbsoluteUrl(),
'ajax' => (int)$request->getIsAjax(), 'ajax' => (int) $request->getIsAjax(),
'method' => $request->getMethod(), 'method' => $request->getMethod(),
'ip' => $request->getUserIP(), 'ip' => $request->getUserIP(),
'time' => time(), 'time' => time(),
......
...@@ -102,7 +102,7 @@ class GenerateAction extends \yii\base\Action ...@@ -102,7 +102,7 @@ class GenerateAction extends \yii\base\Action
return; return;
} }
if ($this->generator->save($files, (array)$answers, $results)) { if ($this->generator->save($files, (array) $answers, $results)) {
$this->controller->stdout("\nFiles were generated successfully!\n", Console::FG_GREEN); $this->controller->stdout("\nFiles were generated successfully!\n", Console::FG_GREEN);
} else { } else {
$this->controller->stdout("\nSome errors occurred while generating the files.", Console::FG_RED); $this->controller->stdout("\nSome errors occurred while generating the files.", Console::FG_RED);
......
...@@ -89,7 +89,7 @@ abstract class Migration extends Component implements MigrationInterface ...@@ -89,7 +89,7 @@ abstract class Migration extends Component implements MigrationInterface
*/ */
public function createIndex($collection, $columns, $options = []) public function createIndex($collection, $columns, $options = [])
{ {
echo " > create index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array)$columns) . empty($options) ? "" : ", " . Json::encode($options) . ") ..."; echo " > create index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . empty($options) ? "" : ", " . Json::encode($options) . ") ...";
$time = microtime(true); $time = microtime(true);
$this->db->getCollection($collection)->createIndex($columns, $options); $this->db->getCollection($collection)->createIndex($columns, $options);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n"; echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
...@@ -102,7 +102,7 @@ abstract class Migration extends Component implements MigrationInterface ...@@ -102,7 +102,7 @@ abstract class Migration extends Component implements MigrationInterface
*/ */
public function dropIndex($collection, $columns) public function dropIndex($collection, $columns)
{ {
echo " > drop index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array)$columns) . ") ..."; echo " > drop index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . ") ...";
$time = microtime(true); $time = microtime(true);
$this->db->getCollection($collection)->dropIndex($columns); $this->db->getCollection($collection)->dropIndex($columns);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n"; echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
......
...@@ -81,7 +81,7 @@ To get actual Mongo ID string your should typecast [[\MongoId]] instance to stri ...@@ -81,7 +81,7 @@ To get actual Mongo ID string your should typecast [[\MongoId]] instance to stri
$query = new Query; $query = new Query;
$row = $query->from('customer')->one(); $row = $query->from('customer')->one();
var_dump($row['_id']); // outputs: "object(MongoId)" var_dump($row['_id']); // outputs: "object(MongoId)"
var_dump((string)$row['_id']); // outputs "string 'acdfgdacdhcbdafa'" var_dump((string) $row['_id']); // outputs "string 'acdfgdacdhcbdafa'"
``` ```
Although this fact is very useful sometimes, it often produces some problems. Although this fact is very useful sometimes, it often produces some problems.
...@@ -90,7 +90,7 @@ In these cases, ensure you have converted [[\MongoId]] into the string: ...@@ -90,7 +90,7 @@ In these cases, ensure you have converted [[\MongoId]] into the string:
```php ```php
/* @var $this yii\web\View */ /* @var $this yii\web\View */
echo $this->createUrl(['item/update', 'id' => (string)$row['_id']]); echo $this->createUrl(['item/update', 'id' => (string) $row['_id']]);
``` ```
While building condition, values for the key '_id' will be automatically cast to [[\MongoId]] instance, While building condition, values for the key '_id' will be automatically cast to [[\MongoId]] instance,
......
...@@ -124,7 +124,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -124,7 +124,7 @@ class ActiveRecord extends BaseActiveRecord
// only insert attributes that are not null // only insert attributes that are not null
if ($value !== null) { if ($value !== null) {
if (is_bool($value)) { if (is_bool($value)) {
$value = (int)$value; $value = (int) $value;
} }
$setArgs[] = $attribute; $setArgs[] = $attribute;
$setArgs[] = $value; $setArgs[] = $value;
...@@ -175,7 +175,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -175,7 +175,7 @@ class ActiveRecord extends BaseActiveRecord
} }
if ($value !== null) { if ($value !== null) {
if (is_bool($value)) { if (is_bool($value)) {
$value = (int)$value; $value = (int) $value;
} }
$setArgs[] = $attribute; $setArgs[] = $attribute;
$setArgs[] = $value; $setArgs[] = $value;
......
...@@ -270,7 +270,7 @@ EOF; ...@@ -270,7 +270,7 @@ EOF;
$parts[] = $this->buildInCondition('in', [$column, $value], $columns); $parts[] = $this->buildInCondition('in', [$column, $value], $columns);
} else { } else {
if (is_bool($value)) { if (is_bool($value)) {
$value = (int)$value; $value = (int) $value;
} }
if ($value === null) { if ($value === null) {
$parts[] = "redis.call('HEXISTS',key .. ':a:' .. pk, ".$this->quoteValue($column).")==0"; $parts[] = "redis.call('HEXISTS',key .. ':a:' .. pk, ".$this->quoteValue($column).")==0";
......
...@@ -73,9 +73,9 @@ class ColumnSchema extends Object ...@@ -73,9 +73,9 @@ class ColumnSchema extends Object
case 'string': case 'string':
return is_resource($value) ? $value : (string) $value; return is_resource($value) ? $value : (string) $value;
case 'integer': case 'integer':
return (integer) $value; return (int) $value;
case 'boolean': case 'boolean':
return (boolean) $value; return (bool) $value;
case 'double': case 'double':
return (double) $value; return (double) $value;
} }
......
...@@ -822,7 +822,7 @@ class QueryBuilder extends Object ...@@ -822,7 +822,7 @@ class QueryBuilder extends Object
if ($values instanceof Query) { if ($values instanceof Query) {
// sub-query // sub-query
list($sql, $params) = $this->build($values, $params); list($sql, $params) = $this->build($values, $params);
$column = (array)$column; $column = (array) $column;
if (is_array($column)) { if (is_array($column)) {
foreach ($column as $i => $col) { foreach ($column as $i => $col) {
if (strpos($col, '(') === false) { if (strpos($col, '(') === false) {
......
...@@ -159,7 +159,7 @@ class Extension extends \Twig_Extension ...@@ -159,7 +159,7 @@ class Extension extends \Twig_Extension
public function addUses($args) public function addUses($args)
{ {
foreach ((array)$args as $key => $value) { foreach ((array) $args as $key => $value) {
$value = str_replace('/', '\\', $value); $value = str_replace('/', '\\', $value);
if (is_int($key)) { if (is_int($key)) {
// namespace or class import // namespace or class import
......
...@@ -593,7 +593,7 @@ class Security extends Component ...@@ -593,7 +593,7 @@ class Security extends Component
*/ */
protected function generateSalt($cost = 13) protected function generateSalt($cost = 13)
{ {
$cost = (int)$cost; $cost = (int) $cost;
if ($cost < 4 || $cost > 31) { if ($cost < 4 || $cost > 31) {
throw new InvalidParamException('Cost must be between 4 and 31.'); throw new InvalidParamException('Cost must be between 4 and 31.');
} }
......
...@@ -128,7 +128,7 @@ class SluggableBehavior extends AttributeBehavior ...@@ -128,7 +128,7 @@ class SluggableBehavior extends AttributeBehavior
$isNewSlug = true; $isNewSlug = true;
if ($this->attribute !== null) { if ($this->attribute !== null) {
$attributes = (array)$this->attribute; $attributes = (array) $this->attribute;
/* @var $owner BaseActiveRecord */ /* @var $owner BaseActiveRecord */
$owner = $this->owner; $owner = $this->owner;
if (!$owner->getIsNewRecord() && !empty($owner->{$this->slugAttribute})) { if (!$owner->getIsNewRecord() && !empty($owner->{$this->slugAttribute})) {
......
...@@ -65,7 +65,7 @@ class TagDependency extends Dependency ...@@ -65,7 +65,7 @@ class TagDependency extends Dependency
public static function invalidate($cache, $tags) public static function invalidate($cache, $tags)
{ {
$keys = []; $keys = [];
foreach ((array)$tags as $tag) { foreach ((array) $tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]); $keys[] = $cache->buildKey([__CLASS__, $tag]);
} }
static::touchKeys($cache, $keys); static::touchKeys($cache, $keys);
......
...@@ -152,7 +152,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -152,7 +152,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} }
if (empty($this->select) && !empty($this->join)) { if (empty($this->select) && !empty($this->join)) {
foreach ((array)$this->from as $alias => $table) { foreach ((array) $this->from as $alias => $table) {
if (is_string($alias)) { if (is_string($alias)) {
$this->select = ["$alias.*"]; $this->select = ["$alias.*"];
} elseif (is_string($table)) { } elseif (is_string($table)) {
......
...@@ -98,9 +98,9 @@ class ColumnSchema extends Object ...@@ -98,9 +98,9 @@ class ColumnSchema extends Object
case 'string': case 'string':
return is_resource($value) ? $value : (string) $value; return is_resource($value) ? $value : (string) $value;
case 'integer': case 'integer':
return (integer) $value; return (int) $value;
case 'boolean': case 'boolean':
return (boolean) $value; return (bool) $value;
case 'double': case 'double':
return (double) $value; return (double) $value;
} }
......
...@@ -531,7 +531,7 @@ class Connection extends Component ...@@ -531,7 +531,7 @@ class Connection extends Component
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
} catch (\PDOException $e) { } catch (\PDOException $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->errorInfo, (int)$e->getCode(), $e); throw new Exception($e->getMessage(), $e->errorInfo, (int) $e->getCode(), $e);
} }
} }
......
...@@ -680,7 +680,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -680,7 +680,7 @@ class QueryBuilder extends \yii\base\Object
} }
// 0:join type, 1:join table, 2:on-condition (optional) // 0:join type, 1:join table, 2:on-condition (optional)
list ($joinType, $table) = $join; list ($joinType, $table) = $join;
$tables = $this->quoteTableNames((array)$table, $params); $tables = $this->quoteTableNames((array) $table, $params);
$table = reset($tables); $table = reset($tables);
$joins[$i] = "$joinType $table"; $joins[$i] = "$joinType $table";
if (isset($join[2])) { if (isset($join[2])) {
...@@ -1055,7 +1055,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -1055,7 +1055,7 @@ class QueryBuilder extends \yii\base\Object
if ($values instanceof Query) { if ($values instanceof Query) {
// sub-query // sub-query
list($sql, $params) = $this->build($values, $params); list($sql, $params) = $this->build($values, $params);
$column = (array)$column; $column = (array) $column;
if (is_array($column)) { if (is_array($column)) {
foreach ($column as $i => $col) { foreach ($column as $i => $col) {
if (strpos($col, '(') === false) { if (strpos($col, '(') === false) {
......
...@@ -425,7 +425,7 @@ SQL; ...@@ -425,7 +425,7 @@ SQL;
$column->name = $info['column_name']; $column->name = $info['column_name'];
$column->precision = $info['numeric_precision']; $column->precision = $info['numeric_precision'];
$column->scale = $info['numeric_scale']; $column->scale = $info['numeric_scale'];
$column->size = $info['size'] === null ? null : (int)$info['size']; $column->size = $info['size'] === null ? null : (int) $info['size'];
if (isset($this->typeMap[$column->dbType])) { if (isset($this->typeMap[$column->dbType])) {
$column->type = $this->typeMap[$column->dbType]; $column->type = $this->typeMap[$column->dbType];
} else { } else {
......
...@@ -150,7 +150,7 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -150,7 +150,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/ */
public function checkIntegrity($check = true, $schema = '', $table = '') public function checkIntegrity($check = true, $schema = '', $table = '')
{ {
return 'PRAGMA foreign_keys='.(int)$check; return 'PRAGMA foreign_keys='.(int) $check;
} }
/** /**
......
...@@ -459,7 +459,7 @@ class GridView extends BaseListView ...@@ -459,7 +459,7 @@ class GridView extends BaseListView
} else { } else {
$options = $this->rowOptions; $options = $this->rowOptions;
} }
$options['data-key'] = is_array($key) ? json_encode($key) : (string)$key; $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
return Html::tag('tr', implode('', $cells), $options); return Html::tag('tr', implode('', $cells), $options);
} }
......
...@@ -636,7 +636,7 @@ class BaseHtml ...@@ -636,7 +636,7 @@ class BaseHtml
*/ */
public static function radio($name, $checked = false, $options = []) public static function radio($name, $checked = false, $options = [])
{ {
$options['checked'] = (boolean) $checked; $options['checked'] = (bool) $checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1'; $value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) { if (isset($options['uncheck'])) {
// add a hidden field so that if the radio button is not selected, it still submits a value // add a hidden field so that if the radio button is not selected, it still submits a value
...@@ -678,7 +678,7 @@ class BaseHtml ...@@ -678,7 +678,7 @@ class BaseHtml
*/ */
public static function checkbox($name, $checked = false, $options = []) public static function checkbox($name, $checked = false, $options = [])
{ {
$options['checked'] = (boolean) $checked; $options['checked'] = (bool) $checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1'; $value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) { if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value // add a hidden field so that if the checkbox is not selected, it still submits a value
......
...@@ -81,7 +81,7 @@ class BaseUrl ...@@ -81,7 +81,7 @@ class BaseUrl
*/ */
public static function toRoute($route, $scheme = false) public static function toRoute($route, $scheme = false)
{ {
$route = (array)$route; $route = (array) $route;
$route[0] = static::normalizeRoute($route[0]); $route[0] = static::normalizeRoute($route[0]);
if ($scheme) { if ($scheme) {
......
...@@ -57,7 +57,7 @@ class MysqlMutex extends DbMutex ...@@ -57,7 +57,7 @@ class MysqlMutex extends DbMutex
*/ */
protected function acquireLock($name, $timeout = 0) protected function acquireLock($name, $timeout = 0)
{ {
return (boolean) $this->db return (bool) $this->db
->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout]) ->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
->queryScalar(); ->queryScalar();
} }
...@@ -70,7 +70,7 @@ class MysqlMutex extends DbMutex ...@@ -70,7 +70,7 @@ class MysqlMutex extends DbMutex
*/ */
protected function releaseLock($name) protected function releaseLock($name)
{ {
return (boolean) $this->db return (bool) $this->db
->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name]) ->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
->queryScalar(); ->queryScalar();
} }
......
...@@ -349,7 +349,7 @@ class DbManager extends BaseManager ...@@ -349,7 +349,7 @@ class DbManager extends BaseManager
$query = (new Query)->select('b.*') $query = (new Query)->select('b.*')
->from(['a' => $this->assignmentTable, 'b' => $this->itemTable]) ->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])
->where('a.item_name=b.name') ->where('a.item_name=b.name')
->andWhere(['a.user_id' => (string)$userId]); ->andWhere(['a.user_id' => (string) $userId]);
$roles = []; $roles = [];
foreach ($query->all($this->db) as $row) { foreach ($query->all($this->db) as $row) {
...@@ -391,7 +391,7 @@ class DbManager extends BaseManager ...@@ -391,7 +391,7 @@ class DbManager extends BaseManager
$query = (new Query)->select('item_name') $query = (new Query)->select('item_name')
->from($this->assignmentTable) ->from($this->assignmentTable)
->where(['user_id' => (string)$userId]); ->where(['user_id' => (string) $userId]);
$childrenList = $this->getChildrenList(); $childrenList = $this->getChildrenList();
$result = []; $result = [];
...@@ -482,7 +482,7 @@ class DbManager extends BaseManager ...@@ -482,7 +482,7 @@ class DbManager extends BaseManager
} }
$row = (new Query)->from($this->assignmentTable) $row = (new Query)->from($this->assignmentTable)
->where(['user_id' => (string)$userId, 'item_name' => $roleName]) ->where(['user_id' => (string) $userId, 'item_name' => $roleName])
->one($this->db); ->one($this->db);
if ($row === false) { if ($row === false) {
...@@ -507,7 +507,7 @@ class DbManager extends BaseManager ...@@ -507,7 +507,7 @@ class DbManager extends BaseManager
$query = (new Query) $query = (new Query)
->from($this->assignmentTable) ->from($this->assignmentTable)
->where(['user_id' => (string)$userId]); ->where(['user_id' => (string) $userId]);
$assignments = []; $assignments = [];
foreach ($query->all($this->db) as $row) { foreach ($query->all($this->db) as $row) {
...@@ -644,7 +644,7 @@ class DbManager extends BaseManager ...@@ -644,7 +644,7 @@ class DbManager extends BaseManager
} }
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->assignmentTable, ['user_id' => (string)$userId, 'item_name' => $role->name]) ->delete($this->assignmentTable, ['user_id' => (string) $userId, 'item_name' => $role->name])
->execute() > 0; ->execute() > 0;
} }
...@@ -658,7 +658,7 @@ class DbManager extends BaseManager ...@@ -658,7 +658,7 @@ class DbManager extends BaseManager
} }
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->assignmentTable, ['user_id' => (string)$userId]) ->delete($this->assignmentTable, ['user_id' => (string) $userId])
->execute() > 0; ->execute() > 0;
} }
......
...@@ -199,7 +199,7 @@ class YiiRequirementChecker ...@@ -199,7 +199,7 @@ class YiiRequirementChecker
return false; return false;
} }
return ((integer) $value == 1 || strtolower($value) == 'on'); return ((int) $value == 1 || strtolower($value) == 'on');
} }
/** /**
...@@ -244,7 +244,7 @@ class YiiRequirementChecker ...@@ -244,7 +244,7 @@ class YiiRequirementChecker
return 0; return 0;
} }
if (is_numeric($verboseSize)) { if (is_numeric($verboseSize)) {
return (integer) $verboseSize; return (int) $verboseSize;
} }
$sizeUnit = trim($verboseSize, '0123456789'); $sizeUnit = trim($verboseSize, '0123456789');
$size = str_replace($sizeUnit, '', $verboseSize); $size = str_replace($sizeUnit, '', $verboseSize);
......
...@@ -189,7 +189,7 @@ class CommandTest extends DatabaseTestCase ...@@ -189,7 +189,7 @@ class CommandTest extends DatabaseTestCase
} }
$this->assertEquals($numericCol, $row['numeric_col']); $this->assertEquals($numericCol, $row['numeric_col']);
if ($this->driverName === 'mysql' || defined('HHVM_VERSION') && $this->driverName === 'sqlite') { if ($this->driverName === 'mysql' || defined('HHVM_VERSION') && $this->driverName === 'sqlite') {
$this->assertEquals($boolCol, (int)$row['bool_col']); $this->assertEquals($boolCol, (int) $row['bool_col']);
} else { } else {
$this->assertEquals($boolCol, $row['bool_col']); $this->assertEquals($boolCol, $row['bool_col']);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment