The more you work with WordPress, especially if you employ tools like Composer and the like, the more you’re likely going to find certain libraries, tools, and similar things that form the foundation of projects on which you work.
Sometimes you may find them when looking through other people’s code, maybe you’ll find them when browsing GitHub, or maybe someone you work with will suggest a library (or a few) depending on a project.
Personally, I’ve had the experience of all of the above, and the more I work on building solutions for myself or others, the more I find myself setting up the same foundation for each project (which is typically a plugin of some sort).
Libraries for WordPress Plugins
To that end, I thought it might be worth sharing the libraries I use when building WordPress plugins. The content will be spread across a few posts to keep each one short and focused, but perhaps you’ll find something that you haven’t previously used (or that will enhance your current use).
Anyone who has written any code in any programming language is likely familiar with any type of *Unit library. In short, it’s a way to write unit tests easily.
From the project’s homepage:
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.
I’m not someone who necessarily subscribes to 100% code coverage (and that’s a topic for another post), but if you’re going to be writing unit tests, then this is the defacto library for unit testing your code.
Throughout this year, GrumPHP has become arguably my favorite library (though it’s made better by additional libraries that I’ll be covering throughout this series of posts).
I’ve talked about it in other posts. Though it describes itself as a “code quality tool,” you really can’t help but see the benefits until you wire it up to watch your code before trying to commit it to a repository.
The longer description is:
If the tests fail, you won’t be able to commit your changes. This handy tool will not only improve your codebase, it will also teach your co-workers to write better code following the best practices you’ve determined as a team.
And, just as if you’ve been employing any coding standards monitor in your IDE, this is one of those things that can be frustrating to use at first.
But as you learn it, it becomes easier to know what it’s going to flag so you automatically start writing code that has a higher level of quality.
PHPMD (short for PHP Mess Detector) works well in conjunction with GrumPHP in that it can process your code before you commit it to a repository.
The project describes itself as:
It takes a given PHP source code base and look for several potential problems within that source.
As with many similar libraries, this particular one can configure a set of rules so that you can easily turn things on and off based on what you or your team prefer.
The idea of having a PHP parser library setup to analyze your code seems a bit meta, right? I mean, you have the PHP runtime doing that, and many IDEs do it as well, but this works in a slightly different way.
From the repository:
This is a PHP 5.2 to PHP 7.1 parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
For those who are familiar how programming languages (and thus grammars) work, the idea of doing static analysis on the code will make sense.
Regardless, this is yet another library that find helps improve the code quality by reducing the complexity of the code.
More To Come
At the end of the series, I’ll share how to incorporate all of these into a
composer.json file that you can use in your project.
When you’re working on a solution for a client, you may have to alter the versions of each of these tools, but you’ll find that most of them offer support going back to PHP5.6 and up to PHP7.1.
Since I no longer offer comments on this site (and there are reasons for this more of which I’ll discuss again soon), feel free to tweet me about some of your preferred libraries and why you like them.
If nothing else, I’m always on the lookout for more.