Completed
Push — develop ( 75d3bd...a06533 )
by Adrien
21:48
created

Chart   D

Complexity

Total Complexity 126

Size/Duplication

Total Lines 503
Duplicated Lines 20.28 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 89.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 102
loc 503
ccs 314
cts 349
cp 0.8997
rs 4.0645
c 1
b 0
f 0
wmc 126
lcom 1
cbo 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
D readChart() 41 141 37
A chartLayoutDetails() 0 16 4
C chartDataSeries() 0 51 14
B chartDataSeriesValueSet() 0 28 5
B getAttribute() 0 17 7
A readColor() 0 8 3
D readChartAttributes() 18 29 9
D setChartAttributes() 0 28 9
C chartDataSeriesValues() 20 33 7
C chartDataSeriesValuesMultiLevel() 20 35 8
F parseRichText() 3 66 17
B chartTitle() 0 24 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Chart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Chart, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
7
/**
8
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 *
24
 * @category    PhpSpreadsheet
25
 *
26
 * @copyright   Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
27
 * @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
28
 */
29
class Chart
30
{
31
    /**
32
     * @param \SimpleXMLElement $component
33
     * @param string $name
34
     * @param string $format
35
     */
36 1
    private static function getAttribute(\SimpleXMLElement $component, $name, $format)
37
    {
38 1
        $attributes = $component->attributes();
39 1
        if (isset($attributes[$name])) {
40 1
            if ($format == 'string') {
41 1
                return (string) $attributes[$name];
42 1
            } elseif ($format == 'integer') {
43 1
                return (int) $attributes[$name];
44 1
            } elseif ($format == 'boolean') {
45 1
                return (bool) ($attributes[$name] === '0' || $attributes[$name] !== 'true') ? false : true;
46
            }
47
48
            return (float) $attributes[$name];
49
        }
50
51 1
        return null;
52
    }
53
54
    private static function readColor($color, $background = false)
55
    {
56
        if (isset($color['rgb'])) {
57
            return (string) $color['rgb'];
58
        } elseif (isset($color['indexed'])) {
59
            return \PhpOffice\PhpSpreadsheet\Style\Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
60
        }
61
    }
62
63
    /**
64
     * @param \SimpleXMLElement $chartElements
65
     * @param string $chartName
66
     */
67 1
    public static function readChart(\SimpleXMLElement $chartElements, $chartName)
68
    {
69 1
        $namespacesChartMeta = $chartElements->getNamespaces(true);
70 1
        $chartElementsC = $chartElements->children($namespacesChartMeta['c']);
71
72 1
        $XaxisLabel = $YaxisLabel = $legend = $title = null;
73 1
        $dispBlanksAs = $plotVisOnly = null;
74
75 1
        foreach ($chartElementsC as $chartElementKey => $chartElement) {
76
            switch ($chartElementKey) {
77 1
                case 'chart':
78 1
                    foreach ($chartElement as $chartDetailsKey => $chartDetails) {
79 1
                        $chartDetailsC = $chartDetails->children($namespacesChartMeta['c']);
0 ignored issues
show
Unused Code introduced by
$chartDetailsC is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
                        switch ($chartDetailsKey) {
81 1
                            case 'plotArea':
82 1
                                $plotAreaLayout = $XaxisLable = $YaxisLable = null;
0 ignored issues
show
Unused Code introduced by
$YaxisLable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$XaxisLable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83 1
                                $plotSeries = $plotAttributes = [];
84 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
85
                                    switch ($chartDetailKey) {
86 1
                                        case 'layout':
87 1
                                            $plotAreaLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
88 1
                                            break;
89 1 View Code Duplication
                                        case 'catAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90 1
                                            if (isset($chartDetail->title)) {
91 1
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
92
                                            }
93 1
                                            break;
94 1 View Code Duplication
                                        case 'dateAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                                            if (isset($chartDetail->title)) {
96
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
97
                                            }
98
                                            break;
99 1 View Code Duplication
                                        case 'valAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100 1
                                            if (isset($chartDetail->title)) {
101 1
                                                $YaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
102
                                            }
103 1
                                            break;
104 1
                                        case 'barChart':
105 1
                                        case 'bar3DChart':
106 1
                                            $barDirection = self::getAttribute($chartDetail->barDir, 'val', 'string');
107 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
108 1
                                            $plotSer->setPlotDirection($barDirection);
109 1
                                            $plotSeries[] = $plotSer;
110 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
111 1
                                            break;
112 1
                                        case 'lineChart':
113 1 View Code Duplication
                                        case 'line3DChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
115 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
116 1
                                            break;
117 1
                                        case 'areaChart':
118 1 View Code Duplication
                                        case 'area3DChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
120 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
121 1
                                            break;
122 1
                                        case 'doughnutChart':
123 1
                                        case 'pieChart':
124 1
                                        case 'pie3DChart':
125 1
                                            $explosion = isset($chartDetail->ser->explosion);
126 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
127 1
                                            $plotSer->setPlotStyle($explosion);
0 ignored issues
show
Documentation introduced by
$explosion is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128 1
                                            $plotSeries[] = $plotSer;
129 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
130 1
                                            break;
131 1 View Code Duplication
                                        case 'scatterChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132 1
                                            $scatterStyle = self::getAttribute($chartDetail->scatterStyle, 'val', 'string');
133 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
134 1
                                            $plotSer->setPlotStyle($scatterStyle);
135 1
                                            $plotSeries[] = $plotSer;
136 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
137 1
                                            break;
138 1
                                        case 'bubbleChart':
139 1
                                            $bubbleScale = self::getAttribute($chartDetail->bubbleScale, 'val', 'integer');
140 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
141 1
                                            $plotSer->setPlotStyle($bubbleScale);
142 1
                                            $plotSeries[] = $plotSer;
143 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
144 1
                                            break;
145 1 View Code Duplication
                                        case 'radarChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146 1
                                            $radarStyle = self::getAttribute($chartDetail->radarStyle, 'val', 'string');
147 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
148 1
                                            $plotSer->setPlotStyle($radarStyle);
149 1
                                            $plotSeries[] = $plotSer;
150 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
151 1
                                            break;
152 1
                                        case 'surfaceChart':
153 1
                                        case 'surface3DChart':
154 1
                                            $wireFrame = self::getAttribute($chartDetail->wireframe, 'val', 'boolean');
155 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
156 1
                                            $plotSer->setPlotStyle($wireFrame);
157 1
                                            $plotSeries[] = $plotSer;
158 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
159 1
                                            break;
160 1 View Code Duplication
                                        case 'stockChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
162 1
                                            $plotAttributes = self::readChartAttributes($plotAreaLayout);
163 1
                                            break;
164
                                    }
165
                                }
166 1
                                if ($plotAreaLayout == null) {
167 1
                                    $plotAreaLayout = new \PhpOffice\PhpSpreadsheet\Chart\Layout();
168
                                }
169 1
                                $plotArea = new \PhpOffice\PhpSpreadsheet\Chart\PlotArea($plotAreaLayout, $plotSeries);
170 1
                                self::setChartAttributes($plotAreaLayout, $plotAttributes);
171 1
                                break;
172 1
                            case 'plotVisOnly':
173 1
                                $plotVisOnly = self::getAttribute($chartDetails, 'val', 'string');
174 1
                                break;
175 1
                            case 'dispBlanksAs':
176 1
                                $dispBlanksAs = self::getAttribute($chartDetails, 'val', 'string');
177 1
                                break;
178 1
                            case 'title':
179 1
                                $title = self::chartTitle($chartDetails, $namespacesChartMeta);
180 1
                                break;
181 1
                            case 'legend':
182 1
                                $legendPos = 'r';
183 1
                                $legendLayout = null;
184 1
                                $legendOverlay = false;
185 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
186
                                    switch ($chartDetailKey) {
187 1
                                        case 'legendPos':
188 1
                                            $legendPos = self::getAttribute($chartDetail, 'val', 'string');
189 1
                                            break;
190 1
                                        case 'overlay':
191 1
                                            $legendOverlay = self::getAttribute($chartDetail, 'val', 'boolean');
192 1
                                            break;
193 1
                                        case 'layout':
194 1
                                            $legendLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
195 1
                                            break;
196
                                    }
197
                                }
198 1
                                $legend = new \PhpOffice\PhpSpreadsheet\Chart\Legend($legendPos, $legendLayout, $legendOverlay);
199 1
                                break;
200
                        }
201
                    }
202
            }
