Completed
Pull Request — development (#86)
by Philippe
01:45
created

FormzCollector::collect()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
/*
3
 * This program is free software: you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation, either version 3 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
17
namespace Romm\Formz\DataCollectors;
18
19
use Konafets\TYPO3DebugBar\DataCollectors\BaseCollector;
20
use Romm\Formz\Form\FormObject;
21
use Romm\Formz\Form\FormObjectFactory;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
24
25
class FormzCollector extends BaseCollector
26
{
27
    /**
28
     * @var FormObject[]
29
     */
30
    private $formzInstances = [];
31
32
    /**
33
     * Called by the DebugBar when data needs to be collected
34
     *
35
     * @return array Collected data
36
     */
37
    public function collect()
38
    {
39
        $formzInstances = $this->getFormzInstances();
40
41
        $debug = '';
42
43
        foreach ($formzInstances as $instance) {
44
            $debug .= DebuggerUtility::var_dump($instance, $instance->getName(), 10, false, true, true);
45
            $forms[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$forms was never initialized. Although not strictly required by PHP, it is generally a good practice to add $forms = 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...
46
                'name' => $instance->getName(),
47
                'params' => [
48
                    'Class Name' => $instance->getClassName(),
49
                    'Fields' => $instance->getProperties(),
50
                    'Hash' => $instance->getHash()
51
                ]
52
            ];
53
        }
54
55
        $formsCount = \count($formzInstances);
56
        $output = [
57
            'formsCount' => $formsCount, // Number of formz instance in the page
58
            'debug' => $debug,
59
            'status' => '<b>' . $formsCount . '</b> Formz instance(s) in this page',
60
            'forms' => $forms,
0 ignored issues
show
Bug introduced by
The variable $forms 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...
61
            'console' => '' // div for output console javascript
62
        ];
63
64
        return $output;
65
    }
66
67
    /**
68
     * @return FormObject[]
69
     */
70
    public function getFormzInstances()
71
    {
72
        $formObjFactory = GeneralUtility::makeInstance(FormObjectFactory::class);
73
74
        foreach ($formObjFactory->getInstances() as $instance) {
75
            $this->formzInstances[$instance->getName()] = $instance;
76
        }
77
78
        return $this->formzInstances;
79
    }
80
81
    /**
82
     * Returns a hash where keys are control names and their values
83
     * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()}
84
     *
85
     * @return array
86
     */
87
    public function getWidgets()
88
    {
89
        $name = $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
90
91
        return [
92
            (string) $name => [
93
                'icon' => $name,
94
                'widget' => 'PhpDebugBar.Widgets.FormzWidget',
95
                'map' => $name,
96
                'default' => '[]',
97
            ],
98
            "$name:badge" => [
99
                'map' => 'formz.formsCount',
100
                'default' => 0,
101
            ],
102
        ];
103
    }
104
105
    /**
106
     * Returns the unique name of the collector
107
     *
108
     * @return string
109
     */
110
    public function getName()
111
    {
112
        return 'formz';
113
    }
114
}
115