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 MoreI 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 MoreI 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
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
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 MoreIn 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 MoreOne 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.
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.
In this technique, we take a section of code and make it a new function.
To perform this refactoring we’ll:
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.
- 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;
}
- 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;
}
- 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();
}
- Run our tests and make sure they all pass
We’ll run our total test suite.
- 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.
In “The PSR Coding Standards” we discussed how we have three options when picking a coding standard.
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.
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 Moresubscribe via RSS