203
        }
204 1
        $chart = new \PhpOffice\PhpSpreadsheet\Chart($chartName, $title, $legend, $plotArea, $plotVisOnly, $dispBlanksAs, $XaxisLabel, $YaxisLabel);
0 ignored issues
show
Bug introduced by
The variable $plotArea does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
205
206 1
        return $chart;
207
    }
208
209 1
    private static function chartTitle(\SimpleXMLElement $titleDetails, array $namespacesChartMeta)
210
    {
211 1
        $caption = [];
212 1
        $titleLayout = null;
213 1
        foreach ($titleDetails as $titleDetailKey => $chartDetail) {
214
            switch ($titleDetailKey) {
215 1
                case 'tx':
216 1
                    $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
217 1
                    foreach ($titleDetails as $titleKey => $titleDetail) {
218
                        switch ($titleKey) {
219 1
                            case 'p':
220 1
                                $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
221 1
                                $caption[] = self::parseRichText($titleDetailPart);
222
                        }
223
                    }
224 1
                    break;
225 1
                case 'layout':
226 1
                    $titleLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
227 1
                    break;
228
            }
229
        }
230
231 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Title($caption, $titleLayout);
232
    }
233
234 1
    private static function chartLayoutDetails($chartDetail, $namespacesChartMeta)
