Skip to content

Commit

Permalink
Replaced strpos() calls with PHP8 string functions (#3258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Reichel committed May 30, 2023
1 parent 4ea7dce commit 1f54fc7
Show file tree
Hide file tree
Showing 75 changed files with 110 additions and 110 deletions.
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Report/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public function addGrandTotals($total)
* recalc totals if we have average
*/
foreach ($this->getColumns() as $key => $_column) {
if (strpos($_column->getTotal(), '/') !== false) {
if (str_contains($_column->getTotal(), '/')) {
list($t1, $t2) = explode('/', $_column->getTotal());
if ($this->getGrandTotals()->getData($t2) != 0) {
$this->getGrandTotals()->setData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getFormValues()
{
$data = $this->getCustomer()->getData();
foreach ($this->getQuote()->getData() as $key => $value) {
if (strpos($key, 'customer_') === 0) {
if (str_starts_with($key, 'customer_')) {
$data[substr($key, 9)] = $value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Urlrewrite/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getButtonsHtml($area = null)
if ($this->_buttonsHtml === null) {
$this->_buttonsHtml = parent::getButtonsHtml();
foreach ($this->_children as $alias => $child) {
if (strpos($alias, '_button') !== false) {
if (str_contains($alias, '_button')) {
$this->unsetChild($alias);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Helper/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Mage_Adminhtml_Helper_Js extends Mage_Core_Helper_Js
*/
public function decodeGridSerializedInput($encoded)
{
$isSimplified = (strpos($encoded, '=') === false);
$isSimplified = !str_contains($encoded, '=');
$result = [];
parse_str($encoded, $decoded);
foreach ($decoded as $key => $value) {
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ protected function _parseOptions(Mage_Sales_Model_Quote_Item $item, $additionalO
foreach (explode("\n", $additionalOptions) as $_additionalOption) {
if (strlen(trim($_additionalOption))) {
try {
if (strpos($_additionalOption, ':') === false) {
if (!str_contains($_additionalOption, ':')) {
Mage::throwException(
Mage::helper('adminhtml')->__('There is an error in one of the option rows.')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public function deleteFilesAction()
foreach ($files as $file) {
$file = $helper->idDecode($file);
$_filePath = realpath($path . DS . $file);
if (strpos($_filePath, realpath($path)) === 0 &&
strpos($_filePath, realpath($helper->getStorageRoot())) === 0
if (str_starts_with($_filePath, realpath($path)) &&
str_starts_with($_filePath, realpath($helper->getStorageRoot()))
) {
$this->getStorage()->deleteFile($path . DS . $file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function _getCollectionNames()
throw new Exception(Mage::helper('adminhtml')->__('No report code specified.'));
}

if (!is_array($codes) && strpos($codes, ',') === false) {
if (!is_array($codes) && !str_contains($codes, ',')) {
$codes = [$codes];
} elseif (!is_array($codes)) {
$codes = explode(',', $codes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function uploadPostAction()
public function downloadAction()
{
$filename = $this->getRequest()->getParam('filename');
if (!$filename || strpos($filename, '..') !== false || $filename[0] === '.') {
if (!$filename || str_contains($filename, '..') || $filename[0] === '.') {
return;
}
$this->_initProfile();
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Api/Model/Server/V2/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __call($function, $args)
$nodes = Mage::getSingleton('api/config')->getNode('v2/resources_function_prefix')->children();
foreach ($nodes as $resource => $prefix) {
$prefix = $prefix->asArray();
if (strpos($function, $prefix) !== false) {
if (str_contains($function, $prefix)) {
$method = substr($function, strlen($prefix));
$apiKey = $resource . '.' . strtolower($method[0]) . substr($method, 1);
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Api/Model/Server/Wsi/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __call($function, $args)
$nodes = Mage::getSingleton('api/config')->getNode('v2/resources_function_prefix')->children();
foreach ($nodes as $resource => $prefix) {
$prefix = $prefix->asArray();
if (strpos($function, $prefix) !== false) {
if (str_contains($function, $prefix)) {
$method = substr($function, strlen($prefix));
$apiKey = $resource . '.' . strtolower($method[0]) . substr($method, 1);
}
Expand Down
10 changes: 5 additions & 5 deletions app/code/core/Mage/Api2/Model/Acl/Global/Rule/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ public function getPostResources()
$prefixPrivilege = self::NAME_PRIVILEGE . self::ID_SEPARATOR;
$nameResource = null;
foreach ($checkedResources as $i => $item) {
if (strpos($item, $prefixResource) === 0) {
if (str_starts_with($item, $prefixResource)) {
$nameResource = substr($item, mb_strlen($prefixResource, 'UTF-8'));
$resources[$nameResource] = [];
} elseif (strpos($item, $prefixPrivilege) === 0) {
} elseif (str_starts_with($item, $prefixPrivilege)) {
$name = substr($item, mb_strlen($prefixPrivilege, 'UTF-8'));
$namePrivilege = str_replace($nameResource . self::ID_SEPARATOR, '', $name);
$resources[$nameResource][$namePrivilege] = $allow;
Expand All @@ -201,14 +201,14 @@ public function getPostResources()
$prefixAttribute = self::NAME_ATTRIBUTE . self::ID_SEPARATOR;
$nameResource = null;
foreach ($checkedResources as $i => $item) {
if (strpos($item, $prefixResource) === 0) {
if (str_starts_with($item, $prefixResource)) {
$nameResource = substr($item, mb_strlen($prefixResource, 'UTF-8'));
$resources[$nameResource] = [];
} elseif (strpos($item, $prefixOperation) === 0) {
} elseif (str_starts_with($item, $prefixOperation)) {
$name = substr($item, mb_strlen($prefixOperation, 'UTF-8'));
$operationName = str_replace($nameResource . self::ID_SEPARATOR, '', $name);
$resources[$nameResource][$operationName] = [];
} elseif (strpos($item, $prefixAttribute) === 0) {
} elseif (str_starts_with($item, $prefixAttribute)) {
$name = substr($item, mb_strlen($prefixOperation, 'UTF-8'));
$attributeName = str_replace(
$nameResource . self::ID_SEPARATOR . $operationName . self::ID_SEPARATOR,
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Model/Category/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function move($categoryId, $parentId, $afterId = null)
$afterId = array_pop(explode(',', $parentChildren));
}

if (strpos($parent_category->getPath(), $category->getPath()) === 0) {
if (str_starts_with($parent_category->getPath(), $category->getPath())) {
$this->_fault('not_moved', "Operation do not allow to move a parent category to any of children category");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function addImage(
$move = false,
$exclude = true
) {
if (strpos($file, chr(0)) !== false
if (str_contains($file, chr(0))
|| preg_match('#(^|[\\\\/])\.\.($|[\\\\/])#', $file)
) {
throw new Exception('Detected malicious path or filename input.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function _getSelect($filter)
$wherePart = $select->getPart(Zend_Db_Select::WHERE);
$excludedWherePart = Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS . '.status';
foreach ($wherePart as $key => $wherePartItem) {
if (strpos($wherePartItem, $excludedWherePart) !== false) {
if (str_contains($wherePartItem, $excludedWherePart)) {
$wherePart[$key] = new Zend_Db_Expr('1=1');
continue;
}
Expand All @@ -121,7 +121,7 @@ protected function _getSelect($filter)
$select->setPart(Zend_Db_Select::WHERE, $wherePart);
$excludeJoinPart = Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS . '.entity_id';
foreach ($priceIndexJoinConditions as $condition) {
if (strpos($condition, $excludeJoinPart) !== false) {
if (str_contains($condition, $excludeJoinPart)) {
continue;
}
$select->where($this->_replaceTableAlias($condition));
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function storeDirective($construction)
$params['_query'] = [];
}
foreach ($params as $k => $v) {
if (strpos($k, '_query_') === 0) {
if (str_starts_with($k, '_query_')) {
$params['_query'][substr($k, 7)] = $v;
unset($params[$k]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Catalog/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public function getUnusedPath($storeId, $requestPath, $idPath)
*/
public function getUnusedPathByUrlKey($storeId, $requestPath, $idPath, $urlKey)
{
if (strpos($idPath, 'product') !== false) {
if (str_contains($idPath, 'product')) {
$suffix = $this->getProductUrlSuffix($storeId);
} else {
$suffix = $this->getCategoryUrlSuffix($storeId);
Expand Down Expand Up @@ -805,7 +805,7 @@ public function getProductRequestPath($product, $category)
* Check if existing request past can be used
*/
if ($product->getUrlKey() == '' && !empty($requestPath)
&& strpos($existingRequestPath, $requestPath) === 0
&& str_starts_with($existingRequestPath, $requestPath)
) {
$existingRequestPath = preg_replace(
'/^' . preg_quote($requestPath, '/') . '/',
Expand Down
10 changes: 5 additions & 5 deletions app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function deleteDirectory($path)
$io->getFilteredPath($path)
));
}
if (strpos($pathCmp, chr(0)) !== false
if (str_contains($pathCmp, chr(0))
|| preg_match('#(^|[\\\\/])\.\.($|[\\\\/])#', $pathCmp)
) {
throw new Exception('Detected malicious path or filename input.');
Expand All @@ -239,7 +239,7 @@ public function deleteDirectory($path)
Mage::throwException(Mage::helper('cms')->__('Cannot delete directory %s.', $io->getFilteredPath($path)));
}

if (strpos($pathCmp, $rootCmp) === 0) {
if (str_starts_with($pathCmp, $rootCmp)) {
$io->rmdir($this->getThumbnailRoot() . DS . ltrim(substr($pathCmp, strlen($rootCmp)), '\\/'), true);
}
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public function getThumbnailPath($filePath, $checkFile = false)
{
$mediaRootDir = $this->getHelper()->getStorageRoot();

if (strpos($filePath, $mediaRootDir) === 0) {
if (str_starts_with($filePath, $mediaRootDir)) {
$thumbPath = $this->getThumbnailRoot() . DS . substr($filePath, strlen($mediaRootDir));

if (!$checkFile || is_readable($thumbPath)) {
Expand All @@ -340,7 +340,7 @@ public function getThumbnailPath($filePath, $checkFile = false)
public function getThumbnailUrl($filePath, $checkFile = false)
{
$mediaRootDir = Mage::getConfig()->getOptions()->getMediaDir() . DS;
if (strpos($filePath, $mediaRootDir) === 0) {
if (str_starts_with($filePath, $mediaRootDir)) {
$thumbSuffix = self::THUMBS_DIRECTORY_NAME . DS . substr($filePath, strlen($mediaRootDir));
if (!$checkFile || is_readable($this->getHelper()->getStorageRoot() . $thumbSuffix)) {
$randomIndex = '?rand=' . time();
Expand Down Expand Up @@ -420,7 +420,7 @@ public function getThumbsPath($filePath = false)
$mediaRootDir = Mage::getConfig()->getOptions()->getMediaDir();
$thumbnailDir = $this->getThumbnailRoot();

if ($filePath && strpos($filePath, $mediaRootDir) === 0) {
if ($filePath && str_starts_with($filePath, $mediaRootDir)) {
$thumbnailDir .= DS . dirname(substr($filePath, strlen($mediaRootDir)));
}

Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Core/Block/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function assign($key, $value = null)
*/
public function setScriptPath($dir)
{
if (strpos($dir, '..') === false && ($dir === Mage::getBaseDir('design') || strpos(realpath($dir), realpath(Mage::getBaseDir('design'))) === 0)) {
if (!str_contains($dir, '..') && ($dir === Mage::getBaseDir('design') || str_starts_with(realpath($dir), realpath(Mage::getBaseDir('design'))))) {
$this->_viewDir = $dir;
} else {
Mage::log('Not valid script path:' . $dir, Zend_Log::CRIT, null, true);
Expand Down Expand Up @@ -245,7 +245,7 @@ public function fetchView($fileName)
try {
if (strpos($this->_viewDir . DS . $fileName, '..') === false
&&
($this->_viewDir == Mage::getBaseDir('design') || strpos(realpath($this->_viewDir), realpath(Mage::getBaseDir('design'))) === 0)
($this->_viewDir == Mage::getBaseDir('design') || str_starts_with(realpath($this->_viewDir), realpath(Mage::getBaseDir('design'))))
) {
include $this->_viewDir . DS . $fileName;
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Controller/Request/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function getHttpHost($trimPort = true)
$host = $hostParts[0];
}

if (strpos($host, ',') !== false || strpos($host, ';') !== false) {
if (str_contains($host, ',') || str_contains($host, ';')) {
$response = new Zend_Controller_Response_Http();
$response->setHttpResponseCode(400)->sendHeaders();
exit();
Expand Down
6 changes: 3 additions & 3 deletions app/code/core/Mage/Core/Controller/Varien/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,12 @@ protected function _getRefererUrl()
*/
protected function _isUrlInternal($url)
{
if (strpos($url, 'http') !== false) {
if (str_contains($url, 'http')) {
/**
* Url must start from base secure or base unsecure url
*/
if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) === 0)
|| (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) === 0)
if (str_starts_with($url, Mage::app()->getStore()->getBaseUrl())
|| str_starts_with($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true))
) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function getControllerFileName($realModule, $controller)
*/
public function validateControllerFileName($fileName)
{
if ($fileName && is_readable($fileName) && strpos($fileName, '//') === false) {
if ($fileName && is_readable($fileName) && !str_contains($fileName, '//')) {
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Helper/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getRemoteAddr($ipToLong = false)
return false;
}

if (strpos($this->_remoteAddr, ',') !== false) {
if (str_contains($this->_remoteAddr, ',')) {
$ipList = explode(',', $this->_remoteAddr);
$this->_remoteAddr = trim(reset($ipList));
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Helper/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function parseQueryStr($str)
*/
protected function _validateQueryStr($str)
{
if (!$str || (strpos($str, '=') === false)) {
if (!$str || !str_contains($str, '=')) {
return false;
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Core/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ public function decodePunycode($url)
*/
private function _isPunycode($host)
{
if (strpos($host, 'xn--') === 0 || strpos($host, '.xn--') !== false
|| strpos($host, 'XN--') === 0 || strpos($host, '.XN--') !== false
if (str_starts_with($host, 'xn--') || str_contains($host, '.xn--')
|| str_starts_with($host, 'XN--') || str_contains($host, '.XN--')
) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Helper/Url/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function _validateRequestPath($requestPath)
$this->__('Request path length exceeds allowed %s symbols.', self::TARGET_PATH_ALLOWED_LENGTH)
);
}
if (strpos($requestPath, '//') !== false) {
if (str_contains($requestPath, '//')) {
throw new Mage_Core_Exception(
$this->__('Two and more slashes together are not permitted in request path'),
self::VERR_MANYSLASHES
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Model/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ public function isCurrentlySecure()
$offloaderHeader = strtoupper(trim((string) Mage::getConfig()->getNode(Mage_Core_Model_Store::XML_PATH_OFFLOADER_HEADER, 'default')));
if ($offloaderHeader) {
$offloaderHeader = preg_replace('/[^A-Z]+/', '_', $offloaderHeader);
$offloaderHeader = strpos($offloaderHeader, 'HTTP_') === 0 ? $offloaderHeader : 'HTTP_' . $offloaderHeader;
$offloaderHeader = str_starts_with($offloaderHeader, 'HTTP_') ? $offloaderHeader : 'HTTP_' . $offloaderHeader;
if (!empty($_SERVER[$offloaderHeader]) && $_SERVER[$offloaderHeader] !== 'http') {
return true;
}
Expand Down
10 changes: 5 additions & 5 deletions app/code/core/Mage/Core/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ public function getGroupedClassName($groupType, $classId, $groupRootNode = null)
*/
public function getBlockClassName($blockType)
{
if (strpos($blockType, '/') === false) {
if (!str_contains($blockType, '/')) {
return $blockType;
}
return $this->getGroupedClassName('block', $blockType);
Expand All @@ -1376,7 +1376,7 @@ public function getBlockClassName($blockType)
*/
public function getHelperClassName($helperName)
{
if (strpos($helperName, '/') === false) {
if (!str_contains($helperName, '/')) {
$helperName .= '/data';
}
return $this->getGroupedClassName('helper', $helperName);
Expand Down Expand Up @@ -1413,7 +1413,7 @@ public function getResourceHelper($moduleName)
public function getModelClassName($modelClass)
{
$modelClass = trim($modelClass);
if (strpos($modelClass, '/') === false) {
if (!str_contains($modelClass, '/')) {
return $modelClass;
}
return $this->getGroupedClassName('model', $modelClass);
Expand Down Expand Up @@ -1583,15 +1583,15 @@ public function shouldUrlBeSecure($url)
}

// If unsecure base url is https, then all urls should be secure
if (strpos(Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_UNSECURE_BASE_URL), 'https://') === 0) {
if (str_starts_with(Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_UNSECURE_BASE_URL), 'https://')) {
return true;
}

if (!isset($this->_secureUrlCache[$url])) {
$this->_secureUrlCache[$url] = false;
$secureUrls = $this->getNode('frontend/secure_url');
foreach ($secureUrls->children() as $match) {
if (strpos($url, (string)$match) === 0) {
if (str_starts_with($url, (string)$match)) {
$this->_secureUrlCache[$url] = true;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Model/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct()
*/
public function getClassNameByType($type)
{
if (strpos($type, '/') !== false) {
if (str_contains($type, '/')) {
return Mage::getConfig()->getModelClassName($type);
}
return parent::getClassNameByType($type);
Expand Down
Loading

0 comments on commit 1f54fc7

Please sign in to comment.