Completed
Pull Request — development (#86)
by Philippe
03:27
created

FormzCollector::getWidgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
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
    function collect()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39
        $formzInstances = $this->getFormzInstances();
40
41
        foreach ($formzInstances as $instance) {
42
            $debug .= DebuggerUtility::var_dump($instance, $instance->getName(),10,false,true, true);
0 ignored issues
show
Bug introduced by
The variable $debug 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...
43
            $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...
44
                'name' => $instance->getName(),
45
                'params' => [
46
                    'Class Name' => $instance->getClassName(),
47
                    'Fields' => $instance->getProperties(),
48
                    'Hash' => $instance->getHash()
49
                ]
50
            ];
51
        }
52
53
        $formsCount = \count($formzInstances);
54
        $output = [
55
            'formsCount' => $formsCount, // Number of formz instance in the page
56
            'debug' => $debug,
57
            'status' => '<b>' . $formsCount . '</b> Formz instance(s) in this page',
58
            '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...
59
            'console' => '' // div for output console javascript
60
        ];
61
62
        return $output;
63
    }
64
65
    /**
66
     * @return FormObject[]
67
     */
68
    public function getFormzInstances()
69
    {
70
        $formObjFactory = GeneralUtility::makeInstance(FormObjectFactory::class);
71
72
        foreach ($formObjFactory->getInstances() as $instance) {
73
            $this->formzInstances[$instance->getName()] = $instance;
74
        }
75
76
        return $this->formzInstances;
77
    }
78
79
    /**
80
     * Returns a hash where keys are control names and their values
81
     * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()}
82
     *
83
     * @return array
84
     */
85
    function getWidgets()
86
    {
87
        $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...
88
89
        return [
90
            (string) $name => [
91
                'icon' => $name,
92
                'widget' => 'PhpDebugBar.Widgets.FormzWidget',
93
                'map' => $name,
94
                "default" => '[]',
95
            ],
96
            "$name:badge" => [
97
                'map' => 'formz.formsCount',
98
                'default' => 0,
99
            ],
100
        ];
101
    }
102
103
    /**
104
     * Returns the unique name of the collector
105
     *
106
     * @return string
107
     */
108
    public function getName()
109
    {
110
        return 'formz';
111
    }
112
}
113