235
    {
236 1
        if (!isset($chartDetail->manualLayout)) {
237 1
            return null;
238
        }
239 1
        $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
240 1
        if (is_null($details)) {
241
            return null;
242
        }
243 1
        $layout = [];
244 1
        foreach ($details as $detailKey => $detail) {
245 1
            $layout[$detailKey] = self::getAttribute($detail, 'val', 'string');
246
        }
247
248 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Layout($layout);
249
    }
250
251 1
    private static function chartDataSeries($chartDetail, $namespacesChartMeta, $plotType)
252
    {
253 1
        $multiSeriesType = null;
254 1
        $smoothLine = false;
255 1
        $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = [];
256
257 1
        $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
258 1
        foreach ($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
259
            switch ($seriesDetailKey) {
260 1
                case 'grouping':
261 1
                    $multiSeriesType = self::getAttribute($chartDetail->grouping, 'val', 'string');
262 1
                    break;
263 1
                case 'ser':
264 1
                    $marker = null;
265 1
                    foreach ($seriesDetails as $seriesKey => $seriesDetail) {
266
                        switch ($seriesKey) {
267 1
                            case 'idx':
268 1
                                $seriesIndex = self::getAttribute($seriesDetail, 'val', 'integer');
269 1
                                break;
270 1
                            case 'order':
271 1
                                $seriesOrder = self::getAttribute($seriesDetail, 'val', 'integer');
272 1
                                $plotOrder[$seriesIndex] = $seriesOrder;
0 ignored issues
show
Bug introduced by
The variable $seriesIndex does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
273 1
                                break;
274 1
                            case 'tx':
275 1
                                $seriesLabel[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
276 1
                                break;
277 1
                            case 'marker':
278 1
                                $marker = self::getAttribute($seriesDetail->symbol, 'val', 'string');
279 1
                                break;
280 1
                            case 'smooth':
281 1
                                $smoothLine = self::getAttribute($seriesDetail, 'val', 'boolean');
282 1
                                break;
283 1
                            case 'cat':
284 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
285 1
                                break;
286 1
                            case 'val':
287 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
288 1
                                break;
289 1
                            case 'xVal':
290 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
291 1
                                break;
292 1
                            case 'yVal':
293 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
294 1
                                break;
295
                        }
296
                    }
297
            }
298
        }
299
300 1
        return new \PhpOffice\PhpSpreadsheet\Chart\DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $smoothLine);
301
    }
302
303 1
    private static function chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null, $smoothLine = false)
