Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Date::months()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Yaro\Jarboe\Table\Fields\Traits\Inline;
6
use Yaro\Jarboe\Table\Fields\Traits\Nullable;
7
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
8
use Yaro\Jarboe\Table\Fields\Traits\Placeholder;
9
10
class Date extends AbstractField
11
{
12
    use Orderable;
13
    use Nullable;
14
    use Inline;
15
    use Placeholder;
16
17
    protected $format = 'YYYY-MM-DD';
18
    protected $months = 1;
19
20 3 View Code Duplication
    public function default($value)
21
    {
22 3
        if (is_object($value) && is_a($value, \DateTime::class)) {
23
            /** @var \DateTime $value */
24 1
            $this->default = $value->format('Y-m-d');
25
        } else {
26 2
            $this->default = date('Y-m-d', strtotime($value));
27
        }
28
29 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Yaro\Jarboe\Table\Fields\Date) 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...
30
    }
31
32 1
    public function format(string $momentJsFormat)
33
    {
34 1
        $this->format = $momentJsFormat;
35
36 1
        return $this;
37
    }
38
39 2
    public function getDateFormat()
40
    {
41 2
        return $this->format;
42
    }
43
44 2
    public function months(int $months)
45
    {
46 2
        $this->months = $months;
47
48 2
        return $this;
49
    }
50
51 2
    public function getMonths()
52
    {
53 2
        return $this->months;
54
    }
55
56 1
    public function getListView($model)
57
    {
58 1
        return view('jarboe::crud.fields.date.list', [
59 1
            'model' => $model,
60 1
            'field' => $this,
61
        ]);
62
    }
63
64 1
    public function getEditFormView($model)
65
    {
66 1
        $template = $this->isReadonly() ? 'readonly' : 'edit';
67
68 1
        return view('jarboe::crud.fields.date.'. $template, [
69 1
            'model' => $model,
70 1
            'field' => $this,
71
        ]);
72
    }
73
74 1
    public function getCreateFormView()
75
    {
76 1
        return view('jarboe::crud.fields.date.create', [
77 1
            'field' => $this,
78
        ]);
79
    }
80
}
81