Posts

Every 2024 Date in Y-m-d Format

I had a need to have a list of every date in 2024 so I could dump it into a text file. I could have done it in Excel but I figured I might as well spend 10 times the amount of time writing it in PHP.

Read More

Getting Started with Test-Driven Development: Longhorn PHP 2023

I gave a talk about TDD at Longhorn PHP 2023. This post contains the slides and any associated resources I wanted to make available to everyone.

Rate my talk on joind.in: https://joind.in/talk/95604

Read More

Laravel Collections: collect() Function

Reducing Bugs With Static Code Analysis: PHP[Tek] 2023

I gave a talk about static code analysis at PHP[Tek] 2023. This is the slides and any associated resources I wanted to make available to everyone.

Rate my talk on joind.in: https://joind.in/talk/23cc8

Resources

Social

Getting Started with Test-Driven Development: PHP[Tek] 2023

I gave a talk about static code analysis at PHP[Tek] 2023. This is the slides and any associated resources I wanted to make available to everyone.

Rate my talk on joind.in: https://joind.in/talk/ec8ad

Resources

Social

Working With Cookies in PHP

A core functionality of any modern web application is keeping track of our users between web requests. We could add a unique identifier to every URL but this makes our URLs hard to generate, hard to read, and hard to share. Thankfully, every modern browser supports cookies to keep track of our users between requests.

In this article, we’ll discuss how to use cookies in our PHP applications.

Read More

Using Precommit Hooks For Static Code Analysis

In our last several articles, we’ve discussed how to use the PHP_CodeSniffer library to verify our code is following an agreed-upon coding standard and how to run the phpcs extension in VSCode to see where we’re not following the code as we type. We’ll eventually discuss how to move these checks to a Continuous Integration server but wouldn’t it be great if we could make sure our code was passing these checks before we push them anywhere?

In this article we’ll discuss how to use Git’s pre-commit hooks to run our static code analysis tools on just the set of files we’ve modified.

Read More

Fixing Long Functions With The Extract Function Refactoring

One of our core tenets of development is that code is read more than it’s written. To that end, we must make our code as easy to read and understand as possible. No one writes perfect code on the first try so it’s important that we continually refine our code so it’s easy for the next developer to read even if we’re the next developer.

In this article, we’re going to discuss what the extract function refactor is, what code smells are an indication to use it, and then work through an example in PHP.

Code Smell

We should use the extract function refactoring when we have code that’s duplicated in multiple places or when we can isolate a portion of a larger method to make multiple methods.

Duplicate code is less than ideal in our codebase because this duplication causes us to maintain the same logic twice and it’s easy for us to not update all the duplicate code which can cause bugs to enter our codebase.

“Large methods” can be hard to define. Some people have a hard limit for their teams while others say it needs to fit on a single screen. The logic of what can fit inside a single screen is a fun concept now that some people use their monitors in a vertical orientation.

A good rule to use is that a function should be as small as possible while still being easy to read. Small functions are also easier to test and debug as they generally only have a few paths.

What Is the Extract Function Code Refactoring?

In this technique, we take a section of code and make it a new function.

To perform this refactoring we’ll:

  1. Copy the section of code we’re extracting and then paste it into a new method
  2. Look for any local variables and add them as parameters to the method
  3. Replace the extracted code with a call to the newly created method
  4. Run our tests and make sure they all pass
  5. Look for places where we can use the newly extracted method

Example

Let’s work through an example. Our codebase contains the following function.

public function rebuildEstimatesBasedOnIncompleteTasks(): void
{
    // get the incomplete tasks assigned to this project
    $tasks = Task::where('project_id', $this->id)
        ->whereNull('end_date')
        ->get();

    $total = 0;
    foreach ($tasks as $task) {
        if ($task->time_estimate > 1) {
            $total += $task->time_estimate;
        }

        if ($task->time_estimate === null) {
            $total += 2;
        }
    }

    $this->estimated_date = now()->modify("+{$total} days");
    $this->save();
}

