Tuesday, October 15, 2013

Using PHPUnit's returnCallback for a variable length valueMap

Awkward title aside, this is just a quick post about something I rigged together the other day. I have a mocked service locator object that needed to implement returnValueMap, but where the contents of the valueMap would vary based on which test I was executing. PHPUnit does not natively support modifying the valueMap after it's been assigned, so I wanted a way to append items to the map without re-defining the whole mock for every test.

Tuesday, September 17, 2013

Cloning a base project in PhpStorm

When I first started using PhpStorm (my current IDE) I noticed that I was applying a number of the same project-specific settings during each new project. Being a good (i.e. properly lazy) programmer, I figured that this should be automatable, so I dug into the project metadata that PhpStorm keeps in each project's .idea directory (at least on OS X). I found that with a few string substitutions I could create a template project with all the settings the way I like them, and then clone that metadata directory into each new project I created. You'll find a sanitized version of the script I use in this Github repo. Hope it saves someone out there some time.

Sunday, June 23, 2013

Rel=disavow for Twitter

Last night I was thinking about links posted on Twitter, and how many people I know will regularly or semi-regularly post links to things that they are not endorsing, in the "look at this flaming pile of idiocy" sense rather than the "check out this cool thing" sense. I know Twitter automatically adds rel="nofollow" attributes to outbound links from tweets, but that still doesn't mean that a user clicking on that link isn't providing the destination site with views/hits/ad impressions/etc. It got me wondering if there was a better way to point out notably awful things on the web without rewarding the destination site.

The first thing I was hoping for is something like rel="disavow", similar to what Google put together in their webmaster tools site, only for outbound links instead of inbound. Then again, any link relations would likely be stripped by (or inadvertently targeting) t.co. I eventually came to the conclusion that the only thing to do was hope that like-minded internet users use an ad blocking software or (possibly) link to Google cached versions of the pages instead of their live sites, which also has the benefit of storing the offending content for longer if the owner tries to change or remove it.

So I'm wondering... am I over-thinking this? Does my desire to see sites without rewarding them make me a bad internet user? Some sort of, dare I say it, pirate?

Edit: good enough for me: http://www.donotlink.com/

Wednesday, May 15, 2013

Testing Practices for Exceptions in unit tests

Back in 2011 I published a post claiming that using anything but try/catch in unit tests was a bad idea. Since then, after having more discussions with others in the field and reading Chris Hartjes's post about try/catch being a bad practice in unit testing, I've realized my initial post on the topic was a bit short-sighted and didn't explain my views as well as I thought. It also contained no examples. I've decided to post some clarifications here, as well as sum up the arguments on both sides based on Chris's post and the comments.

Sunday, February 10, 2013

Database Naming Conventions

I had no idea when I started making slides for this portion of my talk (Bringing Good Design to the Table) at SunshinePHP that this would be such a contentious issue. I should have known in hindsight, but I didn't expect this to be such a holy war. In the future I'll know better and preface this section quite strongly with "my opinion" caveats.

So that being said, I decided to outline those conventions again here. I'm going to try to give more of my reasoning behind the different choices below, to hopefully stem a few of the objections. Then again, nobody likes being told that the way they're doing things is "wrong" so let me again say that this is my opinion. I didn't create this convention and I'm not the only one that uses it, this just happens to be the one I'm most comfortable with. Maybe this will become part of a series exploring the contents of my talk, who knows.

Monday, December 10, 2012

An example of class refactoring

Last week I worked with one of our junior engineers on a design exercise and I thought I'd share. We're working on a theme system based on plug-able widgets, and we have a Widget_Manifest class that generates an array of widget objects based on the contents of a directory on the file system. Here's what we started out with:

Friday, November 23, 2012

Unit Testing magical code

Simply put, unit testing code with lots of magic is hard, much harder than it needs to be. However, if you seek out the magic in your code and replace it with clear, explicit calls to well-defined functionality, testing becomes much, much easier.

Take this common use of magic functionality: __get. This is often used to create fake public properties on an object. Probably the most egregious use of this magic method (that I've seen multiple times in various codebases) is code like this:

public function __get($name) {
    $propName = "_$name";
    if (isset($this->$propName) {
        return $this->$propName;
    }
}