Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type checks #846

Closed
wants to merge 11 commits into from
5 changes: 2 additions & 3 deletions lib/Cake/Cache/Engine/ApcEngine.php
Expand Up @@ -61,9 +61,8 @@ public function init($settings = array()) {
* @return boolean True if the data was successfully cached, false on failure
*/
public function write($key, $value, $duration) {
if ($duration == 0) {
$expires = 0;
} else {
$expires = 0;
if ($duration) {
$expires = time() + $duration;
}
apc_store($key . '_expires', $expires, $duration);
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Command/AclShell.php
Expand Up @@ -598,7 +598,7 @@ protected function _getParams() {
* @return array Variables
*/
protected function _dataVars($type = null) {
if ($type == null) {
if (!$type) {
$type = $this->args[0];
}
$vars = array();
Expand Down
9 changes: 5 additions & 4 deletions lib/Cake/Console/Command/ApiShell.php
Expand Up @@ -73,12 +73,13 @@ public function main() {
$path = $this->paths['core'];
}

if (count($this->args) == 1) {
$file = $type;
$class = Inflector::camelize($type);
} elseif (count($this->args) > 1) {
$count = count($this->args);
if ($count > 1) {
$file = Inflector::underscore($this->args[1]);
$class = Inflector::camelize($this->args[1]);
} elseif ($count) {
$file = $type;
$class = Inflector::camelize($type);
}
$objects = App::objects('class', $path);
if (in_array($class, $objects)) {
Expand Down
14 changes: 7 additions & 7 deletions lib/Cake/Console/Command/ConsoleShell.php
Expand Up @@ -182,7 +182,7 @@ public function main($command = null) {
$this->out(" - {$model}");
}
break;
case (preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp) == true):
case preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp):
foreach ($tmp as $data) {
$data = strip_tags($data);
$data = str_replace($this->badCommandChars, "", $data);
Expand All @@ -200,7 +200,7 @@ public function main($command = null) {
$this->out(__d('cake_console', "Please verify you are using valid models and association types"));
}
break;
case (preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp) == true):
case preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp):
foreach ($tmp as $data) {
$data = strip_tags($data);
$data = str_replace($this->badCommandChars, "", $data);
Expand Down Expand Up @@ -298,7 +298,7 @@ public function main($command = null) {
$this->out(__d('cake_console', 'Saved record for %s', $modelToSave));
}
break;
case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
case preg_match("/^(\w+) columns/", $command, $tmp):
$modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));

if ($this->_isValidModel($modelToCheck)) {
Expand All @@ -315,22 +315,22 @@ public function main($command = null) {
$this->out(__d('cake_console', "Please verify that you selected a valid model"));
}
break;
case (preg_match("/^routes\s+reload/i", $command, $tmp) == true):
case preg_match("/^routes\s+reload/i", $command, $tmp):
if (!$this->_loadRoutes()) {
$this->err(__d('cake_console', "There was an error loading the routes config. Please check that the file exists and is free of parse errors."));
break;
}
$this->out(__d('cake_console', "Routes configuration reloaded, %d routes connected", count(Router::$routes)));
break;
case (preg_match("/^routes\s+show/i", $command, $tmp) == true):
case preg_match("/^routes\s+show/i", $command, $tmp):
$this->out(print_r(Hash::combine(Router::$routes, '{n}.template', '{n}.defaults'), true));
break;
case (preg_match("/^route\s+(\(.*\))$/i", $command, $tmp) == true):
case preg_match("/^route\s+(\(.*\))$/i", $command, $tmp):
if ($url = eval('return array' . $tmp[1] . ';')) {
$this->out(Router::url($url));
}
break;
case (preg_match("/^route\s+(.*)/i", $command, $tmp) == true):
case preg_match("/^route\s+(.*)/i", $command, $tmp):
$this->out(var_export(Router::parse($tmp[1]), true));
break;
default:
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Console/Command/Task/ControllerTask.php
Expand Up @@ -395,7 +395,7 @@ public function listAll($useDbConfig = null) {
}
$this->__tables = $this->Model->getAllTables($useDbConfig);

if ($this->interactive == true) {
if ($this->interactive) {
$this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
$this->hr();
$this->_controllerNames = array();
Expand All @@ -419,14 +419,14 @@ public function getName($useDbConfig = null) {
$controllers = $this->listAll($useDbConfig);
$enteredController = '';

while ($enteredController == '') {
while (!$enteredController) {
$enteredController = $this->in(__d('cake_console', "Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit"), null, 'q');
if ($enteredController === 'q') {
$this->out(__d('cake_console', 'Exit'));
return $this->_stop();
}

if ($enteredController == '' || intval($enteredController) > count($controllers)) {
if (!$enteredController || intval($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 = '';
}
Expand Down
30 changes: 13 additions & 17 deletions lib/Cake/Console/Command/Task/DbConfigTask.php
Expand Up @@ -92,10 +92,10 @@ protected function _interactive() {
$done = false;
$dbConfigs = array();

while ($done == false) {
while (!$done) {
$name = '';

while ($name == '') {
while (!$name) {
$name = $this->in(__d('cake_console', "Name:"), null, 'default');
if (preg_match('/[^a-z0-9_]/i', $name)) {
$name = '';
Expand All @@ -116,12 +116,12 @@ protected function _interactive() {
}

$host = '';
while ($host == '') {
while (!$host) {
$host = $this->in(__d('cake_console', 'Database Host:'), null, 'localhost');
}

$port = '';
while ($port == '') {
while (!$port) {
$port = $this->in(__d('cake_console', 'Port?'), null, 'n');
}

Expand All @@ -130,16 +130,16 @@ protected function _interactive() {
}

$login = '';
while ($login == '') {
while (!$login) {
$login = $this->in(__d('cake_console', 'User:'), null, 'root');
}
$password = '';
$blankPassword = false;

while ($password == '' && $blankPassword == false) {
while (!$password && !$blankPassword) {
$password = $this->in(__d('cake_console', 'Password:'));

if ($password == '') {
if (!$password) {
$blank = $this->in(__d('cake_console', 'The password you supplied was empty. Use an empty password?'), array('y', 'n'), 'n');
if ($blank == 'y') {
$blankPassword = true;
Expand All @@ -148,20 +148,20 @@ protected function _interactive() {
}

$database = '';
while ($database == '') {
while (!$database) {
$database = $this->in(__d('cake_console', 'Database Name:'), null, 'cake');
}

$prefix = '';
while ($prefix == '') {
while (!$prefix) {
$prefix = $this->in(__d('cake_console', 'Table Prefix?'), null, 'n');
}
if (strtolower($prefix) == 'n') {
$prefix = null;
}

$encoding = '';
while ($encoding == '') {
while (!$encoding) {
$encoding = $this->in(__d('cake_console', 'Table encoding?'), null, 'n');
}
if (strtolower($encoding) == 'n') {
Expand All @@ -170,7 +170,7 @@ protected function _interactive() {

$schema = '';
if ($datasource == 'postgres') {
while ($schema == '') {
while (!$schema) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor change in behaviour as '0' is no longer an acceptable value. However, in the above cases I don't think its relevant.

$schema = $this->in(__d('cake_console', 'Table schema?'), null, 'n');
}
}
Expand All @@ -180,7 +180,7 @@ protected function _interactive() {

$config = compact('name', 'datasource', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');

while ($this->_verify($config) == false) {
while (!$this->_verify($config)) {
$this->_interactive();
}

Expand Down Expand Up @@ -277,11 +277,7 @@ public function bake($configs) {
$info['port'] = null;
}

if ($info['persistent'] === false) {
$info['persistent'] = 'false';
} else {
$info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
}
$info['persistent'] = var_export((bool)$info['persistent'], true);

$oldConfigs[] = array(
'name' => $configName,
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -391,7 +391,7 @@ protected function _parse($functionName, $map) {
$position = $count;
$depth = 0;

while ($depth == 0) {
while (!$depth) {
if ($this->_tokens[$position] == '(') {
$depth++;
} elseif ($this->_tokens[$position] == ')') {
Expand Down Expand Up @@ -480,7 +480,7 @@ protected function _processValidationRules($field, $rules, $file, $domain) {
}

$dims = Hash::dimensions($rules);
if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
if ($dims === 1 || ($dims === 2 && isset($rules['message']))) {
$rules = array($rules);
}

Expand Down Expand Up @@ -591,7 +591,7 @@ protected function _writeFiles() {
);
if (strtoupper($response) === 'N') {
$response = '';
while ($response == '') {
while (!$response) {
$response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
$File = new File($this->_output . $response);
$filename = $response;
Expand Down
25 changes: 11 additions & 14 deletions lib/Cake/Console/Command/Task/ModelTask.php
Expand Up @@ -472,16 +472,14 @@ public function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
}

if ($choice != $defaultChoice) {
$validate[$validatorName] = $choice;
if (is_numeric($choice) && isset($this->_validations[$choice])) {
$validate[$validatorName] = $this->_validations[$choice];
} else {
$validate[$validatorName] = $choice;
}
}
if ($this->interactive == true && $choice != $defaultChoice) {
$anotherValidator = 'n';
if ($this->interactive && $choice != $defaultChoice) {
$anotherValidator = $this->in(__d('cake_console', 'Would you like to add another validation rule?'), array('y', 'n'), 'n');
} else {
$anotherValidator = 'n';
}
}
return $validate;
Expand Down Expand Up @@ -583,7 +581,7 @@ public function findHasOneAndMany(Model $model, $associations) {

$pattern = '/_' . preg_quote($model->table, '/') . '|' . preg_quote($model->table, '/') . '_/';
$possibleJoinTable = preg_match($pattern, $otherTable);
if ($possibleJoinTable == true) {
if ($possibleJoinTable) {
continue;
}
foreach ($modelFieldsTemp as $fieldName => $field) {
Expand Down Expand Up @@ -700,7 +698,7 @@ public function doMoreAssociations(Model $model, $associations) {
$alias = $this->in(__d('cake_console', 'What is the alias for this association?'));
$className = $this->in(__d('cake_console', 'What className will %s use?', $alias), null, $alias );

if ($assocType == 0) {
if (!$assocType) {
if (!empty($possibleKeys[$model->table])) {
$showKeys = $possibleKeys[$model->table];
} else {
Expand Down Expand Up @@ -780,7 +778,7 @@ protected function _generatePossibleKeys() {
*/
public function bake($name, $data = array()) {
if (is_object($name)) {
if ($data == false) {
if (!$data) {
$data = array();
$data['associations'] = $this->doAssociations($name);
$data['validate'] = $this->doValidation($name);
Expand Down Expand Up @@ -935,7 +933,7 @@ public function getName($useDbConfig = null) {

$enteredModel = '';

while ($enteredModel == '') {
while (!$enteredModel) {
$enteredModel = $this->in(__d('cake_console', "Enter a number from the list above,\n" .
"type in the name of another model, or 'q' to exit"), null, 'q');

Expand All @@ -944,18 +942,17 @@ public function getName($useDbConfig = null) {
$this->_stop();
}

if ($enteredModel == '' || intval($enteredModel) > count($this->_modelNames)) {
if (!$enteredModel || intval($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)) {
$currentModelName = $this->_modelNames[intval($enteredModel) - 1];
} else {
$currentModelName = $enteredModel;
return $this->_modelNames[intval($enteredModel) - 1];
}
return $currentModelName;

return $enteredModel;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions lib/Cake/Console/Command/Task/ProjectTask.php
Expand Up @@ -66,7 +66,7 @@ public function execute() {
}

$response = false;
while ($response == false && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
while (!$response && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
$prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
$response = $this->in($prompt, array('y', 'n'), 'n');
if (strtolower($response) === 'n') {
Expand Down Expand Up @@ -349,10 +349,7 @@ protected function _replaceCorePath($filename, $hardCode) {
if (!file_put_contents($filename, $result)) {
return false;
}
if ($count == 0) {
return false;
}
return true;
return (bool)$count;
}

/**
Expand Down Expand Up @@ -387,7 +384,7 @@ public function getPrefix() {
$admin = '';
$prefixes = Configure::read('Routing.prefixes');
if (!empty($prefixes)) {
if (count($prefixes) == 1) {
if (count($prefixes) === 1) {
return $prefixes[0] . '_';
}
if ($this->interactive) {
Expand All @@ -409,7 +406,7 @@ public function getPrefix() {
$this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
$this->out(__d('cake_console', 'What would you like the prefix route to be?'));
$this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
while ($admin == '') {
while (!$admin) {
$admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
}
if ($this->cakeAdmin($admin) !== true) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Console/Command/Task/TemplateTask.php
Expand Up @@ -123,7 +123,7 @@ public function set($one, $two = null) {
$data = array($one => $two);
}

if ($data == null) {
if (!$data) {
return false;
}
$this->templateVars = $data + $this->templateVars;
Expand Down Expand Up @@ -166,7 +166,7 @@ public function generate($directory, $filename, $vars = null) {
* @return string returns the path to the selected theme.
*/
public function getThemePath() {
if (count($this->templatePaths) == 1) {
if (count($this->templatePaths) === 1) {
$paths = array_values($this->templatePaths);
return $paths[0];
}
Expand Down