304
    {
305 1
        if (isset($seriesDetail->strRef)) {
306 1
            $seriesSource = (string) $seriesDetail->strRef->f;
307 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
308
309 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
310
        } elseif (isset($seriesDetail->numRef)) {
311 1
            $seriesSource = (string) $seriesDetail->numRef->f;
312 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
313
314 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
315
        } elseif (isset($seriesDetail->multiLvlStrRef)) {
316 1
            $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
317 1
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
318 1
            $seriesData['pointCount'] = count($seriesData['dataValues']);
319
320 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
321
        } elseif (isset($seriesDetail->multiLvlNumRef)) {
322
            $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
323
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
324
            $seriesData['pointCount'] = count($seriesData['dataValues']);
325
326
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
327
        }
328
329
        return null;
330
    }
331
332 1
    private static function chartDataSeriesValues($seriesValueSet, $dataType = 'n')
333
    {
334 1
        $seriesVal = [];
335 1
        $formatCode = '';
336 1
        $pointCount = 0;
337
338 1 View Code Duplication
        foreach ($seriesValueSet as $seriesValueIdx => $seriesValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
            switch ($seriesValueIdx) {
340 1
                case 'ptCount':
341 1
                    $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
342 1
                    break;
343 1
                case 'formatCode':
344 1
                    $formatCode = (string) $seriesValue;
345 1
                    break;
346 1
                case 'pt':
347 1
                    $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
348 1
                    if ($dataType == 's') {
349 1
                        $seriesVal[$pointVal] = (string) $seriesValue->v;
350 1
                    } elseif ($seriesValue->v === Functions::NA()) {
351
                        $seriesVal[$pointVal] = null;
352
                    } else {
353 1
                        $seriesVal[$pointVal] = (float) $seriesValue->v;
354
                    }
355 1
                    break;
356
            }
357
        }
358
359
        return [
360 1
            'formatCode' => $formatCode,
361 1
            'pointCount' => $pointCount,
362 1
            'dataValues' => $seriesVal,
363
        ];
364
    }
365
366 1
    private static function chartDataSeriesValuesMultiLevel($seriesValueSet, $dataType = 'n')
367
    {
368 1
        $seriesVal = [];
369 1
        $formatCode = '';
370 1
        $pointCount = 0;
371
372 1
        foreach ($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
373 1 View Code Duplication
            foreach ($seriesLevel as $seriesValueIdx => $seriesValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
374
                switch ($seriesValueIdx) {
375 1
                    case 'ptCount':
376
                        $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
377
                        break;
378 1
                    case 'formatCode':
379
                        $formatCode = (string) $seriesValue;
380
                        break;
381 1
                    case 'pt':
382 1
                        $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
383 1
                        if ($dataType == 's') {
384 1
                            $seriesVal[$pointVal][] = (string) $seriesValue->v;
385
                        } elseif ($seriesValue->v === Functions::NA()) {
386
                            $seriesVal[$pointVal] = null;
387
                        } else {
388
                            $seriesVal[$pointVal][] = (float) $seriesValue->v;
389
                        }
390 1
                        break;
391
                }
392
            }
393
        }
394
395
        return [
396 1
            'formatCode' => $formatCode,
397 1
            'pointCount' => $pointCount,
398 1
            'dataValues' => $seriesVal,
399
        ];
400
    }
401
402 1
    private static function parseRichText(\SimpleXMLElement $titleDetailPart)
