Completed
Push — master ( 91ab9a...64950e )
by Kevin
04:15
created

Attribute::clean()   D

Complexity

Conditions 16
Paths 32

Size

Total Lines 82
Code Lines 48

Duplication

Lines 48
Ratio 58.54 %

Code Coverage

Tests 47
CRAP Score 16.0173

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 48
loc 82
ccs 47
cts 49
cp 0.9592
rs 4.9422
cc 16
eloc 48
nc 32
nop 3
crap 16.0173

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Psr\Log\LoggerInterface;
7
8
class Attribute
9
{
10
    const BOOL      = 'ci_boo'; // boolean
11
    const CI_ENUM   = 'ci_enu'; // case-insensitive enumeration
12
    const CI_SSENUM = 'ci_sse'; // case-insensitive space-separated enumeration
13
    const INT       = 'ci_int'; // integer
14
    const JS        = 'cs_jsc'; // javascript
15
    const CI_STRING = 'ci_str'; // case-insensitive string
16
    const CS_STRING = 'cs_str'; // case-sensitive string
17
    const URI       = 'cs_uri'; // uri
18
    const UNKNOWN   = 'cs_unk'; // unknown
19
20
    /** @var string */
21
    private $name;
22
23
    /** @var string */
24
    private $type;
25
26
    /** @var null|mixed */
27
    private $value;
28
29
    /** @var bool */
30
    private $isStandard;
31
32
    /**
33
     * Constructor
34
     */
35 66
    public function __construct($name, $value)
36
    {
37 66
        $this->name = $name;
38 66
        $this->type = null;
39 66
        $this->value = $value;
40 66
        $this->isStandard = false;
41 66
    }
42
43
    /**
44
     * Getter for 'name'.
45
     */
46 59
    public function getName()
47
    {
48 59
        return $this->name;
49
    }
50
51
    /**
52
     * Getter for 'type'.
53
     */
54
    public function getType()
55
    {
56
        return $this->type;
57
    }
58
59
    /**
60
     * Chainable setter for 'type'.
61
     */
62 58
    public function setType($type)
63
    {
64 58
        $typeEnum = mb_substr($type, 0, 6);
65 58
        if ($typeEnum !== self::BOOL &&
66 58
            $typeEnum !== self::CI_ENUM &&
67 58
            $typeEnum !== self::CI_SSENUM &&
68 58
            $typeEnum !== self::INT &&
69 58
            $typeEnum !== self::JS &&
70 58
            $typeEnum !== self::CI_STRING &&
71 58
            $typeEnum !== self::CS_STRING &&
72 58
            $typeEnum !== self::URI &&
73 58
            $typeEnum !== self::UNKNOWN) {
74
            throw new \InvalidArgumentException('Invalid attribute type: ' . $typeEnum);
75
        }
76
77 58
        $this->type = $type;
78
79 58
        return $this;
80
    }
81
82
    /**
83
     * Getter for 'value'.
84
     */
85 4
    public function getValue()
86
    {
87 4
        return $this->value;
88
    }
89
90
    /**
91
     * Getter for 'isStandard'.
92
     */
93
    public function getIsStandard()
94
    {
95
        return $this->isStandard;
96
    }
97
98
    /**
99
     * Chainable setter for 'isStandard'.
100
     */
101 58
    public function setIsStandard($isStandard)
102
    {
103 58
        $this->isStandard = (bool) $isStandard;
104
105 58
        return $this;
106
    }
107
108 58
    public function clean(Configuration $configuration, Element $element, LoggerInterface $logger)
109
    {
110 58
        if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
111
            return true;
112
        }
113
114
        // Remove non-standard attributes.
115 58
        if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT && $this->isStandard === false) {
116 9
            $logger->debug('Removing non-standard attribute "' . $this->name . '" from ' . $element);
117
118 9
            return false;
119
        }
120
121
        // Validate attribute value.
122 58
        list($caseSensitivity, $attributeType) = explode('_', $this->type);
123
124
        // Standard is case-insensitive attribute values should be lower case.
