From 1c414caacefbae4ed5e38d99fd58e0221baf79fe Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 30 Jul 2014 02:15:08 +0400 Subject: [PATCH] Improved checks where substr_compare is used, replaced more substr + comapre cases with substr_compare --- build/controllers/PhpDocController.php | 36 +++++++++++-------- extensions/apidoc/models/TypeDoc.php | 2 +- extensions/apidoc/renderers/BaseRenderer.php | 2 +- .../apidoc/templates/html/views/seeAlso.php | 8 +++-- extensions/gii/generators/crud/Generator.php | 2 +- extensions/gii/generators/model/Generator.php | 8 ++--- .../gii/generators/module/Generator.php | 2 +- extensions/mongodb/gii/model/Generator.php | 2 +- extensions/sphinx/gii/model/Generator.php | 6 ++-- .../console/controllers/HelpController.php | 2 +- .../console/controllers/MessageController.php | 4 +-- framework/helpers/BaseHtml.php | 4 +-- framework/i18n/GettextMoFile.php | 3 +- framework/i18n/MessageFormatter.php | 3 +- framework/log/Target.php | 2 +- 15 files changed, 49 insertions(+), 37 deletions(-) diff --git a/build/controllers/PhpDocController.php b/build/controllers/PhpDocController.php index dca2156b61..7158e751d5 100644 --- a/build/controllers/PhpDocController.php +++ b/build/controllers/PhpDocController.php @@ -170,12 +170,15 @@ class PhpDocController extends Controller $namespaceLine = ''; $contentAfterNamespace = false; foreach($lines as $i => $line) { - if (substr(trim($line), 0, 9) === 'namespace') { - $namespace = $i; - $namespaceLine = trim($line); - } elseif ($namespace !== false && trim($line) !== '') { - $contentAfterNamespace = $i; - break; + $line = trim($line); + if (!empty($line)) { + if (substr_compare($line, 'namespace', 0, 9) === 0) { + $namespace = $i; + $namespaceLine = $line; + } elseif ($namespace !== false) { + $contentAfterNamespace = $i; + break; + } } } @@ -275,14 +278,18 @@ class PhpDocController extends Controller // TODO move these checks to different action $lines = explode("\n", $newDoc); - if (trim($lines[1]) == '*' || substr(trim($lines[1]), 0, 3) == '* @') { + $firstLine = trim($lines[1]); + if ($firstLine === '*' || (!empty($firstLine) && substr_compare($firstLine, '* @', 0, 3) === 0)) { $this->stderr("[WARN] Class $className has no short description.\n", Console::FG_YELLOW, Console::BOLD); } foreach ($lines as $line) { - if (substr(trim($line), 0, 9) == '* @since ') { - $seenSince = true; - } elseif (substr(trim($line), 0, 10) == '* @author ') { - $seenAuthor = true; + $line = trim($line); + if (!empty($line)) { + if (substr_compare($line, '* @since ', 0, 9) === 0) { + $seenSince = true; + } elseif (substr_compare($line, '* @author ', 0, 10) === 0) { + $seenAuthor = true; + } } } @@ -350,13 +357,14 @@ class PhpDocController extends Controller $propertyPart = false; $propertyPosition = false; foreach ($lines as $i => $line) { - if (substr(trim($line), 0, 12) == '* @property ') { + $line = trim($line); + if (!empty($line) && substr_compare($line, '* @property ', 0, 12) === 0) { $propertyPart = true; - } elseif ($propertyPart && trim($line) == '*') { + } elseif ($propertyPart && $line == '*') { $propertyPosition = $i; $propertyPart = false; } - if (substr(trim($line), 0, 10) == '* @author ' && $propertyPosition === false) { + if (!empty($line) && substr_compare($line, '* @author ', 0, 10) === 0 && $propertyPosition === false) { $propertyPosition = $i - 1; $propertyPart = false; } diff --git a/extensions/apidoc/models/TypeDoc.php b/extensions/apidoc/models/TypeDoc.php index 80d31d91ee..439bc52149 100644 --- a/extensions/apidoc/models/TypeDoc.php +++ b/extensions/apidoc/models/TypeDoc.php @@ -46,7 +46,7 @@ class TypeDoc extends BaseDoc } } } - if (substr_compare($subjectName, '()', -2, 2) === 0) { + if (!empty($subjectName) && substr_compare($subjectName, '()', -2, 2) === 0) { return null; } if ($this->properties === null) { diff --git a/extensions/apidoc/renderers/BaseRenderer.php b/extensions/apidoc/renderers/BaseRenderer.php index cf097ac658..33aed9a029 100644 --- a/extensions/apidoc/renderers/BaseRenderer.php +++ b/extensions/apidoc/renderers/BaseRenderer.php @@ -72,7 +72,7 @@ abstract class BaseRenderer extends Component foreach ($types as $type) { $postfix = ''; if (is_string($type)) { - if (substr_compare($type, '[]', -2, 2) === 0) { + if (!empty($type) && substr_compare($type, '[]', -2, 2) === 0) { $postfix = '[]'; $type = substr($type, 0, -2); } diff --git a/extensions/apidoc/templates/html/views/seeAlso.php b/extensions/apidoc/templates/html/views/seeAlso.php index 12d83c12fc..8abbf31497 100644 --- a/extensions/apidoc/templates/html/views/seeAlso.php +++ b/extensions/apidoc/templates/html/views/seeAlso.php @@ -23,10 +23,12 @@ if (empty($see)) { } else { echo '

See also:

'; } diff --git a/extensions/gii/generators/crud/Generator.php b/extensions/gii/generators/crud/Generator.php index 8a68386933..3be482f8d5 100644 --- a/extensions/gii/generators/crud/Generator.php +++ b/extensions/gii/generators/crud/Generator.php @@ -373,7 +373,7 @@ class Generator extends \yii\gii\Generator $labels[$name] = 'ID'; } else { $label = Inflector::camel2words($name); - if (strcasecmp(substr($label, -3), ' id') === 0) { + if (!empty($label) && substr_compare($label, ' id', -3, null, true) === 0) { $label = substr($label, 0, -3) . ' ID'; } $labels[$name] = $label; diff --git a/extensions/gii/generators/model/Generator.php b/extensions/gii/generators/model/Generator.php index aeb168a81a..d6ac4d47ee 100644 --- a/extensions/gii/generators/model/Generator.php +++ b/extensions/gii/generators/model/Generator.php @@ -196,7 +196,7 @@ class Generator extends \yii\gii\Generator $labels[$column->name] = 'ID'; } else { $label = Inflector::camel2words($column->name); - if (strcasecmp(substr($label, -3), ' id') === 0) { + if (!empty($label) && substr_compare($label, ' id', -3, null, true)) { $label = substr($label, 0, -3) . ' ID'; } $labels[$column->name] = $label; @@ -428,7 +428,7 @@ class Generator extends \yii\gii\Generator */ protected function generateRelationName($relations, $className, $table, $key, $multiple) { - if (strcasecmp(substr($key, -2), 'id') === 0 && strcasecmp($key, 'id')) { + if (!empty($key) && substr_compare($key, 'id', -2, null, true) === 0 && strcasecmp($key, 'id')) { $key = rtrim(substr($key, 0, -2), '_'); } if ($multiple) { @@ -478,7 +478,7 @@ class Generator extends \yii\gii\Generator if ($this->isReservedKeyword($this->modelClass)) { $this->addError('modelClass', 'Class name cannot be a reserved PHP keyword.'); } - if (substr_compare($this->tableName, '*', -1) && $this->modelClass == '') { + if ((empty($this->tableName) || substr_compare($this->tableName, '*', -1)) && $this->modelClass == '') { $this->addError('modelClass', 'Model Class cannot be blank if table name does not end with asterisk.'); } } @@ -488,7 +488,7 @@ class Generator extends \yii\gii\Generator */ public function validateTableName() { - if (strpos($this->tableName, '*') !== false && substr($this->tableName, -1) !== '*') { + if (strpos($this->tableName, '*') !== false && substr_compare($this->tableName, '*', -1)) { $this->addError('tableName', 'Asterisk is only allowed as the last character.'); return; diff --git a/extensions/gii/generators/module/Generator.php b/extensions/gii/generators/module/Generator.php index 270454865c..04cefbf65b 100644 --- a/extensions/gii/generators/module/Generator.php +++ b/extensions/gii/generators/module/Generator.php @@ -146,7 +146,7 @@ EOD; if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass), false) === false) { $this->addError('moduleClass', 'Module class must be properly namespaced.'); } - if (substr_compare($this->moduleClass, '\\', -1, 1) === 0) { + if (empty($this->moduleClass) || substr_compare($this->moduleClass, '\\', -1, 1) === 0) { $this->addError('moduleClass', 'Module class name must not be empty. Please enter a fully qualified class name. e.g. "app\\modules\\admin\\Module".'); } } diff --git a/extensions/mongodb/gii/model/Generator.php b/extensions/mongodb/gii/model/Generator.php index cdfde9ffbe..0c671754d7 100644 --- a/extensions/mongodb/gii/model/Generator.php +++ b/extensions/mongodb/gii/model/Generator.php @@ -182,7 +182,7 @@ class Generator extends \yii\gii\Generator $label = 'ID'; } else { $label = Inflector::camel2words($attribute); - if (strcasecmp(substr($label, -3), ' id') === 0) { + if (substr_compare($label, ' id', -3, null, true) === 0) { $label = substr($label, 0, -3) . ' ID'; } } diff --git a/extensions/sphinx/gii/model/Generator.php b/extensions/sphinx/gii/model/Generator.php index c7fb10ef5f..d33a017a4d 100644 --- a/extensions/sphinx/gii/model/Generator.php +++ b/extensions/sphinx/gii/model/Generator.php @@ -180,7 +180,7 @@ class Generator extends \yii\gii\Generator $labels[$column->name] = 'ID'; } else { $label = Inflector::camel2words($column->name); - if (strcasecmp(substr($label, -3), ' id') === 0) { + if (substr_compare($label, ' id', -3, null, true) === 0) { $label = substr($label, 0, -3) . ' ID'; } $labels[$column->name] = $label; @@ -266,7 +266,7 @@ class Generator extends \yii\gii\Generator if ($this->isReservedKeyword($this->modelClass)) { $this->addError('modelClass', 'Class name cannot be a reserved PHP keyword.'); } - if (substr_compare($this->indexName, '*', -1) && $this->modelClass == '') { + if ((empty($this->indexName) || substr_compare($this->indexName, '*', -1)) && $this->modelClass == '') { $this->addError('modelClass', 'Model Class cannot be blank if table name does not end with asterisk.'); } } @@ -276,7 +276,7 @@ class Generator extends \yii\gii\Generator */ public function validateIndexName() { - if (strpos($this->indexName, '*') !== false && substr($this->indexName, -1) !== '*') { + if (strpos($this->indexName, '*') !== false && substr_compare($this->indexName, '*', -1)) { $this->addError('indexName', 'Asterisk is only allowed as the last character.'); return; diff --git a/framework/console/controllers/HelpController.php b/framework/console/controllers/HelpController.php index 7b7d00cd28..7bcad78625 100644 --- a/framework/console/controllers/HelpController.php +++ b/framework/console/controllers/HelpController.php @@ -156,7 +156,7 @@ class HelpController extends Controller if (is_dir($controllerPath)) { $files = scandir($controllerPath); foreach ($files as $file) { - if (strcmp(substr($file, -14), 'Controller.php') === 0) { + if (!empty($file) && substr_compare($file, 'Controller.php', -14) === 0) { $controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4); if ($this->validateControllerClass($controllerClass)) { $commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14)); diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index 6a742d3528..167e6dba39 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -331,7 +331,7 @@ class MessageController extends Controller ksort($existingMessages); foreach ($existingMessages as $message => $translation) { if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeUnused) { - if (mb_strlen($translation, Yii::$app->charset) >= 2 && substr_compare($translation, '@@', 0, 2) === 0 && substr_compare($translation, '@@', -2) === 0) { + if (!empty($translation) && substr_compare($translation, '@@', 0, 2) === 0 && substr_compare($translation, '@@', -2) === 0) { $todo[$message] = $translation; } else { $todo[$message] = '@@' . $translation . '@@'; @@ -445,7 +445,7 @@ EOD; // add obsolete unused messages foreach ($existingMessages as $message => $translation) { if (!isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message]) && !$removeUnused) { - if (mb_strlen($translation, Yii::$app->charset) >= 2 && substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') { + if (!empty($translation) && substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') { $todos[$category . chr(4) . $message] = $translation; } else { $todos[$category . chr(4) . $message] = '@@' . $translation . '@@'; diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 204ab041eb..32aebd4fe1 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -797,13 +797,13 @@ class BaseHtml if (!array_key_exists('size', $options)) { $options['size'] = 4; } - if (!empty($options['multiple']) && substr($name, -2) !== '[]') { + if (!empty($options['multiple']) && !empty($name) && substr_compare($name, '[]', -2)) { $name .= '[]'; } $options['name'] = $name; if (isset($options['unselect'])) { // add a hidden field so that if the list box has no option being selected, it still submits a value - if (substr_compare($name, '[]', -2) === 0) { + if (!empty($name) && substr_compare($name, '[]', -2) === 0) { $name = substr($name, 0, -2); } $hidden = static::hiddenInput($name, $options['unselect']); diff --git a/framework/i18n/GettextMoFile.php b/framework/i18n/GettextMoFile.php index 03beaca963..e60a60b2c9 100644 --- a/framework/i18n/GettextMoFile.php +++ b/framework/i18n/GettextMoFile.php @@ -107,7 +107,8 @@ class GettextMoFile extends GettextFile $id = $this->readString($fileHandle, $sourceLengths[$i], $sourceOffsets[$i]); $separatorPosition = strpos($id, chr(4)); - if (($context && $separatorPosition !== false && substr($id, 0, $separatorPosition) === $context) || + + if (($context && $separatorPosition !== false && !empty($id) && substr_compare($id, $context, 0, $separatorPosition) === 0) || (!$context && $separatorPosition === false)) { if ($separatorPosition !== false) { $id = substr($id, $separatorPosition+1); diff --git a/framework/i18n/MessageFormatter.php b/framework/i18n/MessageFormatter.php index baea084ce6..b5d0d16fea 100644 --- a/framework/i18n/MessageFormatter.php +++ b/framework/i18n/MessageFormatter.php @@ -390,7 +390,8 @@ class MessageFormatter extends Component return false; } $selector = trim($plural[$i++]); - if ($i == 1 && substr($selector, 0, 7) == 'offset:') { + + if ($i == 1 && !empty($selector) && substr_compare($selector, 'offset:', 0, 7) === 0) { $offset = (int) trim(mb_substr($selector, 7, ($pos = mb_strpos(str_replace(["\n", "\r", "\t"], ' ', $selector), ' ', 7)) - 7)); $selector = trim(mb_substr($selector, $pos + 1)); } diff --git a/framework/log/Target.php b/framework/log/Target.php index 63422356b4..821e32df85 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -204,7 +204,7 @@ abstract class Target extends Component $matched = empty($categories); foreach ($categories as $category) { - if ($message[2] === $category || substr($category, -1) === '*' && strpos($message[2], rtrim($category, '*')) === 0) { + if ($message[2] === $category || !empty($category) && substr_compare($category, '*', -1) === 0 && strpos($message[2], rtrim($category, '*')) === 0) { $matched = true; break; }