Tag: coding
-

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…
-

Declarative vs. Imperative: asking for soup vs. making soup
A little while ago I wrote a post about my concepts of declarative vs. imperative programming. I ended that article by saying, “I can’t wait to see what I’ll learn next.” Well, I’ve found another definition which has been very helpful to me lately. Also, I’m a little worried that it might actually be the…
-

Lightweight rendering of React to strings
For a recent project I needed to be able to render React components (or really, just React-like components) into plain HTML strings. Of course, React provides a mechanism to do this: renderToStaticMarkup in its ReactDOM library. But wow, that library is a really big dependency to import if I don’t want any of the DOM reconciliation…
-

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…
-

Higher Order Components and Pie Recipes
Higher Order Components (“HOCs”) are the latest hotness to come out of the JavaScript idea world and land in our apps. React apps, anyway. I really want to write: Higher Order Components are just wrappers, but that would be simplifying the concept too much. Wouldn’t it? Maybe. Anyway, they’re wrappers. Keep your actual Component simple…
-

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…
-

An iframe without a url
Sometimes you need to display html inside an iframe, but it’s not at a URL. Perhaps you got the markup from an API endpoint or generated it yourself. Maybe the markup is from a different domain and you need to be able to manipulate its DOM without cross-origin errors. For all these reasons, I created…
-

Declarative vs. Imperative Soup
I remember not too long ago (I think this was about the time that React first came out) I was trying to understand what people meant when they said that it was “declarative” vs. “imperative”. Looking up the terms at the time didn’t enlighten me much. Mostly articles seemed to focus on the difference between…