Completed
Push — master ( b43387...8f2a89 )
by Yaro
08:18
created

OldAndAttribute   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 45.61 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 26
loc 57
ccs 21
cts 24
cp 0.875
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
getDefault() 0 1 ?
name() 0 1 ?
isTranslatable() 0 1 ?
A getAttribute() 0 8 3
A old() 0 8 2
A hasOld() 0 4 1
A oldOrDefault() 13 13 3
A oldOrAttribute() 13 13 3

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 Yaro\Jarboe\Table\Fields\Traits;
4
5
trait OldAndAttribute
6
{
7 86
    public function old($name = null)
8
    {
9 86
        if (is_null($name)) {
10 17
            $name = $this->name();
11
        }
12
13 86
        return old($name);
14
    }
15
16 69
    public function hasOld($name = null)
17
    {
18 69
        return !is_null($this->old($name));
19
    }
20
21 17 View Code Duplication
    public function oldOrDefault($locale = null)
22
    {
23 17
        $name = $this->name();
24 17
        if ($locale) {
25
            $name .= '.'. $locale;
26
        }
27
28 17
        if ($this->hasOld($name)) {
29 17
            return $this->old($name);
30
        }
31
32 17
        return $this->getDefault();
33
    }
34
35 18 View Code Duplication
    public function oldOrAttribute($model, $locale = null)
36
    {
37 18
        $name = $this->name();
38 18
        if ($locale) {
39
            $name .= '.'. $locale;
40
        }
41
42 18
        if ($this->hasOld($name)) {
43 17
            return $this->old($name);
44
        }
45
46 1
        return $this->getAttribute($model, $locale);
47
    }
48
49 35
    public function getAttribute($model, $locale = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51 35
        if ($locale && $this->isTranslatable()) {
52
            return $model->getTranslation($this->name(), $locale, false);
53
        }
54
55 35
        return $model->{$this->name()};
56
    }
57
58
    abstract public function getDefault();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
59
    abstract public function name(string $name = null);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
60
    abstract public function isTranslatable();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
61
}
62