125 58
        if ($caseSensitivity == 'ci' && $this->value !== true) {
126 17
            $newValue = strtolower($this->value);
127 17 View Code Duplication
            if ($newValue !== $this->value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128 3
                $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is case-insensitive.  The value has been converted to lower case.');
129 3
                $this->value = $newValue;
130 3
            }
131 17
        }
132
133
        // Validate value types.
134
        switch ($attributeType) {
135 58 View Code Duplication
        case 'boo': // boolean
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136 2
            $cleanResult = $this->cleanAttributeBoolean(
137 2
                $configuration,
138 2
                $element,
139
                $logger
140 2
            );
141 2
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
142 2
                return $cleanResult;
143
            }
144
145 2
            break;
146
147 57
        case 'enu': // enumeration
148
            /// @todo
149
            break;
150
151 57 View Code Duplication
        case 'int': // integer
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152 4
            $cleanResult = $this->cleanAttributeInteger(
153 4
                $configuration,
154 4
                $element,
155
                $logger
156 4
            );
157 4
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
158 4
                return $cleanResult;
159
            }
160
161 4
            break;
162
163 53 View Code Duplication
        case 'str': // string
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164 39
            $cleanResult = $this->cleanAttributeString(
165 39
                $configuration,
166 39
                $element,
167
                $logger
168 39
            );
169 39
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
170 38
                return $cleanResult;
171
            }
172
173 28
            break;
174
175 27 View Code Duplication
        case 'uri': // URI
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176 20
            $cleanResult = $this->cleanAttributeUri(
177 20
                $configuration,
178 20
                $element,
179
                $logger
180 20
            );
181 20
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
182 20
                return $cleanResult;
183
            }
184
185 20
            break;
186
        }
187
188 47
        return true;
189
    }
190
191 2 View Code Duplication
    private function cleanAttributeBoolean(Configuration $configuration, Element $element, LoggerInterface $logger)
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193 2
        if ($this->value !== true) {
194 1
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" is a boolean attribute.  The value has been removed.');
195 1
            $this->value = true;
196 1
        }
197
198 2
        return true;
199
    }
200
201 4
    private function cleanAttributeInteger(Configuration $configuration, Element $element, LoggerInterface $logger)
202
    {
203 4
        if ($this->value === true ||
204 4
            $this->value == '') {
205 1 View Code Duplication
            if ($configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206 1
                $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is required to be an positive, non-zero integer.  The value is invalid, therefore the attribute has been removed.');
207 1
            }
208
209 1
            return false;
210
        }
211
212 4
        if (!is_int($this->value)) {
213 4
            $origonalValue = (string) $this->value;
214 4
            $this->value = (int) $this->value;
215 4 View Code Duplication
            if ($origonalValue != ((string) $this->value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216 2
                $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is required to be an positive, non-zero integer.  The value has been converted to an integer.');
217 2
            }
218 4
        }
219
220 4 View Code Duplication
        if ($this->value <= 0 &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221 4
        $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
222 1
            $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->value . '" is required to be an positive, non-zero integer.  The value is invalid, therefore the attribute has been removed.');
223
224 1
            return false;
225
        }
226
227 4
        return true;
228
    }
229
230 39 View Code Duplication
    private function cleanAttributeString(Configuration $configuration, Element $element, LoggerInterface $logger)
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232 39
        if ($this->value === true) {
233
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" requires a string value.  The value is invalid, therefore the attribute has been removed.');
234
235
            return false;
236
        }
237
238 39
        return true;
239
    }
240
241 20 View Code Duplication
    private function cleanAttributeUri(Configuration $configuration, Element $element, LoggerInterface $logger)
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        // Check for empty attribute.
244 20
        if ($this->value === true) {
245
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" requires a URI.  The value is invalid, therefore the attribute has been removed.');
246
247
            return false;
248
        }
249
250
        /// @todo
251
252 20
        return true;
253
    }
254
255 60
    public function __toString()
256
    {
257 60
        $output = $this->name;
258 60
        if ($this->value === true) {
259 4
            return $output;
260
        }
261
262
        /// @todo Escape double quotes in value.
263 60
        return $output . '="' . $this->value . '"';
264
    }
265
}
266