Escaper   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 2
dl 0
loc 173
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
B escapeAttributes() 0 18 7
B escape() 0 18 7
A escapeUrlParameter() 0 4 1
A isEscapeHtml() 0 4 1
A setEscapeHtml() 0 6 1
A isEscapeHtmlAttr() 0 4 1
A setEscapeHtmlAttr() 0 6 1
A isEscapeJs() 0 4 1
A setEscapeJs() 0 6 1
A isEscapeCss() 0 4 1
A setEscapeCss() 0 6 1
A isEscapeUrl() 0 4 1
A setEscapeUrl() 0 6 1
A getUrlsAttributes() 0 4 1
1
<?php
2
3
namespace NatePage\EasyHtmlElement;
4
5
use Zend\Escaper\Escaper as BaseEscaper;
6
7
class Escaper extends BaseEscaper implements EscaperInterface
8
{
9
    /** @var array The special escaping types */
10
    private $specialEscapingTypes = array('script', 'style');
11
12
    /** @var array The urls attributes */
13
    private $urlsAttributes = array('href', 'src');
14
15
    /** @var bool Determine if html is escaped or not */
16
    private $escapeHtml = true;
17
18
    /** @var bool Determine if html attributes are escaped or not */
19
    private $escapeHtmlAttr = true;
20
21
    /** @var bool Determine if javascript is escaped or not */
22
    private $escapeJs = true;
23
24
    /** @var bool Determine if css is escaped or not */
25
    private $escapeCss = true;
26
27
    /** @var bool Determine if urls parameters are escaped or not */
28
    private $escapeUrl = true;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function escapeAttributes(array $attributes): array
34
    {
35
        if ($this->escapeHtmlAttr || $this->escapeUrl) {
36
            foreach ($attributes as $attr => $value) {
37
                if (is_array($value)) {
38
                    $value = $this->escapeAttributes($value);
39
                } elseif (!in_array($attr, $this->urlsAttributes)) {
40
                    if ($this->escapeHtmlAttr) {
41
                        $value = $this->escapeHtmlAttr($value);
42
                    }
43
                }
44
45
                $attributes[$attr] = $value;
46
            }
47
        }
48
49
        return $attributes;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function escape(ElementInterface $element): ElementInterface
56
    {
57
        if ($this->escapeHtml && !in_array($element->getType(), $this->specialEscapingTypes)) {
58
            $element->setText($this->escapeHtml($element->getText()));
59
        }
60
61
        $element->setAttributes($this->escapeAttributes($element->getAttributes()));
62
63
        if ($this->escapeJs && 'script' == $element->getType()) {
64
            $element->setText($this->escapeJs($element->getText()));
65
        }
66
67
        if ($this->escapeCss && 'style' == $element->getType()) {
68
            $element->setText($this->escapeCss($element->getText()));
69
        }
70
71
        return $element;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function escapeUrlParameter(string $parameter): string
78
    {
79
        return $this->escapeUrl($parameter);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function isEscapeHtml(): bool
86
    {
87
        return $this->escapeHtml;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function setEscapeHtml(bool $escapeHtml = true): EscaperInterface
94
    {
95
        $this->escapeHtml = $escapeHtml;
96
97
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NatePage\EasyHtmlElement\Escaper) is incompatible with the return type declared by the interface NatePage\EasyHtmlElement...nterface::setEscapeHtml 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...
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function isEscapeHtmlAttr(): bool
104
    {
105
        return $this->escapeHtmlAttr;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function setEscapeHtmlAttr(bool $escapeHtmlAttr = true): EscaperInterface
112
    {
113
        $this->escapeHtmlAttr = $escapeHtmlAttr;
114
115
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NatePage\EasyHtmlElement\Escaper) is incompatible with the return type declared by the interface NatePage\EasyHtmlElement...face::setEscapeHtmlAttr 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...
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function isEscapeJs(): bool
122
    {
123
        return $this->escapeJs;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function setEscapeJs(bool $escapeJs = true): EscaperInterface
130
    {
131
        $this->escapeJs = $escapeJs;
132
133
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NatePage\EasyHtmlElement\Escaper) is incompatible with the return type declared by the interface NatePage\EasyHtmlElement...rInterface::setEscapeJs 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...
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function isEscapeCss(): bool
140
    {
141
        return $this->escapeCss;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function setEscapeCss(bool $escapeCss = true): EscaperInterface
148
    {
149
        $this->escapeCss = $escapeCss;
150
151
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NatePage\EasyHtmlElement\Escaper) is incompatible with the return type declared by the interface NatePage\EasyHtmlElement...Interface::setEscapeCss 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...
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function isEscapeUrl(): bool
158
    {
159
        return $this->escapeUrl;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function setEscapeUrl(bool $escapeUrl = true): EscaperInterface
166
    {
167
        $this->escapeUrl = $escapeUrl;
168
169
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NatePage\EasyHtmlElement\Escaper) is incompatible with the return type declared by the interface NatePage\EasyHtmlElement...Interface::setEscapeUrl 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...
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getUrlsAttributes(): array
176
    {
177
        return $this->urlsAttributes;
178
    }
179
}
180