Commit 1c414caa by Alexander Makarov

Improved checks where substr_compare is used, replaced more substr + comapre…

Improved checks where substr_compare is used, replaced more substr + comapre cases with substr_compare
parent ca9d4867
......@@ -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;
}
......
......@@ -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) {
......
......@@ -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);
}
......
......@@ -23,10 +23,12 @@ if (empty($see)) {
} else {
echo '<p>See also:</p><ul>';
foreach ($see as $ref) {
if (substr_compare($ref, '>', -1, 1)) {
$ref .= '.';
if (!empty($ref)) {
if (substr_compare($ref, '>', -1, 1)) {
$ref .= '.';
}
echo "<li>$ref</li>";
}
echo "<li>$ref</li>";
}
echo '</ul>';
}
......@@ -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;
......
......@@ -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;
......
......@@ -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".');
}
}
......
......@@ -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';
}
}
......
......@@ -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;
......
......@@ -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));
......
......@@ -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 . '@@';
......
......@@ -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']);
......
......@@ -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);
......
......@@ -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));
}
......
......@@ -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;
}
......
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