Because we used an intention revealing name of rebuildEstimatesBasedOnIncompleteTasks we can tell that we’re going to be rebuilding estimates based on our incomplete tasks but it’s hard to quickly parse exactly which part of the function does what.

Let’s extract a new function that calculates the total of a set of tasks.

  1. Copy the section of code we’re extracting and then paste it into a new method
public function rebuildEstimatesBasedOnIncompleteTasks(): void
{
    // get the incomplete tasks assigned to this project
    $tasks = Task::where('project_id', $this->id)
        ->whereNull('end_date')
        ->get();

    $total = 0;
    foreach ($tasks as $task) {
        if ($task->time_estimate > 1) {
            $total += $task->time_estimate;
        }

        if ($task->time_estimate === null) {
            $total += 2;
        }
    }

    $this->estimated_date = now()->modify("+{$total} days");
    $this->save();
}

public function calculateEstimatedHoursForTasks(): int
{
    $total = 0;
    foreach ($tasks as $task) {
        if ($task->time_estimate > 1) {
            $total += $task->time_estimate;
        }

        if ($task->time_estimate === null) {
            $total += 2;
        }
    }

    return $total;
}
  1. Look for any local variables and add them as parameters to the method
public function calculateEstimatedHoursForTasks(Collection $tasks): int
{
    $total = 0;
    foreach ($tasks as $task) {
        if ($task->time_estimate > 1) {
            $total += $task->time_estimate;
        }

        if ($task->time_estimate === null) {
            $total += 2;
        }
    }

    return $total;
}
  1. Replace the extracted code with a call to the newly created method
public function rebuildEstimatesBasedOnIncompleteTasks(): void
{
    // get the incomplete tasks assigned to this project
    $tasks = Task::where('project_id', $this->id)
        ->whereNull('end_date')
        ->get();

    $total = $this->calculateEstimatedHoursForTasks();

    $this->estimated_date = now()->modify("+{$total} days");
    $this->save();
}
  1. Run our tests and make sure they all pass

We’ll run our total test suite.

  1. Look for places where we can use the newly extracted method

In this case, we don’t have anywhere that we can reuse this new method but it could come in handy in the future.

What You Need To Know

  • Extract Function Refactor allows us to extra code from one function into a new one
  • This reduces duplication and improves readability

Creating Your Own Standard for PHP_CodeSniffer

In “The PSR Coding Standards” we discussed how we have three options when picking a coding standard.

  1. We can create a coding standard from scratch.
  2. We can borrow a coding standard that already exists and use it as is.
  3. We can borrow a coding standard that exists and then add or remove rules that don’t fit our team’s environment.

In this article, we’re going to discuss how we can implement the third option by creating our own standard for phpcs using the PSR12 standard as a starting point. If you missed either our article about “The PSR Coding Standards” or “PHP_CodeSniffer” you might want to read them first.

Read More

What is Code Refactoring?

One of our core tenets of development is that code is read more than it’s written. To that end, we must make our code as easy to read and understand as possible. No one writes perfect code on the first try so it’s important that we continually refine our code so it’s easy for the next developer to read even if we’re the next developer.

In this article, we’ll discuss what refactoring is, how to make it dead simple to do, and discuss the next set of articles in this series.

Read More
RSS

Join Our Mailing List!

View previous campaigns.

Top Posts

  1. Working With Soft Deletes in Laravel (By Example)
  2. Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
  3. Upgrading to Laravel 8.x
  4. Get The Count of the Number of Users in an AD Group
  5. Multiple Vagrant VMs in One Vagrantfile
  6. Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
  7. Changing the Directory Vagrant Stores the VMs In
  8. Accepting Android SDK Licenses From The OSX Command Line
  9. Fixing the 'Target class [config] does not exist' Error
  10. Using Rectangle to Manage MacOS Windows

subscribe via RSS

All content copyright This Programming Thing 2012 - 2021
Blogging about PHP, MySQL, Zend Framework, MySQL, Server Administration and Programming in general