Date Published: 9 March, 2019

php logo

Unit Testing Dates and Times with Carbon

One area of my codebase at work that I had problems sufficiently testing were instances in my code base that had time based logic to them. Since I work in the manufacturing sector, lots of logic relies on knowing what the current, last, or next shift is. There are lots of edge cases that need tested around shift change, especially third shift, which starts before midnight, but for all the reporting and other facets of my software it is technically tomorrow.

So these time-dependant functions for the most part did not have useful, functional unit tests, until recently. In the past I would write my logic, cross my fingers and wait for the bug reports to come rolling in. I'm so thankful that I discovered this testing aid built in to the [Carbon] PHP library. (If you haven't used Carbon, stop reading and go check it out, I'll forgive you. It makes working with dates and times in PHP a walk in the park, it is one of the most useful libraries I use!)

So now you are wondering, "What is this wonderful function that will let me test my date/time based logic easier?" I'm glad you asked. It is the setTestNow() function built in to the [Carbon] api. Basically, this allows you to determine when now() is.

Read More
Date Published: 3 March, 2019

Creating an RSS Feed for a Symfony Blog

Introduction

One of the features I've wanted to add to this blog for a while now, but kept putting off, until now, is an RSS feed. I recently created an account on [connect.symfony.com] and there was a spot to add an RSS feed to one's profile, which was the only thing stopping me from getting my profile to 100% and earning the "Profile completed" badge. Nothing like a little gamification to make me stop putting off this task.

Since I'm never one to re-invent the wheel, without justification, I of course looked to see if there were any Symfony bundles that would allow me to easily add an RSS feed to my blog. "Easily" being the keyword in that sentence. I did find several of bundles, but all of them seemed to have convoluted configuration, with a learning curve much steeper than rolling my own.

Oh, yea, you can see it in action here: https://eidson.info/rss

Read More
Date Published: 23 February, 2019

Override a trait in PHP

php logo

I encountered a situation recently in which I wanted to change the method signature for a method that was injected into a class via a trait, namely changing the default parameter. I wanted to do this so I didn't have to pass in the new default parameter every place that I called this method on this particular class.

Overriding the method, was easy enough. As soon as a new method is declared in a class with the same name, the trait method is overridden. The tricky part was calling the 'parent' method from the 'child' method, so I didn't have to duplicate the logic inside my new method. I tried parent::methodName() and TraitName::methodName() before running off to the google machine to learn the proper way to do this.

It turns out that it is as easy as creating an alias for the trait method name in the "Use" statement, which made perfect sense after I saw it.

So, if this is the trait that you want to use and override:

Read More
Date Published: 4 December, 2017

filter_var() is Your Friend

Intro

I was recently reading a blog post by Aaron Saray called In PHP, False is Sometimes True. This is an issue that I've encountered in the past and I'd already considered blogging about the solution, which is something Aaron left out of his blog post.

Read More
Date Published: 30 November, 2017

Create a File Watcher for php-cs-fixer in PhpStorm

File Watcher Settings for Linux

Intro

This is a quick an easy guide to setting up a file watcher on PhpStorm so that every time a php file is saved, php-cs-fixer is ran on the file. More than just a linter, php-cs-fixer will update your code to match coding standards such as the official PSR-1 and PSR-2, as well as community standards such as the Symfony one. It will also do some

Read More
Date Published: 26 November, 2017

php 7.3 - Flexible heredoc & nowdoc Syntax

php logo

Although we've still got a while to go before we see php 7.3, since we're still 4 days away from the release of php 7.2, I've already seen one new feature that looks like it will make its way into 7.3 that I am excited about. This feature is more flexible syntax for heredoc and nowdoc statements. Here is the official rfc if you'd like to read it.

What are heredocs and nowdocs?

Read More
Date Published: 19 November, 2017

Editing Shared Libraries in PhpStorm with Composer and git

Composer logo

The "Old" Workflow

When I started my current position way back in the winter of 2013 as the sole Application Developer in a corporate/enterprise environment, I was tasked with maintaining existing web applications, porting Visual Basic apps into a web environment, as well as creating new applications. Each of the php applications that I had inherited had several shared dependencies that were maintained independently in their separate project directories. Any bug that was found and fixed in one of these 'libraries' had to be copied manually to the other projects

Read More
Date Published: 12 November, 2017

PHP_EOL is Broken (Sometimes)

php logo

What is PHP_EOL?

Introduced in version 5.0.2 of php, PHP_EOL is a string constant that represents the correct end of line symbol for the platform that you are running php on. So on Windows this should be set to "\r\n" (carriage return and line feed) and on a *nix system it should be set to "\n" (only line feed).

Read More

About Todd

Full stack application developer. Life-long learner. Pragmatic programmer. Believer in clean coding. Proponent for extensible and reusable code. Hobbies include (very) amateur photography, collecting old jazz records and going to live music performances.

North Central Ohio, US
toddeidson[dot]info

Obligatory Disclaimer

All opinions are my own, probably wrong, and subject to change without notice.

© 2017-2019 Todd Eidson. All content is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.

Hosted on linode