Completed
Pull Request — master (#51)
by De Cramer
02:42
created

LineFactory::create()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 7
nop 3
crap 30
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
use FML\Controls\Control;
5
use FML\Controls\Frame;
6
use FML\Controls\Label;
7
use FML\Types\Renderable;
8
use oliverde8\AssociativeArraySimplified\AssociativeArray;
9
10
/**
11
 * Class LineBuilder
12
 *
13
 * @package eXpansion\Framework\Core\Model\Gui\Builders;
14
 * @author  oliver de Cramer <[email protected]>
15
 */
16
class LineFactory
17
{
18
    /** @var BackGroundFactory */
19
    protected $backGroundFactory;
20
21
    /** @var LabelFactory */
22
    protected $labelFactory;
23
24
    /** @var string */
25
    protected $type;
26
27
    /**
28
     * TitleLineFactory constructor.
29
     *
30
     * @param BackGroundFactory $backGroundFactory
31
     * @param LabelFactory      $labelFactory
32
     * @param string            $type
33
     */
34
    public function __construct(
35
        BackGroundFactory $backGroundFactory,
36
        LabelFactory $labelFactory,
37
        $type = LabelFactory::TYPE_NORMAL
38
    )
39
    {
40
        $this->backGroundFactory = $backGroundFactory;
41
        $this->labelFactory = $labelFactory;
42
        $this->type = $type;
43
    }
44
45
    /**
46
     * TODO Handle more options such as pregenerated buttons and such instead of text.
47
     *
48
     * @param $totalWidth
49
     * @param $columns
50
     *
51
     * @return Frame
52
     */
53
    public function create($totalWidth, $columns, $index = 0)
54
    {
55
        $totalCoef
56
            = ($totalWidth - 1) / array_reduce($columns, function($carry, $item){return $carry + $item['width'];});
57
58
        $frame = new Frame();
59
        $frame->setHeight(5);
60
        $frame->setWidth($totalWidth);
61
62
        $postX = 1;
63
        foreach ($columns as $columnData)
64
        {
65
            if (isset($columnData['text'])) {
66
                $element = $this->createTextColumn($totalCoef, $columnData, $postX);
67
            } elseif (isset($columnData['renderer'])) {
68
                $element = $this->createRendererColumn($columnData, $postX);
69
            }
70
71
            if (isset($columnData['action'])) {
72
                $element->setAction($columnData['action']);
0 ignored issues
show
Bug introduced by
The variable $element 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...
73
            }
74
75
            $frame->addChild($element);
76
            $postX += $columnData["width"] * $totalCoef;
77
        }
78
79
        $frame->addChild($this->backGroundFactory->create($totalWidth, 5, $index));
80
        return $frame;
81
    }
82
83
    /**
84
     * @param float $totalCoef
85
     * @param array $columnData
86
     * @param float $postX
87
     *
88
     * @return Label
89
     */
90
    protected function createTextColumn($totalCoef, $columnData, $postX)
91
    {
92
        $label = $this->labelFactory->create(
93
            $columnData['text'],
94
            AssociativeArray::getFromKey($columnData, 'translatable', false),
95
            $this->type
96
        );
97
        $label->setWidth(($columnData["width"] * $totalCoef) - 0.5);
98
        $label->setPosition($postX, -0.5);
99
100
        return $label;
101
    }
102
103
    /**
104
     * @param $columnData
105
     * @param $postX
106
     *
107
     * @return Control
108
     */
109
    protected function createRendererColumn($columnData, $postX)
110
    {
111
        /** @var Control $element */
112
        $element = $columnData['renderer'];
113
        $element->setPosition($postX, -0.5);
114
        $element->setHeight(4);
115
116
        return $element;
117
    }
118
}