-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Codacy warning fixes #8949
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
Codacy warning fixes #8949
Changes from all commits
a229c1a
fb3b044
170ced4
93be9bd
33236df
4c47b96
00e1da7
fccf34b
eab05d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,11 @@ | |
|
||
class Save extends \Magento\Sales\Controller\Adminhtml\Order\Status | ||
{ | ||
/** | ||
* @var \Magento\Backend\Model\View\Result\Redirect | ||
*/ | ||
private $resultRedirect; | ||
|
||
/** | ||
* Save status form processing | ||
* | ||
|
@@ -17,8 +22,7 @@ public function execute() | |
{ | ||
$data = $this->getRequest()->getPostValue(); | ||
$isNew = $this->getRequest()->getParam('is_new'); | ||
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ | ||
$resultRedirect = $this->resultRedirectFactory->create(); | ||
$this->resultRedirect = $this->resultRedirectFactory->create(); | ||
if ($data) { | ||
$statusCode = $this->getRequest()->getParam('status'); | ||
|
||
|
@@ -37,35 +41,55 @@ public function execute() | |
$label = $filterManager->stripTags($label); | ||
} | ||
|
||
$status = $this->_objectManager->create(\Magento\Sales\Model\Order\Status::class)->load($statusCode); | ||
// check if status exist | ||
if ($isNew && $status->getStatus()) { | ||
$this->messageManager->addError(__('We found another order status with the same order status code.')); | ||
$this->_getSession()->setFormData($data); | ||
return $resultRedirect->setPath('sales/*/new'); | ||
} | ||
|
||
$status->setData($data)->setStatus($statusCode); | ||
|
||
try { | ||
$status->save(); | ||
$this->messageManager->addSuccess(__('You saved the order status.')); | ||
return $resultRedirect->setPath('sales/*/'); | ||
} catch (\Magento\Framework\Exception\LocalizedException $e) { | ||
$this->messageManager->addError($e->getMessage()); | ||
} catch (\Exception $e) { | ||
$this->messageManager->addException( | ||
$e, | ||
__('We can\'t add the order status right now.') | ||
); | ||
if ($this->updateStatus($isNew, $data, $statusCode) !== false) { | ||
return $this->resultRedirect; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such logic seems quite confusing to me. Random piece of code is moved to a separate method which may return boolean or object :) Please change method name to |
||
} | ||
|
||
$this->_getSession()->setFormData($data); | ||
if ($isNew) { | ||
return $resultRedirect->setPath('sales/*/new'); | ||
} else { | ||
return $resultRedirect->setPath('sales/*/edit', ['status' => $this->getRequest()->getParam('status')]); | ||
return $this->resultRedirect->setPath('sales/*/new'); | ||
} | ||
return $this->resultRedirect->setPath( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two returns is not a good practice. Simply change to
|
||
'sales/*/edit', | ||
['status' => $this->getRequest()->getParam('status')] | ||
); | ||
} | ||
return $this->resultRedirect->setPath('sales/*/'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where this new return comes from? |
||
} | ||
|
||
/** | ||
* Update the order status | ||
* | ||
* @param bool $isNew | ||
* @param array $data | ||
* @param string $statusCode | ||
* @return bool | ||
*/ | ||
private function updateStatus($isNew, $data, $statusCode) | ||
{ | ||
$status = $this->_objectManager->create(\Magento\Sales\Model\Order\Status::class)->load($statusCode); | ||
// check if status exist | ||
if ($isNew && $status->getStatus()) { | ||
$this->messageManager->addError(__('We found another order status with the same order status code.')); | ||
$this->_getSession()->setFormData($data); | ||
return $this->resultRedirect->setPath('sales/*/new'); | ||
} | ||
|
||
$status->setData($data)->setStatus($statusCode); | ||
|
||
try { | ||
$status->save(); | ||
$this->messageManager->addSuccess(__('You saved the order status.')); | ||
return $this->resultRedirect->setPath('sales/*/'); | ||
} catch (\Magento\Framework\Exception\LocalizedException $e) { | ||
$this->messageManager->addError($e->getMessage()); | ||
} catch (\Exception $e) { | ||
$this->messageManager->addException( | ||
$e, | ||
__('We can\'t add the order status right now.') | ||
); | ||
} | ||
return $resultRedirect->setPath('sales/*/'); | ||
|
||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,20 +75,33 @@ public function validate(DesignConfigInterface $designConfig) | |
} | ||
$text = $template->getTemplateText(); | ||
$template->revertDesign(); | ||
// Check if template body has a reference to the same config path | ||
if (preg_match_all(Template::CONSTRUCTION_TEMPLATE_PATTERN, $text, $constructions, PREG_SET_ORDER)) { | ||
foreach ($constructions as $construction) { | ||
$configPath = isset($construction[2]) ? $construction[2] : ''; | ||
$params = $this->getParameters($configPath); | ||
if (isset($params['config_path']) && $params['config_path'] == $data['config_path']) { | ||
throw new LocalizedException( | ||
__( | ||
"The %templateName contains an incorrect configuration. The template has " . | ||
"a reference to itself. Either remove or change the reference.", | ||
["templateName" => $name] | ||
) | ||
); | ||
}; | ||
$this->checkTemplateConfiguration($data, $text, $name); | ||
} | ||
} | ||
|
||
/** | ||
* Check if template body has a reference to the same config path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty logical change 👍 |
||
* | ||
* @param array $data | ||
* @param string $text | ||
* @param string $name | ||
* @throws LocalizedException | ||
* @return void | ||
*/ | ||
private function checkTemplateConfiguration($data, $text, $name) | ||
{ | ||
if (preg_match_all(Template::CONSTRUCTION_TEMPLATE_PATTERN, $text, $constructions, PREG_SET_ORDER)) { | ||
foreach ($constructions as $construction) { | ||
$configPath = isset($construction[2]) ? $construction[2] : ''; | ||
$params = $this->getParameters($configPath); | ||
if (isset($params['config_path']) && $params['config_path'] == $data['config_path']) { | ||
throw new LocalizedException( | ||
__( | ||
"The %templateName contains an incorrect configuration. The template has " . | ||
"a reference to itself. Either remove or change the reference.", | ||
["templateName" => $name] | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why need this instead of local variable?