Completed
Push — master ( 64950e...559766 )
by Kevin
03:41
created

Attribute::clean()   C

Complexity

Conditions 15
Paths 29

Size

Total Lines 78
Code Lines 46

Duplication

Lines 48
Ratio 61.54 %

Code Coverage

Tests 47
CRAP Score 15

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 48
loc 78
ccs 47
cts 47
cp 1
rs 5.15
cc 15
eloc 46
nc 29
nop 3
crap 15

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 84
    public function __construct($name, $value)
36
    {
37 84
        $this->name = $name;
38 84
        $this->type = null;
39 84
        $this->value = $value;
40 84
        $this->isStandard = false;
41 84
    }
42
43
    /**
44
     * Getter for 'name'.
45
     */
46 62
    public function getName()
47
    {
48 62
        return $this->name;
49
    }
50
51
    /**
52
     * Getter for 'type'.
53
     */
54 2
    public function getType()
55
    {
56 2
        return $this->type;
57
    }
58
59
    /**
60
     * Chainable setter for 'type'.
61
     */
62 74
    public function setType($type)
63
    {
64 74
        $typeEnum = mb_substr($type, 0, 6);
65 74
        if ($typeEnum !== self::BOOL &&
66 74
            $typeEnum !== self::CI_ENUM &&
67 74
            $typeEnum !== self::CI_SSENUM &&
68 74
            $typeEnum !== self::INT &&
69 74
            $typeEnum !== self::JS &&
70 74
            $typeEnum !== self::CI_STRING &&
71 74
            $typeEnum !== self::CS_STRING &&
72 74
            $typeEnum !== self::URI &&
73 74
            $typeEnum !== self::UNKNOWN) {
74 1
            throw new \InvalidArgumentException('Invalid attribute type: ' . $typeEnum);
75
        }
76
77 74
        $this->type = $type;
78
79 74
        return $this;
80
    }
81
82
    /**
83
     * Getter for 'value'.
84
     */
85 18
    public function getValue()
86
    {
87 18
        return $this->value;
88
    }
89
90
    /**
91
     * Getter for 'isStandard'.
92
     */
93 2
    public function getIsStandard()
94
    {
95 2
        return $this->isStandard;
96
    }
97
98
    /**
99
     * Chainable setter for 'isStandard'.
100
     */
101 74
    public function setIsStandard($isStandard)
102
    {
103 74
        $this->isStandard = (bool) $isStandard;
104
105 74
        return $this;
106
    }
107
108 73
    public function clean(Configuration $configuration, Element $element, LoggerInterface $logger)
109
    {
110 73
        if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
111 1
            return true;
112
        }
113
114
        // Remove non-standard attributes.
115 72
        if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT && $this->isStandard === false) {
116 10
            $logger->debug('Removing non-standard attribute "' . $this->name . '" from ' . $element);
117
118 10
            return false;
119
        }
120
121
        // Validate attribute value.
122 71
        list($caseSensitivity, $attributeType) = explode('_', $this->type);
123
124
        // Standard is case-insensitive attribute values should be lower case.
125 71
        if ($caseSensitivity == 'ci' && $this->value !== true) {
126 25
            $newValue = strtolower($this->value);
127 25 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 4
                $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is case-insensitive.  The value has been converted to lower case.');
129 4
                $this->value = $newValue;
130 4
            }
131 25
        }
132
133
        // Validate value types.
134
        switch ($attributeType) {
135 71 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 5
            $cleanResult = $this->cleanAttributeBoolean(
137 5
                $configuration,
138 5
                $element,
139
                $logger
140 5
            );
141 5
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
142 4
                return $cleanResult;
143
            }
144
145 3
            break;
146
147 67 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...
148 10
            $cleanResult = $this->cleanAttributeInteger(
149 10
                $configuration,
150 10
                $element,
151
                $logger
152 10
            );
153 10
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
154 10
                return $cleanResult;
155
            }
156
157 6
            break;
158
159 59 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...
160 43
            $cleanResult = $this->cleanAttributeString(
161 43
                $configuration,
162 43
                $element,
163
                $logger
164 43
            );
165 43
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
166 42
                return $cleanResult;
167
            }
168
169 30
            break;
170
171 31 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...
172 24
            $cleanResult = $this->cleanAttributeUri(
173 24
                $configuration,
174 24
                $element,
175
                $logger
176 24
            );
177 24
            if ($configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
178 24
                return $cleanResult;
179
            }
180
181 22
            break;
182
        }
183
184 50
        return true;
185
    }
186
187 5 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...
188
    {
189 5
        if ($this->value !== true) {
190 3
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" is a boolean attribute.  The value has been removed.');
191 3
            $this->value = true;
192 3
        }
193
194 5
        return true;
195
    }
196
197 10
    private function cleanAttributeInteger(Configuration $configuration, Element $element, LoggerInterface $logger)
198
    {
199 10
        if ($this->value === true ||
200 10
            $this->value == '') {
201 2 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...
202 2
                $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.');
203 2
            }
204
205 2
            return false;
206
        }
207
208 9
        if (!is_int($this->value)) {
209 9
            $origonalValue = (string) $this->value;
210 9
            $this->value = (int) $this->value;
211 9 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...
212 3
                $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.');
213 3
            }
214 9
        }
215
216 9 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...
217 9
        $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
218 3
            $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.');
219
220 3
            return false;
221
        }
222
223 7
        return true;
224
    }
225
226 43 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...
227
    {
228 43
        if ($this->value === true) {
229 1
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" requires a string value.  The value is missing, therefore the attribute value is set to the attribute name.');
230
231 1
            $this->value = $this->name;
232 1
        }
233
234 43
        return true;
235
    }
236
237 24 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...
238
    {
239
        // Check for empty attribute.
240 24
        if ($this->value === true) {
241 1
            $logger->debug('Within ' . $element . ', the attribute "' . $this->name . '" requires a URI.  The value is invalid, therefore the attribute has been removed.');
242
243 1
            return false;
244
        }
245
246
        /// @todo
247
248 23
        return true;
249
    }
250
251 63
    public function __toString()
252
    {
253 63
        $output = $this->name;
254 63
        if ($this->value === true) {
255 4
            return $output;
256
        }
257
258
        /// @todo Escape double quotes in value.
259 63
        return $output . '="' . $this->value . '"';
260
    }
261
}
262