Completed
Push — master ( 01480a...d975fd )
by Alex
07:58
created

Response::getDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Adapter\FileSystem;
12
13
use FeedIo\Adapter\ResponseInterface;
14
15
/**
16
 *
17
 */
18
class Response implements ResponseInterface
19
{
20
21
    /**
22
     * @var string
23
     */
24
    protected $fileContent;
25
26
    /**
27
     * @var \DateTime
28
     */
29
    protected $lastModified;
30
31
    /**
32
     * @param string    $fileContent
33
     * @param \DateTime $lastModified
34
     */
35 1
    public function __construct(string $fileContent, \DateTime $lastModified)
36
    {
37 1
        $this->fileContent  = $fileContent;
38 1
        $this->lastModified = $lastModified;
39 1
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function getDuration(): int
45
    {
46
        return 0;
47
    }
48
49
    /**
50
     * @return int
51
     */
52 1
    public function getStatusCode(): int
53
    {
54 1
        return 0;
55
    }
56
57
    /**
58
    * @return boolean
59
    */
60 1
    public function isModified() : bool
61
    {
62 1
        return true;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getBody() : ? string
69 1
    {
70
        return $this->fileContent;
71 1
    }
72
73
    /**
74
     * @return iterable
75
     */
76
    public function getHeaders() : iterable
77 1
    {
78
        return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface FeedIo\Adapter\ResponseInterface::getHeaders of type FeedIo\Adapter\iterable.

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...
79 1
    }
80
81
    /**
82
     * @param  string $name
83
     * @return iterable
84
     */
85
    public function getHeader(string $name) : iterable
86
    {
87
        return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface FeedIo\Adapter\ResponseInterface::getHeader of type FeedIo\Adapter\iterable.

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...
88
    }
89
90
    /**
91
     * @return \DateTime
92
     */
93
    public function getLastModified() : ?\DateTime
94
    {
95
        return $this->lastModified;
96
    }
97
}
98