PurpleTurtleCreative / completionist

Asana integration plugin for WordPress.
https://purpleturtlecreative.com/completionist/
GNU General Public License v3.0
1 stars 0 forks source link

Use autoloading to require class dependencies rather than repetitive require_once calls #175

Closed MichelleBlanchette closed 11 months ago

MichelleBlanchette commented 1 year ago

Sighhh... I've wanted to do this for a long time, but WordPress's coding standards don't make it easy since prefixed, lowercased, hyphenated file names don't actually match capitalized, underscored class names. šŸ¤¦ā€ā™€ļø

Also, WordPress's coding standards specifically note this:

Note about WordPres Core usage

While import use statements can already be used in WordPress Core, it is, for the moment, strongly discouraged.

Import use statements are most useful when combined with namespaces and a class autoloading implementation. As neither of these are currently in place for WordPress Core and discussions about this are ongoing, holding off on adding import use statements to WordPress Core is the sensible choice for now.

I realize this is referring to WordPress Core which doesn't apply to plugins, but I enjoy using PHPCS to enforce the WordPress coding standards throughout all of my WordPress code. (Which I still need to implement CI/CD pipelines for...)

For backwards-compatibility, I could define an associative array to directly map PHP classnames to their file paths. Obviously, that's a manual maintenance requirement as new classes are added, so it is prone to mistake and isn't ideal. However, this would be the safest approach for backwards compatibility to avoid renaming classes and changing file paths.

MichelleBlanchette commented 11 months ago

Welp, there's 200 instances of require in the src directory. šŸ™ƒ

There's 125 instances of require_once PLUGIN_PATH in the src directory. šŸ˜—