Skip to content

Commit

Permalink
Merge pull request #4570 from cakephp/master-casting
Browse files Browse the repository at this point in the history
Use (int) instead of intval() for performance reasons and to unify it.
  • Loading branch information
markstory committed Sep 10, 2014
2 parents 58bf005 + e3dc51b commit 282bc0c
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Cache/Engine/ApcEngine.php
Expand Up @@ -75,7 +75,7 @@ public function write($key, $value, $duration) {
*/
public function read($key) {
$time = time();
$cachetime = intval(apc_fetch($key . '_expires'));
$cachetime = (int)apc_fetch($key . '_expires');
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Cache/Engine/FileEngine.php
Expand Up @@ -165,7 +165,7 @@ public function read($key) {

$this->_File->rewind();
$time = time();
$cachetime = intval($this->_File->current());
$cachetime = (int)$this->_File->current();

if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
if ($this->settings['lock']) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Cache/Engine/WincacheEngine.php
Expand Up @@ -80,7 +80,7 @@ public function write($key, $value, $duration) {
*/
public function read($key) {
$time = time();
$cachetime = intval(wincache_ucache_get($key . '_expires'));
$cachetime = (int)wincache_ucache_get($key . '_expires');
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Cache/Engine/XcacheEngine.php
Expand Up @@ -80,7 +80,7 @@ public function write($key, $value, $duration) {
public function read($key) {
if (xcache_isset($key)) {
$time = time();
$cachetime = intval(xcache_get($key . '_expires'));
$cachetime = (int)xcache_get($key . '_expires');
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Console/Command/AclShell.php
Expand Up @@ -579,8 +579,8 @@ protected function _getNodeId($class, $identifier) {
* @return array aro, aco, action
*/
protected function _getParams() {
$aro = is_numeric($this->args[0]) ? intval($this->args[0]) : $this->args[0];
$aco = is_numeric($this->args[1]) ? intval($this->args[1]) : $this->args[1];
$aro = is_numeric($this->args[0]) ? (int)$this->args[0] : $this->args[0];
$aco = is_numeric($this->args[1]) ? (int)$this->args[1] : $this->args[1];
$aroName = $aro;
$acoName = $aco;

Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Console/Command/Task/ControllerTask.php
Expand Up @@ -452,14 +452,14 @@ public function getName($useDbConfig = null) {
return $this->_stop();
}

if (!$enteredController || intval($enteredController) > count($controllers)) {
if (!$enteredController || (int)$enteredController > count($controllers)) {
$this->err(__d('cake_console', "The Controller name you supplied was empty,\nor the number you selected was not an option. Please try again."));
$enteredController = '';
}
}

if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers)) {
$controllerName = $controllers[intval($enteredController) - 1];
if ((int)$enteredController > 0 && (int)$enteredController <= count($controllers)) {
$controllerName = $controllers[(int)$enteredController - 1];
} else {
$controllerName = Inflector::camelize($enteredController);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -417,7 +417,7 @@ protected function _parse($functionName, $map) {
if ($mapCount === count($strings)) {
extract(array_combine($map, $strings));
$category = isset($category) ? $category : 6;
$category = intval($category);
$category = (int)$category;
$categoryName = $categories[$category];
$domain = isset($domain) ? $domain : 'default';
$details = array(
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Console/Command/Task/ModelTask.php
Expand Up @@ -176,7 +176,7 @@ public function inOptions($options, $prompt = null, $default = null) {
$prompt = __d('cake_console', 'Make a selection from the choices above');
}
$choice = $this->in($prompt, null, $default);
if (intval($choice) > 0 && intval($choice) <= $max) {
if ((int)$choice > 0 && (int)$choice <= $max) {
$valid = true;
}
}
Expand Down Expand Up @@ -728,7 +728,7 @@ public function doMoreAssociations(Model $model, $associations) {
while (strtolower($wannaDoMoreAssoc) === 'y') {
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
$this->out(__d('cake_console', 'What is the association type?'));
$assocType = intval($this->inOptions($assocs, __d('cake_console', 'Enter a number')));
$assocType = (int)$this->inOptions($assocs, __d('cake_console', 'Enter a number'));

$this->out(__d('cake_console', "For the following options be very careful to match your setup exactly.\n" .
"Any spelling mistakes will cause errors."));
Expand Down Expand Up @@ -765,7 +765,7 @@ public function doMoreAssociations(Model $model, $associations) {
if (!empty($showKeys)) {
$this->out(__d('cake_console', 'A helpful List of possible keys'));
$foreignKey = $this->inOptions($showKeys, __d('cake_console', 'What is the foreignKey?'));
$foreignKey = $showKeys[intval($foreignKey)];
$foreignKey = $showKeys[(int)$foreignKey];
}
if (!isset($foreignKey)) {
$foreignKey = $this->in(__d('cake_console', 'What is the foreignKey? Specify your own.'), null, $suggestedForeignKey);
Expand Down Expand Up @@ -985,14 +985,14 @@ public function getName($useDbConfig = null) {
return $this->_stop();
}

if (!$enteredModel || intval($enteredModel) > count($this->_modelNames)) {
if (!$enteredModel || (int)$enteredModel > count($this->_modelNames)) {
$this->err(__d('cake_console', "The model name you supplied was empty,\n" .
"or the number you selected was not an option. Please try again."));
$enteredModel = '';
}
}
if (intval($enteredModel) > 0 && intval($enteredModel) <= count($this->_modelNames)) {
return $this->_modelNames[intval($enteredModel) - 1];
if ((int)$enteredModel > 0 && (int)$enteredModel <= count($this->_modelNames)) {
return $this->_modelNames[(int)$enteredModel - 1];
}

return $enteredModel;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Command/Task/PluginTask.php
Expand Up @@ -203,7 +203,7 @@ public function findPath($pathOptions) {
}
$prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
$choice = $this->in($prompt, null, 1);
if (intval($choice) > 0 && intval($choice) <= $max) {
if ((int)$choice > 0 && (int)$choice <= $max) {
$valid = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -414,7 +414,7 @@ protected function _expire($expires = null) {
$now = new DateTime();

if (is_int($expires) || is_numeric($expires)) {
return $this->_expires = $now->format('U') + intval($expires);
return $this->_expires = $now->format('U') + (int)$expires;
}
$now->modify($expires);
return $this->_expires = $now->format('U');
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Controller/Component/PaginatorComponent.php
Expand Up @@ -179,7 +179,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$extra['type'] = $type;
}

if (intval($page) < 1) {
if ((int)$page < 1) {
$page = 1;
}
$page = $options['page'] = (int)$page;
Expand Down Expand Up @@ -211,7 +211,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
}
$count = $object->find('count', array_merge($parameters, $extra));
}
$pageCount = intval(ceil($count / $limit));
$pageCount = (int)ceil($count / $limit);
$requestedPage = $page;
$page = max(min($page, $pageCount), 1);

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -493,7 +493,7 @@ public function index($model) {
if ($idx->Index_type === 'FULLTEXT') {
$index[$idx->Key_name]['type'] = strtolower($idx->Index_type);
} else {
$index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
$index[$idx->Key_name]['unique'] = (int)($idx->Non_unique == 0);
}
} else {
if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -219,10 +219,10 @@ public function describe($model) {
} elseif ($c->type === 'uuid') {
$length = 36;
} else {
$length = intval($c->oct_length);
$length = (int)$c->oct_length;
}
} elseif (!empty($c->char_length)) {
$length = intval($c->char_length);
$length = (int)$c->char_length;
} else {
$length = $this->length($c->type);
}
Expand Down Expand Up @@ -726,7 +726,7 @@ public function length($real) {
return 36;
}
if ($limit) {
return intval($limit);
return (int)$limit;
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -509,7 +509,7 @@ public function index($model) {
$key['name'] = 'PRIMARY';
}
$index[$key['name']]['column'] = $keyCol[0]['name'];
$index[$key['name']]['unique'] = intval($key['unique'] == 1);
$index[$key['name']]['unique'] = (int)$key['unique'] === 1;
} else {
if (!is_array($index[$key['name']]['column'])) {
$col[] = $index[$key['name']]['column'];
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -537,9 +537,9 @@ public function renderStatement($type, $data) {
if (version_compare($this->getVersion(), '11', '<') && preg_match('/FETCH\sFIRST\s+([0-9]+)/i', $limit, $offset)) {
preg_match('/OFFSET\s*(\d+)\s*.*?(\d+)\s*ROWS/', $limit, $limitOffset);

$limit = 'TOP ' . intval($limitOffset[2]);
$page = intval($limitOffset[1] / $limitOffset[2]);
$offset = intval($limitOffset[2] * $page);
$limit = 'TOP ' . (int)$limitOffset[2];
$page = (int)($limitOffset[1] / $limitOffset[2]);
$offset = (int)($limitOffset[2] * $page);

$rowCounter = self::ROW_COUNTER;
$sql = "SELECT {$limit} * FROM (
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -3048,7 +3048,7 @@ public function length($real) {
list($col, $limit) = explode('(', $col);
}
if ($limit !== null) {
return intval($limit);
return (int)$limit;
}
return null;
}
Expand Down Expand Up @@ -3089,7 +3089,7 @@ public function length($real) {
}
}
}
return intval($length);
return (int)$length;
}

/**
Expand Down Expand Up @@ -3456,10 +3456,10 @@ public function introspectType($value) {
if (is_bool($value)) {
return 'boolean';
}
if (is_float($value) && floatval($value) === $value) {
if (is_float($value) && (float)$value === $value) {
return 'float';
}
if (is_int($value) && intval($value) === $value) {
if (is_int($value) && (int)$value === $value) {
return 'integer';
}
if (is_string($value) && strlen($value) > 255) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/Model/Model.php
Expand Up @@ -2094,7 +2094,7 @@ public function updateCounterCache($keys = array(), $created = false) {

if (isset($keys['old'][$foreignKey]) && $keys['old'][$foreignKey] != $keys[$foreignKey]) {
$conditions[$fkQuoted] = $keys['old'][$foreignKey];
$count = intval($this->find('count', compact('conditions', 'recursive')));
$count = (int)$this->find('count', compact('conditions', 'recursive'));

$Model->updateAll(
array($field => $count),
Expand All @@ -2108,7 +2108,7 @@ public function updateCounterCache($keys = array(), $created = false) {
$conditions = array_merge($conditions, (array)$conditions);
}

$count = intval($this->find('count', compact('conditions', 'recursive')));
$count = (int)$this->find('count', compact('conditions', 'recursive'));

$Model->updateAll(
array($field => $count),
Expand Down Expand Up @@ -2979,7 +2979,7 @@ public function buildQuery($type = 'first', $query = array()) {
$query = $this->{'_find' . ucfirst($type)}('before', $query);
}

if (!is_numeric($query['page']) || intval($query['page']) < 1) {
if (!is_numeric($query['page']) || (int)$query['page'] < 1) {
$query['page'] = 1;
}

Expand Down Expand Up @@ -3092,7 +3092,7 @@ protected function _findCount($state, $query, $results = array()) {
return count($results);
}

return intval($results[0][$key]['count']);
return (int)$results[0][$key]['count'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -2528,7 +2528,7 @@ public function testUpdateExisting() {
'user' => 'some user',
'password' => 'some password'
)));
$this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
$this->assertTrue(is_int($TestModel->id) || ((int)$TestModel->id === 5));
$id = $TestModel->id;

$TestModel->save(array(
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Utility/DebuggerTest.php
Expand Up @@ -187,7 +187,7 @@ public function testChangeOutputFormats() {
'error' => array(),
'code' => array(), '8', '/code',
'file' => array(), 'preg:/[^<]+/', '/file',
'line' => array(), '' . (intval(__LINE__) - 7), '/line',
'line' => array(), '' . ((int)__LINE__ - 7), '/line',
'preg:/Undefined variable:\s+foo/',
'/error'
);
Expand Down Expand Up @@ -246,7 +246,7 @@ public function testAddFormat() {
'<error',
'<code', '8', '/code',
'<file', 'preg:/[^<]+/', '/file',
'<line', '' . (intval(__LINE__) - 7), '/line',
'<line', '' . ((int)__LINE__ - 7), '/line',
'preg:/Undefined variable:\s+foo/',
'/error'
);
Expand Down
14 changes: 7 additions & 7 deletions lib/Cake/Utility/CakeTime.php
Expand Up @@ -321,7 +321,7 @@ public static function fromString($dateString, $timezone = null) {
}

if (is_int($dateString) || is_numeric($dateString)) {
$date = intval($dateString);
$date = (int)$dateString;
} elseif (
$dateString instanceof DateTime &&
$dateString->getTimezone()->getName() != date_default_timezone_get()
Expand Down Expand Up @@ -994,12 +994,12 @@ public static function gmt($dateString = null) {
$time = self::fromString($dateString);
}
return gmmktime(
intval(date('G', $time)),
intval(date('i', $time)),
intval(date('s', $time)),
intval(date('n', $time)),
intval(date('j', $time)),
intval(date('Y', $time))
(int)date('G', $time),
(int)date('i', $time),
(int)date('s', $time),
(int)date('n', $time),
(int)date('j', $time),
(int)date('Y', $time)
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Utility/Hash.php
Expand Up @@ -290,8 +290,8 @@ protected static function _simpleOp($op, $data, $path, $values = null) {
$count = count($path);
$last = $count - 1;
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key);
if (is_numeric($key) && (int)$key > 0 || $key === '0') {
$key = (int)$key;
}
if ($op === 'insert') {
if ($i === $last) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Utility/Set.php
Expand Up @@ -560,7 +560,7 @@ public static function classicExtract($data, $path = null) {
}

foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
if (is_numeric($key) && (int)$key > 0 || $key === '0') {
if (isset($data[$key])) {
$data = $data[$key];
} else {
Expand Down Expand Up @@ -657,8 +657,8 @@ public static function check($data, $path = null) {
}

foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key);
if (is_numeric($key) && (int)$key > 0 || $key === '0') {
$key = (int)$key;
}
if ($i === count($path) - 1) {
return (is_array($data) && array_key_exists($key, $data));
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2893,7 +2893,7 @@ protected function _generateOptions($name, $options = array()) {
}
break;
case 'year':
$current = intval(date('Y'));
$current = (int)date('Y');

$min = !isset($options['min']) ? $current - 20 : (int)$options['min'];
$max = !isset($options['max']) ? $current + 20 : (int)$options['max'];
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/JsHelper.php
Expand Up @@ -292,7 +292,7 @@ protected function _createVars() {
*/
public function link($title, $url = null, $options = array()) {
if (!isset($options['id'])) {
$options['id'] = 'link-' . intval(mt_rand());
$options['id'] = 'link-' . (int)mt_rand();
}
list($options, $htmlOptions) = $this->_getHtmlOptions($options);
$out = $this->Html->link($title, $url, $htmlOptions);
Expand Down Expand Up @@ -368,7 +368,7 @@ public function set($one, $two = null) {
*/
public function submit($caption = null, $options = array()) {
if (!isset($options['id'])) {
$options['id'] = 'submit-' . intval(mt_rand());
$options['id'] = 'submit-' . (int)mt_rand();
}
$formOptions = array('div');
list($options, $htmlOptions) = $this->_getHtmlOptions($options, $formOptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -740,7 +740,7 @@ public function numbers($options = array()) {
$out = '';

if ($modulus && $params['pageCount'] > $modulus) {
$half = intval($modulus / 2);
$half = (int)($modulus / 2);
$end = $params['page'] + $half;

if ($end > $params['pageCount']) {
Expand Down

0 comments on commit 282bc0c

Please sign in to comment.