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

Scope composer dependencies #6614

Merged
merged 14 commits into from
Mar 13, 2023
Merged

Scope composer dependencies #6614

merged 14 commits into from
Mar 13, 2023

Conversation

m1r0
Copy link
Member

@m1r0 m1r0 commented Mar 7, 2023

Resolves #6574

Proposed Changes

  • Use php-scoper to scope the composer dependencies by moving them to a new namespace (Sensei\ThirdParty).
  • The scoped dependencies live in a new folder (third-party).
  • Use the standard composer autoloader to load the scoped dependencies. This approach avoids some of the autoloading issues described in How to use this to prefix a WordPress plugin? humbug/php-scoper#442.
  • Update the CI workflows to use the scoped dependencies.

Testing Instructions

I was not able to add tests for the composer Actions class. The passed Event class is only loaded by composer so I can't mock it. Let me know if you have suggestions on how to test it.

  1. Delete your vendor folder.
  2. Run composer install.
  3. Check that the third-party folder is created and not empty.
  4. Remove the dev dependencies by running composer install --no-dev
  5. Check that your vendor folder has only the composer autoloader related files and no dev dependencies.
  6. Enable the email_customization feature flag.
  7. Go to Sensei LMS -> Settings -> Emails and click "Preview" on one of the emails. This makes sure the emogrifier dependency is loaded and working.
  8. Download the CI generated build zip and make sure it works.
  9. Make sure the composer dev dependencies are not included in the CI generated build zip.

Pre-Merge Checklist

  • PR title and description contain sufficient detail and accurately describe the changes
  • Acceptance criteria is met
  • Decisions are publicly documented
  • Adheres to coding standards (PHP, JavaScript, CSS, HTML)
  • All strings are translatable (without concatenation, handles plurals)
  • Data is sanitized / escaped
  • Follows our naming conventions (P6rkRX-4oA-p2)
  • Hooks (p6rkRX-1uS-p2) and functions are documented
  • New UIs are responsive and use a mobile-first approach
  • New UIs match the designs
  • Test cases are written and passing (including feature flags)
  • Different user privileges (admin, teacher, subscriber) are tested as appropriate
  • Code is tested on the minimum supported PHP and WordPress versions
  • User interface changes have been tested on the latest versions of Chrome, Firefox and Safari
  • We are generally happy with it and don’t feel like it immediately needs to be rewritten
  • Application performance has not degraded
  • "Needs Documentation" label is added if this change requires updates to documentation
  • Known issues are created as new GitHub issues
  • Continuous Integration build is passing

@m1r0 m1r0 self-assigned this Mar 7, 2023
@m1r0 m1r0 changed the title Scope dependencies Scope composer dependencies Mar 7, 2023
@m1r0 m1r0 added this to the 4.12.0 milestone Mar 7, 2023
"phpcompatibility/phpcompatibility-wp": "2.1.4",
"sirbrillig/phpcs-no-get-current-user": "1.1.0",
"sirbrillig/phpcs-variable-analysis": "2.11.9",
"squizlabs/php_codesniffer": "3.7.1",
"wp-coding-standards/wpcs": "2.3.0",
"yoast/phpunit-polyfills": "1.0.4"
},
"minimum-stability": "dev",
Copy link
Member Author

Choose a reason for hiding this comment

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

Lowering the minimum stability because php-scoper has a dev-master dependency.

Comment on lines +17 to +20
Finder::create()->files()->in( 'vendor/pelago/emogrifier' )->name( [ '*.php', 'LICENSE', 'composer.json' ] ),
Finder::create()->files()->in( 'vendor/sabberworm/php-css-parser' )->name( [ '*.php', 'LICENSE', 'composer.json' ] ),
Finder::create()->files()->in( 'vendor/symfony/css-selector' )->name( [ '*.php', 'LICENSE', 'composer.json' ] ),
Finder::create()->files()->in( 'vendor/symfony/polyfill-php80' )->name( [ '*.php', 'LICENSE', 'composer.json' ] ),
Copy link
Member Author

Choose a reason for hiding this comment

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

We use only the emogrifier dependency but its dependencies should also be included.

.gitignore Outdated
@@ -11,6 +11,7 @@ hookdocs
node_modules/
tests/images/test-*
vendor/
vendor_prefixed/
Copy link
Member Author

@m1r0 m1r0 Mar 7, 2023

Choose a reason for hiding this comment

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

Folder name suggestions are welcome. 🙂

Maybe libs, deps, vendor_scoped, third-party? 🤔

Copy link
Member Author

@m1r0 m1r0 Mar 8, 2023

Choose a reason for hiding this comment

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

Having "vendor" in the name is a bit confusing so I've updated the folder/namespace to third-party / Sensei\ThirdParty.

