Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

QaFoo Blog:
Five Tips to Improve Your Unit Testing
Jun 13, 2017 @ 10:52:54

The QaFoo blog has posted a new article sharing five tips to improve your unit testing of your PHP applications. Even if you're a testing veteran, there's some helpful hints in here for you.

After you got the hang of unit testing there is still so much space for improvement. In this post I want to share five tips with advanced testers I have seen to influence testing in the right direction.

Their list of five tips includes advice about what you should consider as a "unit", refactoring test code and writing tests for bugs. Each topic includes a brief description of the suggestion and clear steps to follow when integrating it into your testing workflow.

tagged: unittest tips improvement advice unit logic refactor utilities bugs

Link: https://qafoo.com/blog/105_five_tips_improve_unit_testing.html

QaFoo Blog:
How You Can Successfully Ship New Code in a Legacy Codebase
Apr 21, 2017 @ 13:39:13

On the QaFoo blog there's a new post sharing some ideas on how you can add new code to a legacy application and ship it successfully without too much interruption to the current code.

Usually the problems software needs to solve get more complex over time. As the software itself needs to model this increased complexity it is often necessary to replace entire subsystems with more efficient or flexible solutions. Instead of starting from scratch whenever this happens (often!), a better solution is to refactor the existing code and therefore reducing the risk of losing existing business rules and knowledge.

[...] Instead of introducing a long running branch in your version control system (VCS) where you spend days and months of refactoring, you instead introduce an abstraction in your code-base and implement the branching part by selecting different implementations of this abstraction at runtime.

They then give a few examples of methods that can be use to get the new code in:

  • Replacing the Backend in a CMS
  • Rewriting a submodule without changing public API
  • Github reimplements Merge button

The final point is broken down into the process they recommend including the refactor of the current code, starting in on the new implementation and deleting the old code.

tagged: refactor ship new code legacy application tutorial

Link: https://qafoo.com/blog/101_branch_by_abstraction.html

Michael Dyrynda:
Partial model updates in Laravel
Apr 06, 2017 @ 09:44:05

Michael Dyrynda has written up a post showing the Laravel users out there how to perform partial model updates making use of the "intersect" method.

Many Laravel developers would be familiar with the helpful only method found on the request object, which allows you to specify keys to pluck from the request. Not only does this simplify your workflow, it works quite nicely when completely unguarding your models by setting protected $guarded = [];

[...] For newcomers to Laravel, you might find this suggestion dangerous, but using only means you will only pass the desired input to your model irrespective of what was passed via the request itself. [...] Adam Wathan tweeted about an approach he uncovered whilst helping somebody out when approaching partial model updates.

He shows how the method works by starting with a traditional update method call that reassigns model properties based on input (using "has" checks to ensure the property exists). He then refactors it to use the intersect method and replaces about twenty lines of code with one. He talks about the differences between using only and intersect and offers a caveat to using intersect around preserving null values on properties.

tagged: partial model update intersect only request tutorial refactor

Link: https://dyrynda.com.au/blog/partial-model-updates-in-laravel

How to Perform Extract Service Refactoring When You Don't Have Tests
Mar 22, 2017 @ 10:42:39

On the QaFoo blog they've posted an article sharing advice about refactoring to extract logic to services when there's no testing to cover the code.

When you are refactoring in a legacy codebase, the goal is often to reduce complexity or separate concerns from classes, methods and functions that do too much work themselves. Primary candidates for refactoring are often controller classes or use-case oriented service classes (such as a UserService).

Extracting new service classes is one popular refactoring to separate concerns, but without tests it is dangerous because there are many ways to break your original code. This post presents a list of steps and checklists to perform extract service when you don't have tests or only minimal test coverage. It is not 100% safe but it provides small baby-steps that can be applied and immediately verified.

The article talks about some of the primary risks when performing this kind of refactoring and how their extract method recommendations could case some of those issues. The tutorial then breaks down the process into the small steps:

  • Step 1: Create Class and Copy Method
  • Step 2: Fix Visibility, Namespace, Use and Autoloading
  • Step 3: Check for Instance Variable Usage
  • Step 4: Use New Class Inline
  • Step 5: Inline Method
  • Step 6: Move Instantiation into Constructor or Setter
  • Step 7: Cleanup Dependency Injection

While that seems like a lot of steps to take, they're all pretty small. They include a series of code snippets giving you an example to work from, making these small steps to refactor current functionality into a Solr service class.

tagged: tutorial refactor extract service tutorial unittest example code

Link: https://qafoo.com/blog/099_extract_service_class.html

Jason McCreary:
Accepting Adam's TDD Challenge
Mar 16, 2017 @ 10:43:02

Jason McCreary has a post on his site where he's officially accepted a challenge made by Adam Wathan during the Laracon Online conference and how he went about solving it.

Last week at Laracon Online Adam Wathan gave a talk entitled “Lies you’ve been told about testing”. Following the talk, Adam posted a challenge. Amid Adam’s post, he makes a single claim and presents a challenge.

The challenge was to write a unit test for the three provided class implementations in isolation. The claim is that isolated unit testing is incompatible with Test Driven Development (TDD). He talks about the other phases of TDD outside of the "refactor" step ("red" and "green") and how he would apply those to the steps leading to a complete refactor. He also talks briefly about testing styles and what would need to change to complete the process on both the code and the test side.

tagged: tdd testdriven development refactor challenge resolution

