Completed
Push — master ( 2cd481...da65e9 )
by Daniel
02:29
created

OutputFormBuilder::providers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4286
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\info_compare;
28
29
trait OutputFormBuilder
30
{
31
32
    private function listOfKnownLabels()
33
    {
34
        $urlToGetLbl           = $this->config['Servers'][$this->config['Defaults']['Source']]['url']
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
                . '?Label=---' . urlencode(' List of known labels');
36
        $knownLabels           = $this->getContentFromUrlThroughCurlAsArrayIfJson($urlToGetLbl)['response'];
0 ignored issues
show
Bug introduced by
It seems like getContentFromUrlThroughCurlAsArrayIfJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
        $informatorKnownLabels = array_diff($knownLabels, ['--- List of known labels']);
38
        $tmpOptions            = [];
39
        foreach ($informatorKnownLabels as $value) {
40
            $tmpOptions[] = '<input type="radio" name="Label" id="Label_' . $value . '" value="' . $value . '" '
41
                    . $this->turnRequestedValueIntoCheckboxStatus('Label', $value)
42
                    . '/>'
43
                    . '<label for="Label_' . $value . '">' . $value . '</label>';
44
        }
45
        return '<fieldset style="float:left;">'
46
                . '<legend>Informator Label to use</legend>'
47
                . implode('<br/>', $tmpOptions)
48
                . '</fieldset>';
49
    }
50
51
    private function providers($inArray)
52
    {
53
        $tmpOptions = [];
54
        foreach ($this->config['Servers'] as $key => $value) {
55
            $tmpOptions[] = '<a href="' . $value['url'] . '" target="_blank">run-me</a>&nbsp;'
56
                    . '<input type="radio" name="' . $inArray['ConfigName'] . '" id="'
57
                    . $inArray['ConfigName'] . '_' . $key . '" value="' . $key . '" '
58
                    . $this->turnRequestedValueIntoCheckboxStatus($inArray['ConfigName'], $key)
59
                    . '/>'
60
                    . '<label for="' . $inArray['ConfigName'] . '_' . $key . '">' . $value['name'] . '</label>';
61
        }
62
        return '<fieldset style="float:left;">'
63
                . '<legend>' . $inArray['TitleStart'] . ' config providers</legend>'
64
                . implode('<br/>', $tmpOptions)
65
                . '</fieldset>';
66
    }
67
68
    protected function setFormOptions()
0 ignored issues
show
Coding Style introduced by
setFormOptions uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
69
    {
70
        $sReturn   = [];
71
        $sReturn[] = $this->typeOfResults();
72
        $sReturn[] = $this->providers([
73
            'TitleStart' => 'Source',
74
            'ConfigName' => 'localConfig',
75
        ]);
76
        $sReturn[] = $this->providers([
77
            'TitleStart' => 'Target',
78
            'ConfigName' => 'serverConfig',
79
        ]);
80
        $sReturn[] = $this->listOfKnownLabels();
81
        return '<div class="tabbertab" id="tabOptions" title="Options">'
82
                . '<style type="text/css" media="all" scoped>label { width: auto; }</style>'
83
                . '<form method="get" action="'
84
                . $_SERVER['PHP_SELF'] . '">'
85
                . '<input type="submit" value="Apply" />'
86
                . '<br/>' . implode('', $sReturn)
87
                . '</form>'
88
                . '<div style="float:none;clear:both;height:1px;">&nbsp;</div>'
89
                . '</div><!--from tabOptions-->';
90
    }
91
92
    private function turnRequestedValueIntoCheckboxStatus($requestedName, $checkedValue)
93
    {
94
        $requestedNameValue = $this->informatorInternalArray['superGlobals']->get($requestedName);
0 ignored issues
show
Bug introduced by
The property informatorInternalArray does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
95
        $checkboxStatus     = '';
96
        if ($requestedNameValue === $checkedValue) {
97
            $checkboxStatus = 'checked ';
98
        }
99
        return $checkboxStatus;
100
    }
101
102
    private function typeOfResults()
103
    {
104
        return '<fieldset style="float:left;">'
105
                . '<legend>Type of results displayed</legend>'
106
                . '<input type="radio" name="displayOnlyDifferent" id="displayOnlyDifferent" value="1" '
107
                . $this->turnRequestedValueIntoCheckboxStatus('displayOnlyDifferent', 1)
108
                . '/>'
109
                . '<label for="displayOnlyDifferent">Only the Different values</label>'
110
                . '<br/>'
111
                . '<input type="radio" name="displayOnlyDifferent" id="displayAll" value="0" '
112
                . $this->turnRequestedValueIntoCheckboxStatus('displayOnlyDifferent', 0)
113
                . '/>'
114
                . '<label for="displayAll">All</label>'
115
                . '</fieldset>';
116
    }
117
}
118