LotPresenter   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 34.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 50
loc 143
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A renderName() 0 7 2
A renderDraftedName() 0 4 1
A cover() 0 11 2
A endDate() 12 12 2
A diffEndDate() 11 11 2
A getExpireDateAsString() 14 14 2
A getPublicDateAsString() 13 13 2
A renderPrice() 0 4 1
A renderNewPrice() 0 4 1
A renderOldPrice() 0 4 1
A renderCurrency() 0 4 1
A renderSalePercent() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Libraries\Presenterable\Presenters;
4
5
use Carbon\Carbon;
6
use Jenssegers\Date\Date;
7
8
class LotPresenter extends Presenter
9
{
10
    const END_DATE = 'expire_date';
11
12
    const PUBLIC_DATE = 'public_date';
13
14
    /**
15
     * Render lot's name...
16
     *
17
     * @return string
18
     */
19
    public function renderName()
20
    {
21
        if(! $this->model->name)
22
            return $this->renderDraftedName();
23
24
        return ucfirst($this->model->name);
25
    }
26
27
    /**
28
     * Render drafted lot's name..
29
     *
30
     * @return string
31
     */
32
    public function renderDraftedName()
33
    {
34
        return 'Drafted lot #'. $this->model->id;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function cover($size = null, $default)
41
    {
42
        $images = $this->model->images;
43
44
        if(count($images))
45
        {
46
            return $images->first()->present()->cover($size, $default);
47
        }
48
49
        return $default;
50
    }
51
52
    /**
53
     * Render end date from expiration_date timestamp.
54
     *
55
     * @param string $format
56
     * @return mixed
57
     */
58 View Code Duplication
    public function endDate($format = 'm/d/Y')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
61
        $enddate = self::END_DATE;
62
63
        $date = $this->model->$enddate;
64
65
        if($date)
66
            return $date->format($format);
67
68
        return '';
69
    }
70
71
    /**
72
     * Calc. difference from (expire day and today),
73
     * end_date MUST be bigger than now date.
74
     *
75
     * @return \DateInterval
76
     */
77 View Code Duplication
    public function diffEndDate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $enddate = self::END_DATE;
80
81
        $date = $this->model->$enddate;
82
83
        if($date)
84
            return $date->diff(Carbon::now());
85
86
        return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type documented by App\Libraries\Presentera...tPresenter::diffEndDate of type DateInterval.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
87
    }
88
89
    /**
90
     * Get expire date as string.
91
     *
92
     * @return string
93
     */
94
95
96 View Code Duplication
    public function getExpireDateAsString($format = 'd.m.Y')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        Date::setLocale(\Lang::slug());
99
        $enddate = self::END_DATE;
100
        $date = $this->model->$enddate;
101
102
        if($date){
103
            $date = Date::createFromTimestamp(
104
                $this->model->expire_date->timestamp
105
            );
106
            return $date->format($format);
107
        }
108
        return '';
109
    }
110
111 View Code Duplication
    public function getPublicDateAsString($format = 'd.m.Y')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        Date::setLocale(\Lang::slug());
114
        $public_date = self::PUBLIC_DATE;
115
        $date = $this->model->$public_date;
116
        if ($date) {
117
            $date = Date::createFromTimestamp(
118
                $this->model->public_date->timestamp
119
            );
120
            return $date->format($format);
121
        }
122
        return '';
123
    }
124
125
126
    public function renderPrice($price, $currency)
0 ignored issues
show
Unused Code introduced by
The parameter $price is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $currency is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        //
129
    }
130
131
    public function renderNewPrice()
132
    {
133
        return '';
134
    }
135
136
    public function renderOldPrice()
137
    {
138
139
    }
140
141
    public function renderCurrency()
142
    {
143
144
    }
145
146
    public function renderSalePercent()
147
    {
148
        //
149
    }
150
}