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

Refactor installer class to load dependencies when needed #2764

Merged
merged 4 commits into from
Jan 14, 2025

Conversation

mikkamp
Copy link
Contributor

@mikkamp mikkamp commented Jan 13, 2025

Changes proposed in this Pull Request:

This PR makes 2 main refactors to the Installer class.

1. Remove validation

In the constructor we were explicitly passing all classes which implement InstallableInterface and then validating to check if they implemented the same interface. Since the container is responsible for passing the right dependencies we don't need to do another loop through those classes to validate them.

2. Load dependencies when needed

Passing the dependencies in the constructor means it will call the constructor of all the dependencies, even when we don't use them. Since the Install class relies on a hook being called and some other additional conditions, we can retrieve the necessary classes from the container only when they are needed. This is a minor optimization in how the extension constructs itself, but together with other optimizations it will make a difference.

If we look at the before and after of a front end request we can see that it reduces the time spent resolving container dependencies.

Before:
image

After:
image

Since we changed the load order this was initially generating a fatal error:

PHP Fatal error:  Uncaught TypeError: Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager::__construct(): Argument #1 ($attribute_mapping_rules_query) must be of type Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\AttributeMappingRulesQuery, string given in google-listings-and-ads/src/Product/Attributes/AttributeManager.php:69

This was because the DBServiceProvider wasn't registered as the provider of this class, so it wasn't resolving when loading in a different order.

Detailed test instructions:

  1. Use the snippet add_filter( 'woocommerce_gla_force_run_install', '__return_true' );
  2. Remove the option gla_db_version
  3. Remove the option gla_install_version
  4. Refresh an admin page and confirm the options are re-added
  5. Remove one of the DB tables like wp_gla_budget_recommendations
  6. Refresh an admin page and confirm it's added again

Changelog entry

  • Tweak - Refactor installer class to load dependencies when needed.

@mikkamp mikkamp self-assigned this Jan 13, 2025
@github-actions github-actions bot added the changelog: tweak Small change, that isn't actually very important. label Jan 13, 2025
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

Attention: Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Project coverage is 67.1%. Comparing base (c8c1efa) to head (bd78fab).
Report is 7 commits behind head on develop.

Files with missing lines Patch % Lines
src/Installer.php 0.0% 6 Missing ⚠️
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             develop   #2764     +/-   ##
===========================================
+ Coverage       67.0%   67.1%   +0.1%     
+ Complexity      4692    4688      -4     
===========================================
  Files            480     480             
  Lines          19601   19583     -18     
===========================================
  Hits           13141   13141             
+ Misses          6460    6442     -18     
Flag Coverage Δ
php-unit-tests 67.1% <0.0%> (+0.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...nternal/DependencyManagement/DBServiceProvider.php 6.2% <ø> (ø)
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% <0.0%> (ø)
src/Installer.php 0.0% <0.0%> (ø)

@mikkamp mikkamp requested a review from a team January 13, 2025 17:49
Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

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

LGTM ✅ Thanks for the refactor

THe options are recreated correctly:
Screenshot 2025-01-13 at 19 39 41

I also tested Attribute Mapping feature and works as expected.

Notice it's not necessary to add the snippet. Because if we delete gla_db_version then the installer and first_installer will trigger as the first check is true https://github.com/woocommerce/google-listings-and-ads/pull/2764/files#diff-67306479da293f095927542c4cff1a45875138eb52372caaba982b07c053285bL82 .

@mikkamp mikkamp merged commit 4ffa272 into develop Jan 14, 2025
14 checks passed
@mikkamp mikkamp deleted the tweak/refactor-installer-class branch January 14, 2025 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: tweak Small change, that isn't actually very important.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants