Browse Source

Fix #18899: Replace usages of `strpos` with `strncmp` and remove redundant usage of `array_merge` and `array_values`

tags/2.0.44
Alexander Gubarev 3 years ago committed by GitHub
parent
commit
e3028c524d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      framework/BaseYii.php
  2. 1
      framework/CHANGELOG.md
  3. 2
      framework/caching/FileCache.php
  4. 2
      framework/console/controllers/MessageController.php
  5. 2
      framework/console/controllers/MigrateController.php
  6. 2
      framework/data/ActiveDataFilter.php
  7. 2
      framework/db/Schema.php
  8. 2
      framework/db/oci/Schema.php
  9. 2
      framework/db/pgsql/Schema.php
  10. 2
      framework/helpers/BaseArrayHelper.php
  11. 6
      framework/helpers/BaseFileHelper.php
  12. 2
      framework/helpers/BaseUrl.php
  13. 2
      framework/i18n/MessageFormatter.php
  14. 2
      framework/log/Logger.php
  15. 2
      framework/rest/CreateAction.php
  16. 6
      framework/validators/UniqueValidator.php
  17. 2
      framework/widgets/Menu.php

4
framework/BaseYii.php

@ -130,7 +130,7 @@ class BaseYii
*/
public static function getAlias($alias, $throwException = true)
{
if (strpos($alias, '@') !== 0) {
if (strncmp($alias, '@', 1) !== 0) {
// not an alias
return $alias;
}
@ -278,7 +278,7 @@ class BaseYii
{
if (isset(static::$classMap[$className])) {
$classFile = static::$classMap[$className];
if (strpos($classFile, '@') === 0) {
if (strncmp($classFile, '@', 1) === 0) {
$classFile = static::getAlias($classFile);
}
} elseif (strpos($className, '\\') !== false) {

1
framework/CHANGELOG.md

@ -19,6 +19,7 @@ Yii Framework 2 Change Log
- Bug #18880: Fix `yii\helpers\ArrayHelper::toArray()` for `DateTime` objects in PHP >= 7.4 (rhertogh)
- Bug #18883: Fix `yii\web\HeaderCollection::fromArray()` now ensures lower case keys (rhertogh)
- Bug #18886: Fix default return of `yii\db\Migration::safeUp()` and `yii\db\Migration::safeDown()` (WinterSilence)
- Enh #18899: Replace usages of `strpos` with `strncmp` and remove redundant usage of `array_merge` and `array_values` (AlexGx)
- Bug #18898: Fix `yii\helpers\Inflector::camel2words()` to work with words ending with 0 (michaelarnauts)
- Enh #18904: Improve Captcha client-side validation (hexkir)

2
framework/caching/FileCache.php

@ -261,7 +261,7 @@ class FileCache extends Cache
{
if (($handle = opendir($path)) !== false) {
while (($file = readdir($handle)) !== false) {
if (strpos($file, '.') === 0) {
if (strncmp($file, '.', 1) === 0) {
continue;
}
$fullPath = $path . DIRECTORY_SEPARATOR . $file;

2
framework/console/controllers/MessageController.php

@ -386,7 +386,7 @@ EOD;
if (!$removeUnused) {
foreach ($obsolete as $pk => $msg) {
if (strpos($msg, '@@') === 0 && substr($msg, -2) === '@@') {
if (strncmp($msg, '@@', 2) === 0 && substr($msg, -2) === '@@') {
unset($obsolete[$pk]);
}
}

2
framework/console/controllers/MigrateController.php

@ -387,7 +387,7 @@ class MigrateController extends BaseMigrateController
$name = substr($name, 0, -1);
}
if (strpos($name, '_') === 0) {
if (strncmp($name, '_', 1) === 0) {
return substr($name, 1);
}

2
framework/data/ActiveDataFilter.php

@ -101,7 +101,7 @@ class ActiveDataFilter extends DataFilter
if (!empty($parts)) {
if (count($parts) > 1) {
$parts = array_merge(['AND'], $parts);
array_unshift($parts, 'AND');
} else {
$parts = array_shift($parts);
}

2
framework/db/Schema.php

@ -480,7 +480,7 @@ abstract class Schema extends BaseObject
public function quoteTableName($name)
{
if (strpos($name, '(') === 0 && strpos($name, ')') === strlen($name) - 1) {
if (strncmp($name, '(', 1) === 0 && strpos($name, ')') === strlen($name) - 1) {
return $name;
}
if (strpos($name, '{{') !== false) {

2
framework/db/oci/Schema.php

@ -421,7 +421,7 @@ SQL;
if ($defaultValue !== null) {
if (
strlen($defaultValue) > 2
&& strpos($defaultValue, "'") === 0
&& strncmp($defaultValue, "'", 1) === 0
&& substr($defaultValue, -1) === "'"
) {
$defaultValue = substr($defaultValue, 1, -1);

2
framework/db/pgsql/Schema.php

@ -443,7 +443,7 @@ SQL;
$row = array_change_key_case($row, CASE_LOWER);
}
$column = $row['columnname'];
if (strpos($column, '"') === 0) {
if (strncmp($column, '"', 1) === 0) {
// postgres will quote names that are not lowercase-only
// https://github.com/yiisoft/yii2/issues/10613
$column = substr($column, 1, -1);

2
framework/helpers/BaseArrayHelper.php

@ -954,7 +954,7 @@ class BaseArrayHelper
continue;
}
if (is_string($filter) && strpos($filter, '!') === 0) {
if (is_string($filter) && strncmp($filter, '!', 1) === 0) {
$excludeFilters[] = substr($filter, 1);
continue;
}

6
framework/helpers/BaseFileHelper.php

@ -697,7 +697,7 @@ class BaseFileHelper
private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags)
{
// match with FNM_PATHNAME; the pattern has base implicitly in front of it.
if (strpos($pattern, '/') === 0) {
if (strncmp($pattern, '/', 1) === 0) {
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
if ($firstWildcard !== false && $firstWildcard !== 0) {
$firstWildcard--;
@ -807,7 +807,7 @@ class BaseFileHelper
return $result;
}
if (strpos($pattern, '!') === 0) {
if (strncmp($pattern, '!', 1) === 0) {
$result['flags'] |= self::PATTERN_NEGATIVE;
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
}
@ -819,7 +819,7 @@ class BaseFileHelper
$result['flags'] |= self::PATTERN_NODIR;
}
$result['firstWildcard'] = self::firstWildcardInPattern($pattern);
if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) {
if (strncmp($pattern, '*', 1) === 0 && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) {
$result['flags'] |= self::PATTERN_ENDSWITH;
}
$result['pattern'] = $pattern;

2
framework/helpers/BaseUrl.php

@ -248,7 +248,7 @@ class BaseUrl
return $url;
}
if (strpos($url, '//') === 0) {
if (strncmp($url, '//', 2) === 0) {
// e.g. //example.com/path/to/resource
return $scheme === '' ? $url : "$scheme:$url";
}

2
framework/i18n/MessageFormatter.php

@ -423,7 +423,7 @@ class MessageFormatter extends Component
$selector = trim(mb_substr($selector, $pos + 1, mb_strlen($selector, $charset), $charset));
}
if ($message === false && $selector === 'other' ||
strpos($selector, '=') === 0 && (int) mb_substr($selector, 1, mb_strlen($selector, $charset), $charset) === $arg ||
strncmp($selector, '=', 1) === 0 && (int) mb_substr($selector, 1, mb_strlen($selector, $charset), $charset) === $arg ||
$selector === 'one' && $arg - $offset == 1
) {
$message = implode(',', str_replace('#', $arg - $offset, $plural[$i]));

2
framework/log/Logger.php

@ -196,7 +196,7 @@ class Logger extends Component
if (is_int($index)) {
$messages[] = $message;
} else {
if (strpos($index, 'begin-') === 0) {
if (strncmp($index, 'begin-', 6) === 0) {
$oppositeProfile = 'end-' . substr($index, 6);
} else {
$oppositeProfile = 'begin-' . substr($index, 4);

2
framework/rest/CreateAction.php

@ -52,7 +52,7 @@ class CreateAction extends Action
if ($model->save()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
$id = implode(',', array_values($model->getPrimaryKey(true)));
$id = implode(',', $model->getPrimaryKey(true));
$response->getHeaders()->set('Location', Url::toRoute([$this->viewAction, 'id' => $id], true));
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');

6
framework/validators/UniqueValidator.php

@ -191,10 +191,10 @@ class UniqueValidator extends Validator
// only select primary key to optimize query
$columnsCondition = array_flip($targetClass::primaryKey());
$query->select(array_flip($this->applyTableAlias($query, $columnsCondition)));
// any with relation can't be loaded because related fields are not selected
$query->with = null;
if (is_array($query->joinWith)) {
// any joinWiths need to have eagerLoading turned off to prevent related fields being loaded
foreach ($query->joinWith as &$joinWith) {
@ -321,7 +321,7 @@ class UniqueValidator extends Validator
foreach ($conditions as $columnName => $columnValue) {
if (strpos($columnName, '(') === false) {
$columnName = preg_replace('/^' . preg_quote($alias) . '\.(.*)$/', '$1', $columnName);
if (strpos($columnName, '[[') === 0) {
if (strncmp($columnName, '[[', 2) === 0) {
$prefixedColumn = "{$alias}.{$columnName}";
} else {
$prefixedColumn = "{$alias}.[[{$columnName}]]";

2
framework/widgets/Menu.php

@ -306,7 +306,7 @@ class Menu extends Widget
{
if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
$route = Yii::getAlias($item['url'][0]);
if (strpos($route, '/') !== 0 && Yii::$app->controller) {
if (strncmp($route, '/', 1) !== 0 && Yii::$app->controller) {
$route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
}
if (ltrim($route, '/') !== $this->route) {

Loading…
Cancel
Save