403
    {
404 1
        $value = new \PhpOffice\PhpSpreadsheet\RichText();
405
406 1
        foreach ($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
407 1
            if (isset($titleDetailElement->t)) {
408 1
                $objText = $value->createTextRun((string) $titleDetailElement->t);
409
            }
410 1
            if (isset($titleDetailElement->rPr)) {
411 1 View Code Duplication
                if (isset($titleDetailElement->rPr->rFont['val'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
412
                    $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont['val']);
0 ignored issues
show
Bug introduced by
The variable $objText does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
413
                }
414
415 1
                $fontSize = (self::getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
416 1
                if (!is_null($fontSize)) {
417 1
                    $objText->getFont()->setSize(floor($fontSize / 100));
418
                }
419
420 1
                $fontColor = (self::getAttribute($titleDetailElement->rPr, 'color', 'string'));
421 1
                if (!is_null($fontColor)) {
422
                    $objText->getFont()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color(self::readColor($fontColor)));
423
                }
424
425 1
                $bold = self::getAttribute($titleDetailElement->rPr, 'b', 'boolean');
426 1
                if (!is_null($bold)) {
427 1
                    $objText->getFont()->setBold($bold);
428
                }
429
430 1
                $italic = self::getAttribute($titleDetailElement->rPr, 'i', 'boolean');
431 1
                if (!is_null($italic)) {
432 1
                    $objText->getFont()->setItalic($italic);
433
                }
434
435 1
                $baseline = self::getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
436 1
                if (!is_null($baseline)) {
437 1
                    if ($baseline > 0) {
438 1
                        $objText->getFont()->setSuperScript(true);
439 1
                    } elseif ($baseline < 0) {
440 1
                        $objText->getFont()->setSubScript(true);
441
                    }
442
                }
443
444 1
                $underscore = (self::getAttribute($titleDetailElement->rPr, 'u', 'string'));
445 1
                if (!is_null($underscore)) {
446 1
                    if ($underscore == 'sng') {
447 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
448 1
                    } elseif ($underscore == 'dbl') {
449 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE);
450
                    } else {
451 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE);
452
                    }
453
                }
454
455 1
                $strikethrough = (self::getAttribute($titleDetailElement->rPr, 's', 'string'));
456 1
                if (!is_null($strikethrough)) {
457
                    if ($strikethrough == 'noStrike') {
458
                        $objText->getFont()->setStrikethrough(false);
459
                    } else {
460
                        $objText->getFont()->setStrikethrough(true);
461
                    }
462
                }
463
            }
464
        }
465
466 1
        return $value;
467
    }
468
469 1
    private static function readChartAttributes($chartDetail)
470
    {
471 1
        $plotAttributes = [];
472 1
        if (isset($chartDetail->dLbls)) {
473 1
            if (isset($chartDetail->dLbls->howLegendKey)) {
474
                $plotAttributes['showLegendKey'] = self::getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
475
            }
476 1 View Code Duplication
            if (isset($chartDetail->dLbls->showVal)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
477 1
                $plotAttributes['showVal'] = self::getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
478
            }
479 1 View Code Duplication
            if (isset($chartDetail->dLbls->showCatName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
480 1
                $plotAttributes['showCatName'] = self::getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
481
            }
482 1 View Code Duplication
            if (isset($chartDetail->dLbls->showSerName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
483 1
                $plotAttributes['showSerName'] = self::getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
484
            }
485 1 View Code Duplication
            if (isset($chartDetail->dLbls->showPercent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
486 1
                $plotAttributes['showPercent'] = self::getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
487
            }
488 1 View Code Duplication
            if (isset($chartDetail->dLbls->showBubbleSize)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
489 1
                $plotAttributes['showBubbleSize'] = self::getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
490
            }
491 1 View Code Duplication
            if (isset($chartDetail->dLbls->showLeaderLines)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
492 1
                $plotAttributes['showLeaderLines'] = self::getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
493
            }
494
        }
495
496 1
        return $plotAttributes;
497
    }
498
499
    /**
500
     * @param \PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea
501
     * @param mixed $plotAttributes
502
     */
503 1
    private static function setChartAttributes(\PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea, $plotAttributes)
504
    {
505 1
        foreach ($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
506
            switch ($plotAttributeKey) {
507 1
                case 'showLegendKey':
508
                    $plotArea->setShowLegendKey($plotAttributeValue);
509
                    break;
510 1
                case 'showVal':
511 1
                    $plotArea->setShowVal($plotAttributeValue);
512 1
                    break;
513 1
                case 'showCatName':
514 1
                    $plotArea->setShowCatName($plotAttributeValue);
515 1
                    break;
516 1
                case 'showSerName':
517 1
                    $plotArea->setShowSerName($plotAttributeValue);
518 1
                    break;
519 1
                case 'showPercent':
520 1
                    $plotArea->setShowPercent($plotAttributeValue);
521 1
                    break;
522 1
                case 'showBubbleSize':
523 1
                    $plotArea->setShowBubbleSize($plotAttributeValue);
524 1
                    break;
525 1
                case 'showLeaderLines':
526 1
                    $plotArea->setShowLeaderLines($plotAttributeValue);
527 1
                    break;
528
            }
529
        }
530 1
    }
531
}
532