Completed
Pull Request — master (#21)
by Daniel
02:31
created

GridBench   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 55 2
A grid_loading() 0 4 1
A grid_loading_with_form() 0 6 1
A provideNbObjects() 0 14 1
1
<?php
2
3
namespace Psi\Component\Grid\Benchmarks;
4
5
use Psi\Component\Grid\Tests\Functional\GridTestCase;
6
use Psi\Component\Grid\Tests\Model\Article;
7
8
/**
9
 * @BeforeMethods({"init"})
10
 * @Revs(100)
11
 * @Iterations(10)
12
 * @OutputTimeUnit("milliseconds")
13
 * @ParamProviders({"provideNbObjects"})
14
 */
15
class GridBench extends GridTestCase
16
{
17
    private $factory;
18
19
    public function init($params)
20
    {
21
        $nbObjects = $params['nb_objects'];
22
23
        for ($i = 0; $i < $nbObjects; $i++) {
24
            $objects[] = new Article('hello', $i);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$objects was never initialized. Although not strictly required by PHP, it is generally a good practice to add $objects = 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...
25
        }
26
27
        $container = $this->createContainer([
28
            'mapping' => [
29
                Article::class => [
30
                    'grids' => [
31
                        'main' => [
32
                            'columns' => [
33
                                'title' => [
34
                                    'type' => 'property',
35
                                ],
36
                                'number' => [
37
                                    'type' => 'property',
38
                                ],
39
                            ],
40
                            'filters' => [
41
                                'title' => [
42
                                    'type' => 'string',
43
                                ],
44
                            ],
45
                        ],
46
                        'form' => [
47
                            'columns' => [
48
                                'select' => [
49
                                    'type' => 'select',
50
                                ],
51
                                'title' => [
52
                                    'type' => 'property',
53
                                ],
54
                                'number' => [
55
                                    'type' => 'property',
56
                                ],
57
                            ],
58
                            'filters' => [
59
                                'title' => [
60
                                    'type' => 'string',
61
                                ],
62
                            ],
63
                        ],
64
                    ],
65
                ],
66
            ],
67
            'collections' => [
68
                Article::class => $objects,
0 ignored issues
show
Bug introduced by
The variable $objects 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...
69
            ],
70
        ]);
71
72
        $this->factory = $container->get('grid.factory');
73
    }
74
75
    /**
76
     * @Subject()
77
     */
78
    public function grid_loading()
79
    {
80
        $this->factory->loadGrid(Article::class, []);
81
    }
82
83
    /**
84
     * @Subject()
85
     */
86
    public function grid_loading_with_form()
87
    {
88
        $this->factory->loadGrid(Article::class, [
89
            'variant' => 'form',
90
        ]);
91
    }
92
93
    public function provideNbObjects()
94
    {
95
        return [
96
            [
97
                'nb_objects' => 10,
98
            ],
99
            [
100
                'nb_objects' => 100,
101
            ],
102
            [
103
                'nb_objects' => 1000,
104
            ],
105
        ];
106
    }
107
}
108