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

XMLDateInterval::handleN()   B

Complexity

Conditions 5
Paths 12

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 8.8571
cc 5
eloc 5
nc 12
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
94
            return $this->f === 0 ? $this->$componentProperty : trim($this->$componentProperty . '.' . $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...
95
        }
96
        return $this->$componentProperty;
97
    }
98
99
    /**
100
     * @param bool $tSeen
101
     *
102
     * @return string
103
     */
104
    private function handleT(&$tSeen)
105
    {
106
        $tSeen = true;
107
        return 'T';
108
    }
109
110
    /**
111
     * @param int $i
112
     *
113
     * @return string
114
     */
115
    private function handleOther($i)
116
    {
117
        return $this->pattern[$i];
118
    }
119
120
    public function __get($name)
121
    {
122
        if ($name == 'f') {
123
            return $this->fractionOfSecond;
124
        }
125
    }
126
}
127