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

Mage Factory Methods: Direct "_registry" Access Without Method Call #2319

Merged
merged 9 commits into from
Jul 20, 2022
Merged

Mage Factory Methods: Direct "_registry" Access Without Method Call #2319

merged 9 commits into from
Jul 20, 2022

Conversation

Sdfendor
Copy link
Contributor

@Sdfendor Sdfendor commented Jul 15, 2022

Description (*)

Four factory methods in the main Mage class use the registry() method to retrieve object instances they created prior. To use this method in those cases is unnecessary and counterproductive. This PR adjusts those methods to use self::$_registry directly.
In addition small formatting fixes are applied and one null coalescing replaces a "if/isset" construct. Due to a changed return type of getSingleton() a few type hints (with explanation) are added as well to satisfy PHPStan static analysis.

Questions or comments

  1. Why can the registry() calls be replaced?
    • The method accesses self::$_registry while checking if given key exists and is not null. In each adjusted case however it is guaranteed by the code executed prior that the used $registryKey is not null. Take helper() for example:

      image
      The value of self::$_registry['registryKey'] is either an object instance (the helper) or a fatal error is emitted because class name in $helperClass is not found. Null isn't possible and the registry value is written.

  2. Why is replacement useful?
    • A call to another method is saved which only additional use is another (in this case unnecessary) isset check. More importantly though there are inspections (namely in the popular plugin Php Inspections ​(EA Extended))​ that look at return values of methods and check if null is possible. In those cases (and if the returned value is an object and its methods are called) the inspection warns and suggests usage of optional chaining (a feature of PHP 8.1). Those false positives can be avoided by this change.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

Direct access is possible because null is no possibility due to executed code prior. This avoids additional isset checks being unnecessary.
@github-actions github-actions bot added the Mage.php Relates to app/Mage.php label Jul 15, 2022
app/Mage.php Outdated Show resolved Hide resolved
@github-actions github-actions bot added the Component: Adminhtml Relates to Mage_Adminhtml label Jul 19, 2022
…Mage::getSingleton().

Added explanation to type hints as well.
@github-actions github-actions bot added Component: Catalog Relates to Mage_Catalog Component: Core Relates to Mage_Core Component: Oauth Relates to Mage_Oauth labels Jul 19, 2022
@github-actions github-actions bot removed Component: Adminhtml Relates to Mage_Adminhtml Component: Core Relates to Mage_Core Component: Oauth Relates to Mage_Oauth Component: Catalog Relates to Mage_Catalog labels Jul 19, 2022
Copy link
Contributor

@fballiano fballiano left a comment

Choose a reason for hiding this comment

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

tested briefly and LGTM

@elidrissidev elidrissidev merged commit 92a3b4f into OpenMage:1.9.4.x Jul 20, 2022
@github-actions
Copy link
Contributor

Unit Test Results

1 files  ±0  1 suites  ±0   0s ⏱️ ±0s
0 tests ±0  0 ✔️ ±0  0 💤 ±0  0 ❌ ±0 
7 runs  ±0  5 ✔️ ±0  2 💤 ±0  0 ❌ ±0 

Results for commit 92a3b4f. ± Comparison against base commit 79ecbe8.

elidrissidev pushed a commit to elidrissidev/magento-lts that referenced this pull request Jul 22, 2022
* refactor: Accessed _registry directly in several Mage factory methods.

Direct access is possible because null is no possibility due to executed code prior. This avoids additional isset checks being unnecessary.

* refactor: small code formatting adjustments

* refactor: Utilized null coalescing operator.

* fix: Try so solve PHPStan complaint due to changed return type of Mage::getSingleton().

* fix: Solved rest of PHPStan complaints due to changed return type of Mage::getSingleton().

Added explanation to type hints as well.

* fix: Reverted automatically replaced short list syntax due to php 7.0 compatibility.

* Revert "fix: Solved rest of PHPStan complaints due to changed return type of Mage::getSingleton()."

This reverts commit c9d165c.

* Revert "fix: Try so solve PHPStan complaint due to changed return type of Mage::getSingleton()."

This reverts commit fc9901c.

* fix: Reverted return type of getSingleton to original state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Mage.php Relates to app/Mage.php
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants