XMLDateInterval::handleN()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 5
nc 8
nop 2
1
<?php
2
3
namespace AlgoWeb\xsdTypes\AxillaryClasses;
4
5
class XMLDateInterval extends \DateInterval
6
{
7
    /**
8
     * formating string like ISO 8601 (PnYnMnDTnHnMnS).
9
     */
10
    const INTERVAL_ISO8601 = 'P%yY%mM%dDT%hH%iM%sS';
11
    private $pattern;
12
    private $patternLen;
13
14
    private $fractionOfSecond;
15
16
    public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS')
17
    {
18
        if (-1 === version_compare(PHP_VERSION, '7.1.0')) {
19
            $intervalSpec = $this->handleFractionOfSecond($intervalSpec);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $intervalSpec. This often makes code more readable.
Loading history...
20
        }
21
        parent::__construct(trim($intervalSpec, '-'));
22
        if ($intervalSpec[0] == '-') {
23
            $this->invert = 1;
24
        }
25
        $this->pattern = trim($pattern);
26
        $this->patternLen = strlen($this->pattern);
27
    }
28
29
    private function handleFractionOfSecond($intervalSpec)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31
        $re = '/(?:[0-5][0-9]|60)(.\d+)S/';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $re. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
32
        preg_match_all($re, $intervalSpec, $matches, PREG_SET_ORDER, 0);
33
        if (1 != count($matches) || strpos($intervalSpec, '.') === false) {
34
            return $intervalSpec;
35
        }
36
        $this->fractionOfSecond = trim($matches[0][1], '.');
37
        return str_replace($matches[0][1], '', $intervalSpec);
38
    }
39
40
    /**
41
     * formating the interval like ISO 8601 (PnYnMnDTnHnMnS).
42
     *
43
     * @return string|null
44
     */
45
    public function __toString()
46
    {
47
        $sReturn = $this->handleSign();
48
        $tSeen = false;
49
        for ($i = 0; $i < $this->patternLen; $i++) {
50
            $sReturn .= $this->handleChar($i, $tSeen);
51
        }
52
        return $sReturn;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    private function handleSign()
59
    {
60
        if ($this->invert === 1) {
61
            return '-';
62
        }
63
        return '';
64
    }
65
66
    /**
67
     * @param int  $i
68
     * @param bool $tSeen
69
     *
70
     * @return string
71
     */
72
    private function handleChar($i, &$tSeen)
73
    {
74
        switch ($this->pattern[$i]) {
75
            case 'n':
76
                return $this->handleN($i, $tSeen);
77
            case 'T':
78
                return $this->handleT($tSeen);
79
            default:
80
                return $this->handleOther($i);
81
        }
82
    }
83
84
    /**
85
     * @param int  $i
86
     * @param bool $tSeen
87
     *
88
     * @return string
89
     */
90
    private function handleN($i, $tSeen)
91
    {
92
        $componentProperty = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]);
93
        if ('s' === $componentProperty) {
94
            return $this->handleS();
95
        }
96
        return $this->$componentProperty;
97
    }
98
99
    private function handleS()
100
    {
101
        return $this->f === 0 ? $this->s : trim($this->s . '.' . $this->f, '.');
0 ignored issues
show
Documentation introduced by
The property f does not exist on object<AlgoWeb\xsdTypes\...lasses\XMLDateInterval>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
    }
103
104
    /**
105
     * @param bool $tSeen
106
     *
107
     * @return string
108
     */
109
    private function handleT(&$tSeen)
110
    {
111
        $tSeen = true;
112
        return 'T';
113
    }
114
115
    /**
116
     * @param int $i
117
     *
118
     * @return string
119
     */
120
    private function handleOther($i)
121
    {
122
        return $this->pattern[$i];
123
    }
124
125
    public function __get($name)
126
    {
127
        if ($name == 'f') {
128
            return $this->fractionOfSecond;
129
        }
130
    }
131
}
132