Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

LineFactory::create()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

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