Completed
Push — master ( 0c75f6...35d3bb )
by Stéphane
14:38
created

Datetime::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
1
<?php namespace Rocket\Entities\Fields;
2
3
use Rocket\Entities\Field;
4
5
/**
6
 * DateTime Field
7
 *
8
 * @property DateTime $value The field's value
9
 */
10
class Datetime extends Field
11
{
12
    public $table = 'field_datetime';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $casts = [
18
        'value' => 'datetime',
19
    ];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 9
    protected function prepareValue($value)
25
    {
26 9
        return $this->fromDateTime($value);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function toArray()
33
    {
34 3
        return $this->serializeDate($this->getAttribute('value'));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serializeD...getAttribute('value')); (string) is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Arrayable::toArray of type array.

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...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 12
    protected function isValid($value)
41
    {
42
        try {
43 12
            $this->asDateTime($value);
44
45 9
            return true;
46 3
        } catch (\InvalidArgumentException $e) {
47 3
            return false;
48
        }
49
    }
50
}
51