Steve Grunwell

Open-source contributor, speaker, and electronics tinkerer

Tag: PHP

A dimly-lit, empty factory building

Factory Methods for Hydrating Objects from JSON

If you’ve worked in a lot of codebases, this scenario will be familiar: somewhere in the app, we’re JSON-decoding a string, then using that to pass arguments to a method (such as a constructor). It may look something like this:

$json = '{"first": "Steve", "last": "Grunwell"}';
$data = json_decode($json);

$person = new Person($data->first, $data->last);

I’d like to humbly ask that you stop doing this. Instead, this post is going to show you how to accomplish the same result with a static, factory method and explain why the latter approach will save you all sorts of headaches.

Continue reading→

Grayscale close-up of a watch face

For a Great Time, Make it a DateTime

Let’s acknowledge something right out of the gate: working with dates and times can be a slog. Not a year goes by without some app breaking due to Daylight Saving Time or an assistant not realizing that March 31st exists.

For those of us working in PHP—especially more recent versions of the language—dates and times don’t need to be a source of pain. Instead of strtotime() this and date() that, we have functionality baked into PHP that dramatically simplifies the work of parsing, converting, and formatting dates and times.

Continue reading→

A typewriter with a sheet of paper reading "Equality"

Strict Equality for Better Code

A major focus of my day job right now is cleaning up the PHP in a decades-old monolith, which includes tests written for two different test runners by hundreds of engineers over the years.

I could write a book on the horrors I’ve seen (and currently have at least half a dozen blog posts in draft state), but I’m not interested in raking anyone over the coals for past engineering decisions—honestly, it’s to be expected with any project this size and age. Instead, I wanted to take a moment to talk about one of the most prevalent oversights made by engineers of all levels: strict equality.

Continue reading→

A pile of LEGO bricks spread out across a hardwood floor

Decouple Your Application Code with the Adapter Pattern

The last few months at work I’ve been deep in a refactoring project, cleaning up over twenty years of technical debt. It’s been a massive undertaking, but it’s rewarding work when I’m finally able to remove code that’s been hanging out well-past its expiration date.

One of the patterns that’s come in extremely handy is the Adapter Pattern, which lets me decouple application code from the underlying libraries that we use. This post will discuss how the Adapter Pattern works (with coffee-themed examples, as I tend to do), then demonstrate how it can help with refactoring.

Continue reading→

Taco, a black cat, peeking out of a white drawer

The Beauty of PHP Value Objects

At last year’s php[tek], one of my biggest “holy cow, why haven’t I been doing this?!” moments came from my friend Andrew Cassell when he explained PHP Value Objects in the context of Domain-Driven Design.

Put simply, a Value Object is an immutable object that encapsulates some data and will always be in a valid state.

Continue reading→

Stacks of vintage, sepia-toned photographs

Paid Support for Legacy Libraries

A few weeks ago, I was talking to my good friend Eric Mann about an open-source package he maintains. This particular package has quite a number of downloads and active users, despite Eric trying to abandon it a few years ago. He’s since restarted development on it, but now he faces a problem: people are upset that he’s dropped legacy PHP version support.

This particular package is popular within the WordPress ecosystem, which is big on backwards compatibility. Despite the fact that both PHP 5.6 and 7.0 stopped receiving even security updates at the end of 2018, there are still plenty of users out there running their applications in old, insecure versions of PHP. As a result, some people were rather upset when Eric stated “I’m not going to spend my [limited] time supporting EOL’d versions of PHP.”

Some commenters were quick to jump in with remarks ranging from “well, it doesn’t take that much time to support older versions of PHP…” to “WordPress supports older versions of PHP, so should you!”, but Eric remained firm: if you want support for older versions of PHP, you can either pay me for my time or contribute the code yourself.

It may sound a little harsh, but I’m 100% with Eric on this one: he doesn’t owe anybody his time and effort. That’s time he could be spending with his family, out hiking, or working on projects that he enjoys. Heck, knowing what Eric can do, back-porting support for old versions of PHP should be way down on his list of priorities.

Continue reading→

The skyline of Austin, TX

Getting started with testing at Longhorn PHP 2019!

To my knowledge, Longhorn PHP was the only new PHP community conference in the US that I didn’t get to speak at last year — I was fortunate enough to speak at PHP Detroit (Detroit), Southeast PHP (Nashville), Cascadia PHP (Portland), and WavePHP (San Diego). In fact, I haven’t been outside of an airport in Texas since the final Lone Star PHP (the spiritual predecessor to Longhorn PHP) in 2017. That’s but one reason I’m excited to announce that I’ll be giving my Testing Like You’ve Never Tested Before (Because You Haven’t) talk at Longhorn PHP 2019!

Continue reading→

A basket full of assorted vegetables

Streamlining your test suite with PHPUnit Data Providers

In every testing talk I’ve attended (or given), there’s one stand-out feature that often has the audience saying “whoa, I had no idea you could do that!” No, it’s sadly not “hey look, you can reliably build quality software with a much lower chance of defects or regressions!”, but rather the inevitable use of PHPUnit’s Data Providers.

With Data Providers, our test suite can become more readable and maintainable while making it trivial to add new testing scenarios. Best of all? PHPUnit ships with Data Providers right out of the box.

Continue reading→

The Pacific coastline in San Diego at sunset

Speaking at WavePHP 2018

I’ve spent most of my life living in Ohio, and it’s a state I very much consider to be my home. However, after a trip to San Diego a few years ago while I was working at 10up, San Diego certainly entered the running. When I heard that a new PHP conference, WavePHP, was starting up in San Diegoat the same resort we stayed in for our team meetup (so I already know where the best tacos in the area are) — I knew I had to apply. Now, I’m finally able to announce that I’ve been accepted to speak at the first-ever WavePHP!

Continue reading→

Two small monkeys on a tree branch

Automatically Install PHP’s Runkit7 via Composer

A WordPress plugin I’ve been working on recently needed to be able to accept configuration in a few different ways: users should be able to define a constant in the wp-config.php file or fill out a form within a settings screen. If the constant is defined, the setting screen should be aware of that and hide the setting, since the constant should take precedence.

This is a pretty common pattern in WordPress plugins, but it can get rather tricky to test; by design, once a constant is defined in PHP, you shouldn’t be able to change its value. PHPUnit has ways to work around this by running tests that define constants in separate processes, but this can seriously impact the performance of your test suite. Furthermore, the WordPress core test suite is pretty tightly coupled, so it doesn’t like when tests are run separately.

Continue reading→

Page 1 of 3

Be excellent to each other.