Completed
Push — master ( 3bf7a5...ee6b5d )
by Yaro
15:42 queued 14:10
created

Datetime   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 19.3 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 11
loc 57
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 6 1
A getDateFormat() 0 4 1
A default() 11 11 3
A getListView() 0 7 1
A getEditFormView() 0 9 2
A getCreateFormView() 0 6 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 Yaro\Jarboe\Table\Fields;
4
5
use Yaro\Jarboe\Table\Fields\Traits\Nullable;
6
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
7
use Yaro\Jarboe\Table\Fields\Traits\Placeholder;
8
9
class Datetime extends AbstractField
10
{
11
    use Orderable;
12
    use Nullable;
13
    use Placeholder;
14
15
    protected $format = 'YYYY-MM-DD HH:mm:ss';
16
17 1
    public function format(string $momentJsFormat)
18
    {
19 1
        $this->format = $momentJsFormat;
20
21 1
        return $this;
22
    }
23
24 2
    public function getDateFormat()
25
    {
26 2
        return $this->format;
27
    }
28
29 3 View Code Duplication
    public function default($value)
30
    {
31 3
        if (is_object($value) && is_a($value, \DateTime::class)) {
32
            /** @var \DateTime $value */
33 1
            $this->default = $value->format('Y-m-d H:i:s');
34
        } else {
35 2
            $this->default = date('Y-m-d H:i:s', strtotime($value));
36
        }
37
38 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Yaro\Jarboe\Table\Fields\Datetime) is incompatible with the return type of the parent method Yaro\Jarboe\Table\Fields\AbstractField::default of type Yaro\Jarboe\Table\Fields\Traits\DefaultTrait.

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...
39
    }
40
41 1
    public function getListView($model)
42
    {
43 1
        return view('jarboe::crud.fields.datetime.list', [
44 1
            'model' => $model,
45 1
            'field' => $this,
46
        ]);
47
    }
48
49 1
    public function getEditFormView($model)
50
    {
51 1
        $template = $this->isReadonly() ? 'readonly' : 'edit';
52
53 1
        return view('jarboe::crud.fields.datetime.'. $template, [
54 1
            'model' => $model,
55 1
            'field' => $this,
56
        ]);
57
    }
58
59 1
    public function getCreateFormView()
60
    {
61 1
        return view('jarboe::crud.fields.datetime.create', [
62 1
            'field' => $this,
63
        ]);
64
    }
65
}
66