Completed
Push — master ( bdca4c...d26f0c )
by WEBEWEB
02:55
created

AbstractValidationStatus::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Validation\Status;
13
14
use WBW\Library\Core\Validation\API\ValidationStatusInterface;
15
16
/**
17
 * Abstract validation status.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Validation\Status
21
 * @abstract
22
 */
23
abstract class AbstractValidationStatus implements ValidationStatusInterface {
24
25
    /**
26
     * Get the code.
27
     *
28
     * @var string
29
     */
30
    private $code;
31
32
    /**
33
     * Message.
34
     *
35
     * @var bool
36
     */
37
    private $message;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param int $code The code.
43
     * @param string $message The message.
44
     */
45
    protected function __construct($code, $message) {
46
        $this->setCode($code);
47
        $this->setMessage($message);
48
    }
49
50
    /**
51
     * Get the code.
52
     *
53
     * @return int Returns the code.
54
     */
55
    public function getCode() {
56
        return $this->code;
57
    }
58
59
    /**
60
     * Get the message.
61
     *
62
     * @return string Returns the message.
63
     */
64
    public function getMessage() {
65
        return $this->message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->message; (boolean) is incompatible with the return type declared by the interface WBW\Library\Core\Validat...usInterface::getMessage of type string.

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...
66
    }
67
68
    /**
69
     * Set the code.
70
     *
71
     * @param int $code The code.
72
     * @return ValidationStatusInterface Returns this validation status.
73
     */
74
    public function setCode($code) {
75
        $this->code = $code;
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $code is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
76
        return $this;
77
    }
78
79
    /**
80
     * Set the message.
81
     *
82
     * @param string $message The message.
83
     * @return ValidationStatusInterface Returns this validation status.
84
     */
85
    public function setMessage($message) {
86
        $this->message = $message;
0 ignored issues
show
Documentation Bug introduced by
The property $message was declared of type boolean, but $message is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
87
        return $this;
88
    }
89
90
}
91