Completed
Push — develop ( f4b279...a19c81 )
by René
17:42
created

OnPageService::showColumns()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 23
rs 8.5906
cc 5
eloc 15
nc 6
nop 2
1
<?php
2
/**
3
 *
4
 */
5
6
namespace HDNET\OnpageIntegration\Service;
7
8
9
use HDNET\OnpageIntegration\Loader\ApiResultLoader;
10
11
class OnPageService extends AbstractService
12
{
13
14
    /**
15
     * @var ApiResultLoader
16
     */
17
    protected $loader;
18
19
    /**
20
     * OnPageService constructor.
21
     *
22
     * @param ApiResultLoader $loader
23
     */
24
    public function __construct(ApiResultLoader $loader)
25
    {
26
        $this->loader = $loader;
27
    }
28
29
    /**
30
     * So that you get the error report of an
31
     * api call you have to crawl the graph api call
32
     * from a filter.
33
     *
34
     * In the next the you load the error report keys and
35
     * commit it into the errorReport function.
36
     *
37
     * errorReport generates the error report and afterwards
38
     * its stores in the field 'errors'.
39
     * '     *
40
     *
41
     * @param array           $buildData
42
     * @param string          $section
43
     * @param ApiResultLoader $loader
0 ignored issues
show
Bug introduced by
There is no parameter named $loader. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     */
45
    public function build($buildData, $section)
46
    {
47
        $i = 0;
48
        foreach ($buildData as &$element) {
49
            $graphDataArray = $this->loader->load('zoom_' . $section . '_' . $i . '_graph');
50
            $errorReportKey = $element['errors'];
51
            $element['errors'] = $this->errorReport($graphDataArray, $errorReportKey);
52
            $i++;
53
        }
54
        return $buildData;
55
    }
56
57
    /**
58
     * Generates the error report key of an api call and
59
     * return the result.
60
     *
61
     * @param mixed $graphApiCallResult
62
     * @param mixed $errorReportKey
63
     *
64
     * @return int
65
     */
66
    protected function errorReport($graphApiCallResult, $errorReportKey)
67
    {
68
        $totalErrors = 0;
69
        foreach ($graphApiCallResult as $element) {
70
            if (in_array('sum', $errorReportKey)) {
71
                foreach ($errorReportKey['hidden'] as $hidden) {
72
                    if (in_array($hidden, $element)) {
73
                        continue 2;
74
                    }
75
                }
76
                $totalErrors += $element['count'];
77
            }
78
            if (in_array($errorReportKey['show'], $element)) {
79
                $totalErrors += $element['count'];
80
            }
81
        }
82
        return $totalErrors;
83
    }
84
85
    /**
86
     * Fitted $tableApiCallResult by the elements of
87
     * $showTableKey
88
     *
89
     * @param string $apiCall
90
     * @param array  $showTableKey
91
     *
92
     * @return array
93
     */
94
    public function showColumns($apiCall, array $showTableKey)
95
    {
96
        $apiCallResult = $this->loader->load($apiCall);
97
98
        $fittedTablesRecords = [];
99
        foreach ($apiCallResult as $singleCallElement) {
100
            foreach ($showTableKey as $key) {
101
                if (array_key_exists($key, $singleCallElement)) {
102
                    $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...
103
                }
104
                if($key === 'documents') {
105
                    $documents = [
106
                        'mime' => $singleCallElement['mime'],
107
                        'meta_title' => $singleCallElement['meta_title'],
108
                        'url' => $singleCallElement['url'],
109
                    ];
110
                    $singleRecordArray['document'] = $documents;
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...
111
                }
112
            }
113
            $fittedTablesRecords[] = $this->replaceNULL($singleRecordArray);
114
        }
115
        return $fittedTablesRecords;
116
    }
117
118
    /**
119
     * Build only for development
120
     */
121
    public function replaceNULL($array)
122
    {
123
        foreach ($array as &$element) {
124
            if(empty($element)) {
125
                $element = "Keine";
126
            }
127
        }
128
        return $array;
129
    }
130
}
131