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

Use in_array instead of array_search + comparison #24886

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/code/Magento/Analytics/Model/Cryptographer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ private function validateCipherMethod($cipherMethod)
);
$cipherMethod = strtolower($cipherMethod);

return (false !== array_search($cipherMethod, $methods));
return in_array($cipherMethod, $methods);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to replace both array_search & in_array to array_flip + isset.
Reason: magento/magento-coding-standard#143

@carusogabriel @osrecio what do you think about it?

Copy link
Member

Choose a reason for hiding this comment

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

Followin the thread I think is better to usee array_flip + isset.

If @carusogabriel agree with us, he can perform the changes and for me will be ready to be merged :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@osrecio I'll do it :)

}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Backend/Model/Menu/Item/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function assertContainsRequiredParameters($data)
*/
private function assertIdentifierIsNotUsed($id)
{
if (array_search($id, $this->_ids) !== false) {
if (in_array($id, $this->_ids)) {
throw new \InvalidArgumentException('Item with id ' . $id . ' already exists');
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Cms/Model/ResourceModel/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function getIsUniqueBlockToStores(AbstractModel $object)

$stores = (array)$object->getData('store_id');
$isDefaultStore = $this->_storeManager->isSingleStoreMode()
|| array_search(Store::DEFAULT_STORE_ID, $stores) !== false;
|| in_array(Store::DEFAULT_STORE_ID, $stores);

if (!$isDefaultStore) {
$stores[] = Store::DEFAULT_STORE_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private function getDependentIndexerIds(string $indexerId)
$dependentIndexerIds = [];
foreach (array_keys($this->getConfig()->getIndexers()) as $id) {
$dependencies = $this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($id);
if (array_search($indexerId, $dependencies) !== false) {
if (in_array($indexerId, $dependencies)) {
$dependentIndexerIds = array_merge(
$dependentIndexerIds,
[$id],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function processAssert(
$availableAttributes = $advancedSearch->getForm()->getFormLabels();
if (isset($attributeForSearch['isVisible'])) {
\PHPUnit\Framework\Assert::assertTrue(
(false !== array_search($attributeForSearch['name'], $availableAttributes)),
(in_array($attributeForSearch['name'], $availableAttributes)),
'Attribute ' . $attributeForSearch['name'] . 'was not found in Advanced Search Page.'
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private function getModuleActionsMapping(string $module, string $routerId, array
{
$subdirectoryPattern = str_replace('\\', DIRECTORY_SEPARATOR, $module);
$subdirectoryPattern .= DIRECTORY_SEPARATOR . 'Controller/';
if (array_search($routerId, [Area::AREA_ADMINHTML, Area::AREA_ADMIN], true) !== false) {
if (in_array($routerId, [Area::AREA_ADMINHTML, Area::AREA_ADMIN], true)) {
$subdirectoryPattern .= ucfirst(Area::AREA_ADMINHTML) . DIRECTORY_SEPARATOR;
} else {
$subdirectoryPattern .= '(?!' . ucfirst(Area::AREA_ADMINHTML) . ')';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private function filterSelfDependency(string $moduleName, array $dependencies):a
$decodedId = $this->decodeDependencyId($id);
$entityType = $decodedId['entityType'];
if ($entityType === self::SCHEMA_ENTITY_TABLE || $entityType === "column") {
if (array_search($moduleName, $modules) !== false) {
if (in_array($moduleName, $modules)) {
unset($dependencies[$id]);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Data/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ protected function _insertChild($targetParentId, $elementId, $offset, $alias)
)
);
}
if (false !== array_search($alias, $children)) {
if (in_array($alias, $children)) {
throw new LocalizedException(
new \Magento\Framework\Phrase(
'The element "%1" can\'t have a child because "%1" already has a child with alias "%2".',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getIndexerIdsToRunAfter(string $indexerId): array
$this->getIndexerDataWithValidation($indexerId);
$result = [];
foreach ($this->config->getIndexers() as $id => $indexerData) {
if (array_search($indexerId, $indexerData['dependencies']) !== false) {
if (in_array($indexerId, $indexerData['dependencies'])) {
$result[] = $id;
}
}
Expand Down