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

AbstractApiProblem::getDetail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * @param string $title
34
     *
35
     * @return ApiProblemInterface
36
     */
37 1
    public function withTitle(string $title): ApiProblemInterface
38
    {
39 1
        $clone = clone $this;
40 1
        $clone->title = $title;
41
42 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...
43
    }
44
45
    /**
46
     * @return string
47
     */
48 2
    public function getTitle(): string
49
    {
50 2
        return $this->title;
51
    }
52
53
    /**
54
     * @param string|null $detail
55
     *
56
     * @return ApiProblemInterface
57
     */
58 1
    public function withDetail(string $detail = null): ApiProblemInterface
59
    {
60 1
        $clone = clone $this;
61 1
        $clone->detail = $detail;
62
63 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...
64
    }
65
66
    /**
67
     * @return string|null
68
     */
69 2
    public function getDetail()
70
    {
71 2
        return $this->detail;
72
    }
73
74
    /**
75
     * @param string|null $instance
76
     *
77
     * @return ApiProblemInterface
78
     */
79 1
    public function withInstance(string $instance = null): ApiProblemInterface
80
    {
81 1
        $clone = clone $this;
82 1
        $clone->instance = $instance;
83
84 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...
85
    }
86
87
    /**
88
     * @return string|null
89
     */
90 2
    public function getInstance()
91
    {
92 2
        return $this->instance;
93
    }
94
}
95