Tag: php
-

How do we deal with dependencies in PHP
Generally what we want to do when we’re coding something is to call a function. While some of the functions we call could stand alone and some could be methods on an object, they’re all just function calls. So why does most PHP code have so many classes? I don’t think code organization is a…
-

Safely coding with constants
In PHP there is a tendency to assume that the code we are working on is the only code that is running. The global and transactional nature of the language’s past has made this easy to do. This tendency naturally leads us to use global state, and while there does seem to be a resistance…
-

Creating Sniffs for a PHPCS Standard
As a follow-up to my summary of using phpcs to lint your PHP code, this post will explain how to create, modify, and test a phpcs standard. As a quick refresher, phpcs organizes its linting messages (warnings and errors) into “sniffs”, then into “categories”, then into a “standard”. The official tutorial for creating a standard…
-

Linting PHP with phpcs sniffs
Linting is the process of using an automated tool to scan your code for problems before you commit and deploy. It is a practice widely used in the development workflow of many languages, but hasn’t much been used in the PHP of WordPress developers. The most commonly used linter in PHP right now is called…
-

Maybe returning errors in PHP
A common pattern I see in WordPress PHP code (and probably other PHP code) is a function which does some operation on data and returns a value. For example, a function which makes a database query and then returns the resulting row. In order to make more robust code and prevent bugs, I’ve been systematically…
-

Functional Dependency Injection in PHP
Having become used to the convenience of passing first-class functions around in JavaScript to make other functions decoupled and easily testable, I was wondering if we could do the same thing in PHP. Of course, PHP technically has first-class functions as well, but the syntax is a little awkward, since the functions must be referenced…
-

PHP Unit Testing and Pad Thai
As a follow-up to my last post about PHP unit testing, I recently gave a talk at WordCamp Boston about how to write testable WordPress plugins. You can see the talk slides here: https://payton.codes/testable-wordpress-plugins/ You can also watch the talk here, although the audio quality isn’t great. The premise of my talk was mainly that…
-

Test spies in PHP
I think that I wrote my first unit tests in Ruby with RSpec, back in the day. But I learned most of my testing knowledge from working with mocha and chai in JavaScript. One of the things that I learned from these tools is that being able to express your test logic in clear, nearly-natural…
-

Copying files… sometimes
File this one under “tools that probably only I will find useful”. In the course of my normal job I need to copy files to a synchronized directory on my computer (something like a DropBox folder). The files are JavaScript code that’s been transpiled and copying them to the synchronized directory is what deploys them…
-

Avoiding Tightly-Coupled REST APIs
As you might have read, WordPress is getting a powerful REST API. For a few years now I’ve been writing endpoints for the WordPress.com version of this API, as well as few REST APIs in other languages (Ruby, JavaScript). As I’m also a big fan of unit testing, I’d like to share a pattern that…