@@ -7,14 +7,39 @@
"bamarni/composer-bin-plugin": "1.8.2",
"dealerdirect/phpcodesniffer-composer-installer": "0.7.2",
"dms/phpunit-arraysubset-asserts": "0.4.0",
"humbug/php-scoper": "0.13.9",
"pelago/emogrifier": "6.0.0",
Copy link
Member Author

@m1r0 m1r0 Mar 7, 2023

Choose a reason for hiding this comment

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

The pelago/emogrifier dependency is intentionally moved to require-dev because it shouldn't be loaded by the composer autoloader (vendor/autoload.php). The third-party dependencies are now managed by the php-scoper config and then autoloaded by vendor/autoload.php.

Copy link
Member

@merkushin merkushin Mar 12, 2023

Choose a reason for hiding this comment

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

Isn't Emgorifier used in Sensei\Internal\Emails\Email_Sender?
I mean that usually considered as a production dependency.

Copy link
Member

Choose a reason for hiding this comment

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

Hum. I think I understand the problem. We don't include vendor (as a source of dependencies) anywhere, so it doesn't make sense to use production dependencies at all. Basically, I think it is a bit confusing, but we probably "resolve" this confusing by documenting it properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, I agree, it's a bit confusing. Another option was to have a separate composer file but I felt that is a bad idea. 🙂 Where do you think would be a good place to add docs for this? CONTRIBUTING.md?

For posterity: the way I look at it is that the third-party dependencies (other projects call them libs) are not part of the composer production dependencies. Composer is there to only generate and autoload them.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like a good place.
Maybe also add information about it here: https://github.com/Automattic/sensei/wiki/Setting-Up-Your-Development-Environment

@codecov
Copy link

codecov bot commented Mar 7, 2023

Codecov Report

Merging #6614 (ebbba67) into trunk (902fb3d) will increase coverage by 0.04%.
The diff coverage is n/a.

❗ Current head ebbba67 differs from pull request most recent head 8a20894. Consider uploading reports for the commit 8a20894 to get more accurate results

Impacted file tree graph

@@             Coverage Diff              @@
##              trunk    #6614      +/-   ##
============================================
+ Coverage     48.10%   48.15%   +0.04%     
- Complexity     9993     9999       +6     
============================================
  Files           502      502              
  Lines         35578    35602      +24     
  Branches        283      283              
============================================
+ Hits          17115    17144      +29     
+ Misses        18251    18246       -5     
  Partials        212      212              
Impacted Files Coverage Δ
includes/internal/emails/class-email-sender.php 88.04% <ø> (+0.94%) ⬆️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0cbeb0a...8a20894. Read the comment docs.

@m1r0 m1r0 force-pushed the add/scope-dependencies branch from 6cde5ec to 554860b Compare March 7, 2023 20:03
@m1r0 m1r0 force-pushed the add/scope-dependencies branch 4 times, most recently from b2cb014 to b4aa3cb Compare March 7, 2023 22:04
@m1r0 m1r0 removed the No Changelog label Mar 7, 2023
@m1r0 m1r0 force-pushed the add/scope-dependencies branch 2 times, most recently from 4d0f944 to fd8fb5f Compare March 7, 2023 22:22
@m1r0 m1r0 force-pushed the add/scope-dependencies branch from fd8fb5f to 95321ae Compare March 7, 2023 22:26
@m1r0 m1r0 force-pushed the add/scope-dependencies branch from 2a67c76 to 7e8848c Compare March 7, 2023 22:42
@m1r0 m1r0 marked this pull request as ready for review March 7, 2023 23:04
@m1r0 m1r0 requested a review from a team March 7, 2023 23:33
@merkushin merkushin added the [Status] Needs Documentation Requires documentation updates label Mar 12, 2023
merkushin
merkushin previously approved these changes Mar 12, 2023
Copy link
Member

@merkushin merkushin left a comment

Choose a reason for hiding this comment

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

Works well both locally and on jurassic site 👍

As for the test for the Actions class, I don't see a straightforward solution here. We can consider a couple of workarounds. One is to add this class (or interface) to tests suite. Another is to remove type hinting and use "duck-typing" approach.

@@ -7,14 +7,39 @@
"bamarni/composer-bin-plugin": "1.8.2",
"dealerdirect/phpcodesniffer-composer-installer": "0.7.2",
"dms/phpunit-arraysubset-asserts": "0.4.0",
"humbug/php-scoper": "0.13.9",
"pelago/emogrifier": "6.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

Hum. I think I understand the problem. We don't include vendor (as a source of dependencies) anywhere, so it doesn't make sense to use production dependencies at all. Basically, I think it is a bit confusing, but we probably "resolve" this confusing by documenting it properly.

@m1r0 m1r0 merged commit 0ae251c into trunk Mar 13, 2023
@m1r0 m1r0 deleted the add/scope-dependencies branch March 13, 2023 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
No Changelog [Status] Needs Documentation Requires documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable to include dependencies during the plugin build
2 participants