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

Rector: CQ - SimplifyIfReturnBoolRector #4152

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

sreichel
Copy link
Contributor

@sreichel sreichel commented Aug 12, 2024

@github-actions github-actions bot added environment composer Relates to composer.json ddev labels Aug 12, 2024
@github-actions github-actions bot added Component: PayPal Relates to Mage_Paypal Component: Core Relates to Mage_Core Component: Catalog Relates to Mage_Catalog Component: Cms Relates to Mage_Cms Component: CatalogInventory Relates to Mage_CatalogInventory Component: Checkout Relates to Mage_Checkout Component: lib/Varien Relates to lib/Varien Component: Sales Relates to Mage_Sales Component: Customer Relates to Mage_Customer Component: Eav Relates to Mage_Eav Component: lib/Mage Relates to lib/Mage Component: Adminhtml Relates to Mage_Adminhtml labels Aug 12, 2024
Comment on lines -591 to +585
if (!$this->canShipPartiallyItem() || !$this->canShipPartially()) {
return false;
}
return true;
return !(!$this->canShipPartiallyItem() || !$this->canShipPartially());
Copy link
Contributor

@kiatng kiatng Aug 12, 2024

Choose a reason for hiding this comment

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

Apply DeMorgan's Law: !(A || B) == (!A && !B)

Suggested change
if (!$this->canShipPartiallyItem() || !$this->canShipPartially()) {
return false;
}
return true;
return !(!$this->canShipPartiallyItem() || !$this->canShipPartially());
return $this->canShipPartiallyItem() && $this->canShipPartially();

if (!is_null($value) && !$value) {
return false;
}
return true;
return !(!is_null($value) && !$value);
Copy link
Contributor

Choose a reason for hiding this comment

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

DeMorgan's Law !(A && B) == (!A || !B):

Suggested change
if (!is_null($value) && !$value) {
return false;
}
return true;
return !(!is_null($value) && !$value);
return is_null($value) || $value;

Comment on lines -398 to +399

if (!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| !hash_equals(Mage::getSingleton('adminhtml/url')->getSecretKey(), $secretKey)
) {
return false;
}
return true;
return !(!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| !hash_equals(Mage::getSingleton('adminhtml/url')->getSecretKey(), $secretKey));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| !hash_equals(Mage::getSingleton('adminhtml/url')->getSecretKey(), $secretKey)
) {
return false;
}
return true;
return !(!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| !hash_equals(Mage::getSingleton('adminhtml/url')->getSecretKey(), $secretKey));
return ($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
&& hash_equals(Mage::getSingleton('adminhtml/url')->getSecretKey(), $secretKey);

Copy link
Contributor

Choose a reason for hiding this comment

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

Continue on the conversation on chaining rector rules, I don't like the idea that it requires another PR to clean up the code above. This example from rector is really bad. It should be clean up before committing. Don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think rector did right. With that change it was possible to make use of DeMorgans law, that's another rule.

It's no must to have only one rule per PR, but I'd like to keep it as simple as possible to review.

DeMorgans rule added, let's wait for you bugreport.

Copy link
Contributor Author

@sreichel sreichel Aug 12, 2024

Choose a reason for hiding this comment

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

I'd prefer to revert last commit and add the DeMorgans rule separately.

Let's keep it simple. It affects some more files and makes reviews harder.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried to apply the DeMorgan's rule to !(!is_null($value) && !$value), not sure if I made the test correctly here: https://getrector.com/demo/6a07c84e-3a20-45c6-9513-d20329994eb8

Did I make a mistake in the test? It doesn't work, no code change. If the above test is correct and if this PR is committed, there is no guarantee the convoluted code can be cleaned up by the rule later. So, now what?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, so rector won't fix the convoluted bool expression now:
image

So, should we apply DeMorgan's manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

392d0a8

No. I'd take it as it is and apply that rule later. I dont think its worth to change it manually, same pattern is already there.

@github-actions github-actions bot added the errors Relates to error pages label Aug 15, 2024
# Conflicts:
#	dev/tests/functional/tests/app/Mage/Adminhtml/Test/Block/Widget/Grid.php
#	dev/tests/functional/tests/app/Mage/Paypal/Test/Block/Login.php
#	dev/tests/functional/tests/app/Mage/Paypal/Test/Block/NewLogin.php
# Conflicts:
#	composer.lock
#	errors/report.php
@github-actions github-actions bot removed the errors Relates to error pages label Sep 9, 2024
@sreichel sreichel requested a review from kiatng September 9, 2024 19:30
@sreichel sreichel marked this pull request as draft September 17, 2024 10:14
# Conflicts:
#	.github/workflows/check-files.yml
#	composer.lock
@sreichel sreichel marked this pull request as ready for review September 17, 2024 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Admin Relates to Mage_Admin Component: Adminhtml Relates to Mage_Adminhtml Component: Api2 Relates to Mage_Api2 Component: Authorizenet Relates to Mage_Authorizenet Component: Bundle Relates to Mage_Bundle Component: Catalog Relates to Mage_Catalog Component: CatalogIndex Relates to Mage_CatalogIndex Component: CatalogInventory Relates to Mage_CatalogInventory Component: CatalogSearch Relates to Mage_CatalogSearch Component: Centinel Relates to Mage_Centinel Component: Checkout Relates to Mage_Checkout Component: Cms Relates to Mage_Cms Component: Core Relates to Mage_Core Component: Cron Relates to Mage_Cron Component: Customer Relates to Mage_Customer Component: Downloadable Relates to Mage_Downloadable Component: Eav Relates to Mage_Eav Component: lib/Mage Relates to lib/Mage Component: lib/Magento Relates to lib/Magento Component: lib/Varien Relates to lib/Varien Component: lib/* Relates to lib/* Component: Media Relates to Mage_Media Component: Newsletter Relates to Mage_Newsletter Component: Paygate Relates to Mage_Paygate Component: Payment Relates to Mage_Payment Component: PayPal Relates to Mage_Paypal Component: ProductAlert Relates to Mage_ProductAlert Component: Rss Relates to Mage_Rss Component: Sales Relates to Mage_Sales Component: SalesRule Relates to Mage_SalesRule Component: Tax Relates to Mage_Tax Component: Widget Relates to Mage_Widget Component: Wishlist Relates to Mage_Wishlist composer Relates to composer.json ddev environment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants