Completed
Push — develop ( 67a76c...0bffec )
by René
07:36
created

ArrayUtility::showTable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
/**
3
 * Class ArrayUtility
4
 */
5
namespace HDNET\OnpageIntegration\Utility;
6
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
9
10
/**
11
 * Class ArrayUtility
12
 */
13
class ArrayUtility
14
{
15
16
    /**
17
     * Append the errors of an api call to
18
     * metaDataArray
19
     *
20
     * @param $metaDataArray
21
     * @param $section
22
     */
23
    public static function buildIndexActionArray(&$metaDataArray, $section)
24
    {
25
        $loader = GeneralUtility::makeInstance(\HDNET\OnpageIntegration\Loader\ApiResultLoader::class);
26
27
        for ($i = 0; $i < count($metaDataArray[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
28
29
            $graphDataArray = $loader->load('zoom_' . $section . '_' . $i . '_graph');
30
            $errorReportyKey = $metaDataArray[0][$i]['errors'];
31
32
            $metaDataArray[0][$i]['errors'] = self::errorReport($graphDataArray, $errorReportyKey);
33
            //todo remove
34
            $metaDataArray[0][$i]['test'] = $graphDataArray;
35
        }
36
    }
37
38
    /**
39
     * Determine the error report of an aspect
40
     *
41
     * @param $graphApiCallResult
42
     * @param $errorReportKey
43
     *
44
     * @return int
45
     */
46
    protected static function errorReport($graphApiCallResult,$errorReportKey) {
47
        $totalErrors = 0;
48
49
        foreach($graphApiCallResult as $element) {
50
51
            if(in_array('sum', $errorReportKey)) {
52
                if(in_array($errorReportKey['hidden'], $element)) {
53
                    continue;
54
                }
55
                $totalErrors += $element['count'];
56
            }
57
58
            if(in_array($errorReportKey['show'], $element)) {
59
                $totalErrors += $element['count'];
60
            }
61
        }
62
        return $totalErrors;
63
    }
64
65
    /**
66
     * Fitted $tableApiCallResult by the elements of
67
     * $showTableKey
68
     *
69
     * @param array $tableApiCallResult
70
     * @param array $showTableKey
71
     *
72
     * @return array
73
     */
74
    public static function showTable(array $tableApiCallResult,array $showTableKey) {
75
        $fittedTablesRecords = [];
76
        foreach($tableApiCallResult as $singleCallElement) {
77
78
            foreach($showTableKey as $key) {
79
                if(array_key_exists($key, $singleCallElement)) {
80
                    $singleRecordArray[$key] = $singleCallElement[$key];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$singleRecordArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $singleRecordArray = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
81
                }
82
            }
83
            $fittedTablesRecords[] = $singleRecordArray;
0 ignored issues
show
Bug introduced by
The variable $singleRecordArray 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...
84
        }
85
        return $fittedTablesRecords;
86
    }
87
}
88