Link: https://jason.pureconcepts.net/2017/02/tdd-isolated-unit-test-challenge-adam-wathan/

QaFoo Blog:
Basic Refactoring Techniques: Extract Method
Mar 09, 2017 @ 11:24:35

On the QaFoo blog there's a tutorial posted to their blog sharing one of their basic refactoring techniques: the "extract" method:

Refactoring is the process of restructuring code without changing its behaviour and the technique "Extract Method" is one of the most important building blocks of refactoring.

With extract method you move a fragment of code from an existing method into a new method with a name that explains what it is doing. Therefore this technique can be used to reduce complexity and improve readability of code.

In this post I want to explain the mechanics of extract method using an example so that you have a checklist of steps when performing this refactoring. Extract method is a technique that you can use even without tests, because the potential risks of breaking are manageable when you follow the steps.

While things like PHPStorm provide their own "extract" functionality to help with refactoring, they want you to understand the manual steps involved. They start with a sample method that mixes controller logic with low level query logic. They break the process down into a few steps:

  • Identify code fragment to extract
  • Create empty method and copy code
  • Identify undeclared variables that must be arguments
  • Identify variables that are still used in old method
  • Call new method from original method

They also include a "risky" checklist with a few questions to ask about the refactor to see what extra steps might need to be considered.

tagged: refactor extract method technique tutorial

Link: https://qafoo.com/blog/098_extract_method.html

Laravel News:
Review: Refactoring to Collections
Feb 16, 2017 @ 10:45:33

The Laravel News site has posted a book review by Diaa Fares of Adam Wathan's "Refactoring to Collections" book.

“Never write another loop again.” A strong promo sentence used by Adam Wathan to market his Refactoring to Collections book, but does it hold the promise? Well, together we will explore how close that claim is to reality.

If you are a Laravel developer, chances are you’ve heard about the collection pipeline before. While I use it a little in my projects I hadn’t realized the possibilities of this approach until I read Refactoring to Collections. It brought me to another level of thinking about utilizing the power and capabilities of functional programming.

The review goes on to talk about imperative versus declarative programming (with examples) and how the book walks you through "the journey" to move to a collections-driven code structure. The review also points out the amount of concepts to learn in the book and the style/effort that was clearly put into its creation. It also points to this RFC as hope for the native use of "arrow functions" in PHP in the future.

tagged: refactor collections book review adamwathan

Link: https://laravel-news.com/review-refactoring-to-collections

QaFoo Blog:
Extracting Data Objects
Feb 10, 2017 @ 12:16:36

On the QaFoo blog they have a new post offering some advice on extracting functionality to data objects and reducing the complexity of your application's interfaces.

Extracting data objects from your code will make it easier to read and write, easier to test and more forward compatible. This post shows you the two most common cases where introducing a data object makes sense and how to do it.

The first case covers the extraction when a method ends up with too many parameters. We've all been there and remembering the correct order and values for each (not to mention optional vs required). By making use of value objects you can reduce that down to one or two parameters that act as self-contained "containers" for the same values. They illustrate with a refactor of "product" search criteria into a "ProductCriteria" object. The second example show a refactor away from using an array as an input value and providing a bit more structure with a "Checkout" value object instead.

The post ends with a helpful hint about migrating from one method to the other in legacy systems using a "shim" method to handle the new case right alongside the old one.

tagged: data object refactor simplicity parameters array

Link: https://qafoo.com/blog/096_refactoring_extract_data_objects.html

QaFoo Blog:
Getting Rid of static
Jan 12, 2017 @ 10:46:41

On the QaFoo blog today Kore Nordmann has posted a suggestion that could make your unit testing life simpler: get rid of statics (variables, methods, etc).

When people start (unit-)testing their code one of the worst problems to tackle are static calls. How can we refactor static calls out of an existing application without breaking the code and while producing new features? How can we get rid of this big test impediment?

They illustrate the main problem with a simple UserService class that contains a static function which, in turn, uses static calls to a Cache and a DB class. The major issue is that, when the static getUser method is called there's not a way to mock the Cache or DB classes, resulting in the actual handlers being called. They offer three things you can do to help refactor away from using static methods:

  1. Replaceable Singletons
  2. Service Locator
  3. Dependency Injection

For each item on the list a brief explanation is provided of what it is and how it helps you get away from the singletons scattered throughout your codebase (and how it makes things easier to test).

tagged: static refactor unittest testing singleton servicelocator dependencyinjection

Link: https://qafoo.com/blog/094_getting_rid_of_static.html

Freek Van der Herten:
Making overloaded functions readable
Jan 10, 2017 @ 12:18:19

Freek Van der Herten has a new post to his site sharing some of his ideas around making overloaded functions more readable, functions that can take in variable types of parameters (ex: both a string or an array) and can handle them correctly.

Sometimes you might allow a function to accept multiple data types. I don’t know for certain if it’s the correct term but for the remainder of this post I’m going to call such a function overloaded. In this post I’d like to show you a little trick to make overloaded functions more readable.

He starts off with an illustration from the Laravel framework of a "session" helper method that, in turn, calls "put" and checks for an array versus string input with some interesting logic behind it. He gives another example from a recent pull request where the code could be simplified using the same method as the "put" example, making it much more readable in the end. The post ends with one more example from this package and how the "respond" method was refactored with the same process, simplifying it down to a more readable and less-nested version.

tagged: overloaded method readability refactor loop variable argument

Link: https://murze.be/2017/01/making-overloaded-functions-readable/