DetailedErrorMessage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace MetaHydrator\ErrorMessage;
3
4
/**
5
 * A descriptor for a specific failure in form data parsing/validation.
6
 *
7
 * Class DetailedErrorMessage
8
 * @package MetaHydrator\ErrorMessage
9
 */
10
class DetailedErrorMessage extends SimpleErrorMessage
11
{
12
    /** @var string */
13
    private $details;
14
    /** @return string */
15
    public function getDetails() { return $this->details; }
16
17
    /**
18
     * FieldError constructor.
19
     * @Important
20
     * @param string $error
0 ignored issues
show
Bug introduced by
There is no parameter named $error. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
     * @param string $details
22
     */
23
    public function __construct($message = '', $details = '')
24
    {
25
        parent::__construct($message);
26
        $this->details = $details;
27
    }
28
29
    /**
30
     * @return array
31
     */
32
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
    {
34
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('message' =...ls' => $this->details); (array<string,string>) is incompatible with the return type of the parent method MetaHydrator\ErrorMessag...rMessage::jsonSerialize 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...
35
            'message' => $this->getMessage(),
36
            'details' => $this->details
37
        ];
38
    }
39
}
40