Completed
Pull Request — master (#28)
by Christopher
08:40 queued 03:24
created

XMLDateInterval::handleChar()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 5
eloc 10
nc 6
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
    public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS')
15
    {
16
        if ($intervalSpec[0] == '-') {
17
            $intervalSpec = substr($intervalSpec, 1);
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...
18
            $this->invert = true;
0 ignored issues
show
Documentation Bug introduced by
The property $invert was declared of type integer, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
19
        }
20
        parent::__construct($intervalSpec);
21
        $this->pattern = trim($pattern);
22
23
        $this->patternLen = strlen($this->pattern);
24
    }
25
26
    /**
27
     * formating the interval like ISO 8601 (PnYnMnDTnHnMnS).
28
     *
29
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
     */
31
    public function __toString()
32
    {
33
        $sReturn = $this->handleSign();
34
        $tSeen = false;
35
        for ($i = 0; $i < $this->patternLen; $i++) {
36
            $sReturn .= $this->handleChar($i, $tSeen);
37
        }
38
        return $sReturn;
39
    }
40
41
    private function handleSign()
42
    {
43
        if ($this->invert === 1) {
44
            return '-';
45
        }
46
    }
47
48
    private function handleChar($i, &$tSeen)
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...
49
    {
50
        switch ($this->pattern[$i]) {
51
            case 'n':
52
                $v = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]);
53
                return $this->$v;
54
            case 'T':
55
                $tSeen = true;
56
                return 'T';
57
            default:
58
                return $this->pattern[$i];
59
        }
60
    }
61
}
62