user12057543
user12057543

Reputation:

Is there a simple way to reuse code in functions, rather than duplicating?

I am building my first web application, using the Laravel framework. However, the models are starting to get quite busy and I am noticing duplicate code - especially within functions.

My experience with coding is very limited, so I am getting confident at refactoring within functions and splitting functions into bite sized code, that has one task.

However... I'm wondering if there is any way to easily reuse code from functions?

I have attached an example of code I would like to refactor - This is a small example, as I have a few models where these types of repetition happen 4-5 times, which are now getting quite difficult to read through.

As you can see, the only difference between the two functions are the IsPast / IsFuture text within their names and the call on the $date.

Can anyone recommend how I could refactor this code?

public function getIsPastAttribute(): bool
{
    return $this->date_period->map(static function ($date) {
        /** @var Carbon $date */
        return $date->isPast();
    })->contains(false) === false;
}


public function getIsFutureAttribute(): bool
{
    return $this->date_period->map(static function ($date) {
        /** @var Carbon $date */
        return $date->isFuture();
    })->contains(false) === false;
}

Upvotes: 1

Views: 440

Answers (3)

Zahit Rios
Zahit Rios

Reputation: 179

I use traits to use the same functions in many controllers.

In the Http directory, create a new directory called Traits, here's an example of a Trait:

App/Http/Traits/MyTrait.php

<?php

namespace App\Http\Traits;


trait MyTrait{  
   public function myTraitFunction(){
     ...
   }
}

You can use in your controllers like this:

<?php

namespace App\Http\Controllers;


use App\Http\Traits\MyTrait;

class MyController extends Controller{

    use MyTrait;

    public function controllerFunction(){
        //calling the trait function
        $this->myTraitFunction();
        ...
    }   
}

Upvotes: 0

failedCoder
failedCoder

Reputation: 1424

For the two specific methods you provided you can do this:

public function getTimeAttribute($time): bool
{
    return $this->date_period->map(static function ($date) use($time) {
       /** @var Carbon $date */
       return $time == 'future' ? $date->isFuture() : $date->isPast();
    })->contains(false) === false;
}

You can reduce code duplication with smaller 'sub methods' that you call in the main methods. Instead of doing this:

public function f1() {
    ...a lot of duplicate code
}

public function f2() {
    ...a lot of duplicate code
}

You can do this:

private function helper() {
    ...a lot of duplicate code
}

public function f1() {
    $this->helper();
}

public function f2() {
   $this->helper();
}

You can also check out traits

Upvotes: 0

Salim Djerbouh
Salim Djerbouh

Reputation: 11064

Try dynamically named functions

public function getIsPastAttribute(): bool
{
    return $this->isDate('isPast');
}

public function getIsFutureAttribute(): bool
{
    return $this->isDate('isFuture');
}

public function isDate($tense): bool
{
    return $this->date_period->map(static function ($date) use ($tense) {
        /** @var Carbon $date */
        return $date->$tense();
    })->contains(false) === false;
}

Upvotes: 2

Related Questions