Passed
Pull Request — master (#2)
by Dominik
02:16
created

AbstractApiProblem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\ApiProblem;
6
7
abstract class AbstractApiProblem implements ApiProblemInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $title;
13
14
    /**
15
     * @var string
16
     */
17
    private $detail;
18
19
    /**
20
     * @var string|null
21
     */
22
    private $instance;
23
24
    /**
25
     * @param string $title
26
     */
27 2
    public function __construct(string $title)
28
    {
29 2
        $this->title = $title;
30 2
    }
31
32
    /**
33
     * @return array
34
     */
35 2
    public function getHeaders(): array
36
    {
37 2
        return [];
38
    }
39
40
    /**
41
     * @param string $title
42
     *
43
     * @return ApiProblemInterface
44
     */
45 1
    public function withTitle(string $title): ApiProblemInterface
46
    {
47 1
        $clone = clone $this;
48 1
        $clone->title = $title;
49
50 1
        return $clone;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $clone; (Chubbyphp\ApiHttp\ApiProblem\AbstractApiProblem) is incompatible with the return type declared by the interface Chubbyphp\ApiHttp\ApiPro...lemInterface::withTitle of type self.

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...
51
    }
52
53
    /**
54
     * @return string
55
     */
56 2
    public function getTitle(): string
57
    {
58 2
        return $this->title;
59
    }
60
61
    /**
62
     * @param string|null $detail
63
     *
64
     * @return ApiProblemInterface
65
     */
66 1
    public function withDetail(string $detail = null): ApiProblemInterface
67
    {
68 1
        $clone = clone $this;
69 1
        $clone->detail = $detail;
70
71 1
        return $clone;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $clone; (Chubbyphp\ApiHttp\ApiProblem\AbstractApiProblem) is incompatible with the return type declared by the interface Chubbyphp\ApiHttp\ApiPro...emInterface::withDetail of type self.

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...
72
    }
73
74
    /**
75
     * @return string|null
76
     */
77 2
    public function getDetail()
78
    {
79 2
        return $this->detail;
80
    }
81
82
    /**
83
     * @param string|null $instance
84
     *
85
     * @return ApiProblemInterface
86
     */
87 1
    public function withInstance(string $instance = null): ApiProblemInterface
88
    {
89 1
        $clone = clone $this;
90 1
        $clone->instance = $instance;
91
92 1
        return $clone;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $clone; (Chubbyphp\ApiHttp\ApiProblem\AbstractApiProblem) is incompatible with the return type declared by the interface Chubbyphp\ApiHttp\ApiPro...Interface::withInstance of type self.

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...
93
    }
94
95
    /**
96
     * @return string|null
97
     */
98 2
    public function getInstance()
99
    {
100 2
        return $this->instance;
101
    }
102
}
103