Styles::readNext()   B
last analyzed

Complexity

Conditions 11
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.3166
c 0
b 0
f 0
cc 11
nc 6
nop 0

How to fix   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 Akeneo\Component\SpreadsheetParser\Xlsx;
4
5
/**
6
 * Spreadsheet styles
7
 *
8
 * @author    Antoine Guigan <[email protected]>
9
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
10
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
11
 */
12
class Styles extends AbstractXMLDictionnary
13
{
14
    /**
15
     * @staticvar int Default format
16
     */
17
    const FORMAT_DEFAULT = 0;
18
19
    /**
20
     * @staticvar int Date format
21
     */
22
    const FORMAT_DATE = 1;
23
24
    /**
25
     * @var array
26
     */
27
    protected $nativeDateFormats = [14, 15, 16, 17, 18, 19, 20, 21, 22];
28
29
    /**
30
     * @var array
31
     */
32
    protected $numberFormats = [];
33
34
    /**
35
     * @var boolean
36
     */
37
    protected $inXfs;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function readNext()
43
    {
44
        $xml = $this->getXMLReader();
45
        while ($xml->read()) {
46
            if (\XMLReader::END_ELEMENT === $xml->nodeType && 'cellXfs' === $xml->name) {
47
                break;
48
            } elseif (\XMLReader::ELEMENT === $xml->nodeType && 'cellXfs' === $xml->name) {
49
                $this->inXfs = true;
50
            } elseif ($this->inXfs && \XMLReader::ELEMENT === $xml->nodeType && 'xf' === $xml->name) {
51
                $fmtId = $xml->getAttribute('numFmtId');
52
                if (isset($this->numberFormats[$fmtId])) {
53
                    $value = $this->numberFormats[$fmtId];
54
                } elseif (in_array($fmtId, $this->nativeDateFormats)) {
55
                    $value = static::FORMAT_DATE;
56
                } else {
57
                    $value = static::FORMAT_DEFAULT;
58
                }
59
                $this->values[] = $value;
60
61
                return;
62
            }
63
        }
64
        $this->valid = false;
65
        $this->closeXMLReader();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function createXMLReader()
72
    {
73
        $xml = parent::createXMLReader();
74
        $needsRewind = false;
75
        while ($xml->read()) {
76
            if (\XMLReader::END_ELEMENT === $xml->nodeType && 'numFmts' === $xml->name) {
77
                break;
78
            } elseif (\XMLReader::ELEMENT === $xml->nodeType) {
79
                switch ($xml->name) {
80
                    case 'numFmt':
81
                        $this->numberFormats[$xml->getAttribute('numFmtId')] =
82
                            preg_match('{^(\[\$[[:alpha:]]*-[0-9A-F]*\])*[hmsdy]}i', $xml->getAttribute('formatCode'))
83
                                ? static::FORMAT_DATE
84
                                : static::FORMAT_DEFAULT;
85
                        break;
86
                    case 'cellXfs':
87
                        $needsRewind = true;
88
                        break;
89
                }
90
            }
91
        }
92
        if ($needsRewind) {
93
            $xml->close();
94
            $xml = parent::createXMLReader();
95
        }
96
97
        return $xml;
98
    }
99
}
100