Test Failed
Pull Request — master (#28)
by Christopher
02:37
created

xsNMTOKENS   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 24.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 11
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B fixValue() 0 16 5
A isOK() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace AlgoWeb\xsdTypes;
3
4
use AlgoWeb\xsdTypes\Facets\LengthTrait;
5
6
/**
7
 * The type xsd:NMTOKENS represents a list of NMTOKEN values separated by whitespace.  There must be at least one
8
 * NMTOKEN in the list.
9
 *
10
 * @package AlgoWeb\xsdTypes
11
 */
12
class xsNMTOKENS extends xsAnySimpleType
13
{
14
    use LengthTrait;
15
16
    /**
17
     * Construct.
18
     *
19
     * @param array $value
20
     */
21
    public function __construct($value)
22
    {
23
        parent::__construct($value);
24
        $this->setWhiteSpaceFacet('collapse');
25
        $this->setMinLengthFacet(1);
26
    }
27
28
    protected function fixValue()
29
    {
30
        if (is_string($this->value)) {
31
            $parts = explode(' ', $this->value);
32
            $this->value = [];
33
            foreach ($parts as $part) {
34
                if (0 != strlen(trim($part))) {
35
                    $this->value[] = new xsNMTOKEN(trim($part));
36
                }
37
            }
38
        }
39
        assert(is_array($this->value), 'Somehow, xsNMTOKENs ended up not being an array.');
40
        foreach ($this->value as $v) {
41
            $v->fixValue($v);
42
        }
43
    }
44
45 View Code Duplication
    protected function isOK()
46
    {
47
        if (!is_array($this->value)) {
48
            throw new \InvalidArgumentException(
49
                'The provided value for ' . __CLASS__ . ' must be an array of type xsNMTOKEN.'
50
            );
51
        }
52
        foreach ($this->value as $v) {
53
            $v->isOKInternal();
54
        }
55
    }
56
}
57