Factory::createGrid()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Grid;
6
7
use Foo\Grid\Cell\Cell;
8
use Foo\Grid\Collection\Actions;
9
use Foo\Grid\Collection\Cells;
10
use Foo\Grid\Collection\Rows;
11
use Foo\Grid\Row\Row;
12
use Foo\Grid\Table\Table;
13
use Foo\Translate\ITranslator;
14
use Opulence\Orm\IEntity;
15
16
class Factory
17
{
18
    const CELL_ACTIONS_CONTENT = 'grid:actions';
19
    const CELL_ACTIONS_GROUP   = 'actions';
20
21
    /**
22
     * @param array            $entities
23
     * @param array            $getters
24
     * @param array            $headers
25
     * @param array            $headerAttributes
26
     * @param array            $bodyAttributes
27
     * @param array            $tableAttributes
28
     * @param array            $gridAttributes
29
     * @param Actions|null     $cellActions
30
     * @param Actions|null     $gridActions
31
     * @param ITranslator|null $translator
32
     *
33
     * @return Grid
34
     */
35
    public static function createGrid(
36
        array $entities,
37
        array $getters,
38
        array $headers,
39
        array $headerAttributes = [],
40
        array $bodyAttributes = [],
41
        array $tableAttributes = [],
42
        array $gridAttributes = [],
43
        Actions $cellActions = null,
44
        Actions $gridActions = null,
45
        ITranslator $translator = null
46
    ) {
47
        $tableBody   = static::createTableBody($entities, $getters, $bodyAttributes, $cellActions);
0 ignored issues
show
Bug introduced by
Since createTableBody() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTableBody() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
48
        $tableHeader = static::createTableHeader($headers, $headerAttributes, $cellActions);
0 ignored issues
show
Bug introduced by
Since createTableHeader() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTableHeader() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
49
50
        $table = new Table($tableBody, $tableHeader, $tableAttributes);
0 ignored issues
show
Bug introduced by
It seems like $tableBody defined by static::createTableBody(...tributes, $cellActions) on line 47 can also be of type array; however, Foo\Grid\Table\Table::__construct() does only seem to accept object<Foo\Grid\Collection\Rows>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $tableHeader defined by static::createTableHeade...tributes, $cellActions) on line 48 can also be of type array; however, Foo\Grid\Table\Table::__construct() does only seem to accept object<Foo\Grid\Collection\Cells>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
52
        $grid = new Grid($table, null, $gridActions, $gridAttributes);
53
54
        $grid->setIndentation(8);
55
56
        if ($translator) {
57
            $grid->setTranslator($translator);
58
        }
59
60
        return $grid;
61
    }
62
63
    /**
64
     * @param array        $entities
65
     * @param array        $getters
66
     * @param array        $bodyAttributes
67
     * @param Actions|null $actions
68
     *
69
     * @return array|Rows
70
     */
71
    private static function createTableBody(
72
        array $entities,
73
        array $getters,
74
        array $bodyAttributes = [],
75
        Actions $actions = null
76
    ) {
77
        $tableBody = new Rows();
78
79
        foreach ($entities as $entity) {
80
            $cells = static::createTableRowCell($getters, $bodyAttributes, $entity);
0 ignored issues
show
Bug introduced by
Since createTableRowCell() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTableRowCell() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
81
82
            $rowActions = $actions ? $actions->duplicate() : null;
83
84
            $row = new Row($cells, $rowActions);
0 ignored issues
show
Bug introduced by
It seems like $cells defined by static::createTableRowCe...odyAttributes, $entity) on line 80 can also be of type array; however, Foo\Grid\Row\Row::__construct() does only seem to accept object<Foo\Grid\Collection\Cells>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
            $row->setEntity($entity);
86
87
            $tableBody[] = $row;
88
        }
89
90
        return $tableBody;
91
    }
92
93
    /**
94
     * @param array   $getters
95
     * @param array   $bodyAttributes
96
     * @param IEntity $entity
97
     *
98
     * @return array|Cells
99
     */
100
    private static function createTableRowCell(
101
        array $getters,
102
        array $bodyAttributes,
103
        IEntity $entity
104
    ) {
105
        $cells = new Cells();
106
107
        foreach ($getters as $group => $getter) {
108
            $content = is_callable($getter) ? $getter($entity) : (string)$entity->$getter();
109
110
            $cells[] = new Cell($content, $group, $bodyAttributes, Cell::BODY);
111
        }
112
113
        return $cells;
114
    }
115
116
    /**
117
     * @param array        $headers
118
     * @param array        $headerAttributes
119
     * @param Actions|null $actions
120
     *
121
     * @return array|Cells
122
     */
123
    private static function createTableHeader(array $headers, array $headerAttributes = [], Actions $actions = null)
124
    {
125
        $cells = new Cells(Cells::HEAD);
126
        foreach ($headers as $group => $content) {
127
            $cells[] = new Cell($content, $group, $headerAttributes, Cell::HEAD);
128
        }
129
130
        if ($actions) {
131
            $cells[] = new Cell(static::CELL_ACTIONS_CONTENT, static::CELL_ACTIONS_GROUP, [], Cell::HEAD);
132
        }
133
134
        return $cells;
135
    }
136
}