Open tobeycodes opened 1 month ago
Thanks, @tobeycodes. I've added this to the Internal Tools working group agenda to be discussed. If you have any additional context/information that you think would be useful for those reviewing this, I'd greatly appreciate it if you could add it.
@darylldoyle I don't have too much context to add but I can perhaps help with an example of what I mean
class Bitly extends Module
// CANT DO THIS WITH MODULES
public function __construct(
private BitlyAPI $bitlyAPI,
) {}
public function add_hooks() {
add_action('save_post' [$this, 'add_bitly_short_url_on_save']);
}
public function add_bitly_short_url_on_save(WP_Post $post) {
// CANT DO THIS WITH MODULES
$this->bitlyAPI
// NEED TO DO THIS OR PUT THE API CODE IN THIS CLASS AND CODE IS TIGHTLY COUPLED
$bitlyApi = new BitlyAPI();
}
}
By not using depedency injection we are making our code less testable and tighly coupling our WP actions/filters with generic PHP code. It makes it harder to refactor and replace any code in the future
The scaffold currently has a pattern of Modules and a way to initialize these modules. These are useful but they do not allow for dependency injection which makes it harder to write easily testable code.
I think we should replace this in favour of a container and dependency injection. There are several popular packages to do this or we could maintain our own
https://github.com/silexphp/Pimple https://github.com/PHP-DI/PHP-DI