Adam Cameron's Dev Blog

"... I completely disagree..." - Peter Lafferty
PHP: Expectation management with Symfony Validator validation
Wednesday Jul 4, 2018 12:26 PM

G'day: I was checking out of of our web services the other day, replicating a bug I was fixing, and found the need to guess at what the body content needed to be for a POST I was doing. Our validation is solid, so I could just start with an empty object, and the validation would tell me what I needed to pass. Or, yes, I could RTFM but I couldn't be arsed. Anyhow, this tactic worked for the first few properties: the validation came back with "you need this property"... "nah, it needs to be an...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

In defence of public properties
Monday May 7, 2018 4:30 PM

G'day: Recently I was re-writing one of our cron job scripts. It's the sort of thing that runs at 2am, reads some stuff from the DB, analyses it, and emails some crap to interested parties. We usually try to avoid complete rewrites, but this code was written... erm... [cough insert some period of time ago when it was apparently OK to write really shit code with no code quality checks and no tests]... and it's unmaintainable, so we're biting the bullet and rewriting coherently whilst adding some...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

PHP: perpetual misuse of array type declarations
Saturday May 5, 2018 4:24 PM

G'day: This has been irking me enough recently to cause me to dust off this old blog. For a while PHP has had limited argument type checking on function arguments, and this was improved in PHP 7. The docs cover it - Function arguments › Type declarations - so I'll not repeat them here. But we can have this code: function takesArray(array $a){ return $a; } var_dump(takesArray(["tahi", "rua", "toru", "wha"])); Output: array(4) { [0]=> string(4) "tahi" [1]=> string(3)...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

That array_map quandary implemented in other languages
Wednesday Oct 10, 2018 8:58 PM

G'day: A coupla days ago I bleated about array_map [having] a dumb implementation. I had what I thought was an obvious application for array_map in PHP, but it couldn't really accommodate me due to array_map not exposing the array's keys to the callback, and then messing up the keys in the mapped array if one passes array_map more than one array to process. I needed to remap this: [ "2008-11-08" => "Jacinda", "1990-10-27" => "Bill", "2014-09-20" => "James", ...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

PHP: array_map has a dumb implementation
Sunday Nov 26, 2017 3:40 PM

G'day: This cropped up a few weeks back, but I've had no motivation to write anything recently so I've sat on it for a bit. It came back to bite me again the other day and my motivation seems to be returning so here we go. I was working on some code the other day wherein I needed to query some stuff from one DB, and then for the records returned from that, look for records in an entirely different data repository for some "additional data". The best I could do was to get the IDs from the first...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

PHP: misuse of nullable arguments
Wednesday Nov 22, 2017 4:06 PM

G'day: PHP 7.1 added nullable types to the PHP language. Here's the RFC. In short, one can specify that a return type or an argument type will accept null instead of an object (or primitive) of the specified type. This code demonstrates: function f(int $x) : int { return $x; } try { $x = null; $y = f($x); } catch (\TypeError $e) { echo $e->getMessage(); } echo PHP_EOL . PHP_EOL; function g(?int $x) : ?int { return $x; } $x = null; $y = g($x); var_dump($y); Ouptut:...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Mishmash: code review as a learning exercise, loops vs higher-order-functions and testing
Saturday Aug 19, 2017 4:06 PM

G'day: There's a few things going on in my head here, and none individually are worthy of an article, but perhaps if I dwell on a bunch of stuff something might shake out. I have no idea as I have not drafted this one in my head like I usually do, I'm just "let's start typing and see if I get anywhere". Eek. OK so yesterday I was code reviewing one of my mates' work, and breezed past this code (this is not the exact code, but it's a replication of it): public function...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Yeah, you do want 100% test coverage
Monday May 13, 2019 2:55 PM

G'day: I've read a number of blog articles recently that have been eshewing 100% test coverage. The rationale being that there's little difference between 90% and 100%, and that that last 10% can be quite tricky to attain as - we being human 'n' all - sometimes leave the worst to last. Obviously we should all be doing TDD so that situation would not arise, but - again - we're human so don't always do TDD for one reason or another ("can't be arsed" is the usual one). Another rationale is that...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

PHP: A function that returns two different things
Sunday Jul 23, 2017 10:02 PM

G'day: I'm gonna plagiarise one of me own answers to a Stack Overflow question here, as I think it's good generic advice. The question was "PHP 7: Multiple function return types". The subject line sums it up really. The person here wants to return a value or false from a function. I see this a lot in PHP code. Indeed... PHP itself does it a lot. My answer was as follows: [...] From a design perspective, having a function that potentially returns different types of result indicates a...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

@dac_dev Twitter account
Saturday Jul 8, 2017 4:34 PM

G'day: Yes, this thing is still alive. This morning I indicated on Twitter I was discontinuing my usage of the @dac_dev Twitter account. I'll just be using my @adam_cameron one from now on. @NXRK made this observation just now: half expected that you'd rename the two to swap names and keep the historical content/followers on this one— rmcdowall (@NXRK) July 8, 2017 Just to be clear: my twitter account is the @adam_cameron one. The @dac_dev one was created specifically...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]