Completed
Branch develop (22789e)
by Mariano
08:33
created

SimplePropertyParser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 109
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setFilters() 0 5 1
A setValidators() 0 5 1
A getName() 0 4 1
A parse() 0 8 1
A cloneValue() 0 7 2
A validate() 0 10 3
A runFilters() 0 11 3
1
<?php
2
namespace Mcustiel\SimpleRequest\Strategies\Properties;
3
4
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser;
5
use Mcustiel\SimpleRequest\Exception\InvalidValueException;
6
7
class SimplePropertyParser implements PropertyParser
8
{
9
    /**
10
     *
11
     * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[]
12
     */
13
    protected $validators;
14
    /**
15
     *
16
     * @var \Mcustiel\SimpleRequest\Interfaces\FilterInterface[]
17
     */
18
    protected $filters;
19
    /**
20
     *
21
     * @var string
22
     */
23
    protected $name;
24
25
    public function __construct($name)
26
    {
27
        $this->name = $name;
28
    }
29
30
    public function setFilters(array $filters)
31
    {
32
        $this->filters = $filters;
33
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Mcustiel\SimpleRequest\S...es\SimplePropertyParser) is incompatible with the return type declared by the interface Mcustiel\SimpleRequest\S...pertyParser::setFilters of type Mcustiel\SimpleRequest\PropertyParser.

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...
34
    }
35
36
    public function setValidators(array $validators)
37
    {
38
        $this->validators = $validators;
39
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Mcustiel\SimpleRequest\S...es\SimplePropertyParser) is incompatible with the return type declared by the interface Mcustiel\SimpleRequest\S...tyParser::setValidators of type Mcustiel\SimpleRequest\PropertyParser.

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...
40
    }
41
42
    public function getName()
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * Filters and validates a value. And return the filtered value.
49
     * It throws an exception if the value is not valid.
50
     *
51
     * @param mixed $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. 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...
52
     * @return mixed
53
     *
54
     * @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException
55
     */
56
    public function parse($propertyValue)
57
    {
58
        $return = $this->cloneValue($propertyValue);
59
        $return = $this->runFilters($return);
60
        $this->validate($return);
61
62
        return $return;
63
    }
64
65
    /**
66
     * Returns a copy of the received value.
67
     *
68
     * @param mixed $value
69
     * @return mixed
70
     */
71
    protected function cloneValue($value)
72
    {
73
        if (is_object($value)) {
74
            return clone $value;
75
        }
76
        return $value;
77
    }
78
79
    /**
80
     * Checks the value against all validators.
81
     *
82
     * @param mixed $value
83
     *
84
     * @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException
85
     */
86
    protected function validate($value)
87
    {
88
        foreach ($this->validators as $validator) {
89
            if (!$validator->validate($value)) {
90
                throw new InvalidValueException(
91
                    "Field {$this->name}, was set with invalid value: " . var_export($value, true)
92
                );
93
            }
94
        }
95
    }
96
97
    /**
98
     * Run all the filters on the value.
99
     *
100
     * @param mixed $value
101
     *
102
     * @return mixed
103
     */
104
    protected function runFilters($value)
105
    {
106
        $return = $value;
107
        if ($return !== null) {
108
            foreach ($this->filters as $filter) {
109
                $return = $filter->filter($return);
110
            }
111
        }
112
113
        return $return;
114
    }
115
}
116