Completed
Push — develop ( afe0ec...7c3dfe )
by
unknown
21:46 queued 13:34
created

GeoText::setValue()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 5.3846
cc 8
eloc 17
nc 12
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** GetText.php */
11
namespace Geo\Form;
12
13
use Geo\Form\GeoText\Converter;
14
use Jobs\Entity\Location;
15
use Zend\Form\Element\Hidden;
16
use Zend\Form\Element\Text;
17
use Core\Form\ViewPartialProviderInterface;
18
use Zend\Form\ElementPrepareAwareInterface;
19
use Zend\Form\FormInterface;
20
21
class GeoText extends Text implements ViewPartialProviderInterface, ElementPrepareAwareInterface
22
{
23
    
24
    protected $partial = 'geo/form/GeoText';
25
26
    protected $nameElement;
27
    protected $dataElement;
28
    protected $typeElement;
29
    protected $converter;
30
    protected $filter;
31
32
    public function __construct($name = null, array $options = null)
33
    {
34
        $this->nameElement = new Text('name');
35
        $this->dataElement = new Hidden('data');
36
        $this->typeElement = new Hidden('type');
37
38
        parent::__construct($name, $options);
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 32 can also be of type null; however, Zend\Form\Element::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
39
    }
40
41
    public function setOptions($options)
42
    {
43
        parent::setOptions($options);
44
45
        if (isset($options['engine_type'])) {
46
            $this->setType($options['engine_type']);
47
        }
48
49
        return $this;
50
    }
51
52
    public function setConverter($converter)
53
    {
54
        $this->converter = $converter;
55
56
        return $this;
57
    }
58
59
    public function setFilter($filter)
60
    {
61
        $this->filter = $filter;
62
63
        return $this;
64
    }
65
66
    public function getConverter()
67
    {
68
        if (!$this->converter) {
69
            $this->setConverter(new Converter());
70
        }
71
        return $this->converter;
72
    }
73
74
    public function getViewPartial()
75
    {
76
        return $this->partial;
77
    }
78
    
79
    public function setViewPartial($partial)
80
    {
81
        $this->partial = $partial;
82
        return $this;
83
    }
84
85
    /**
86
     * Prepare the form element (mostly used for rendering purposes)
87
     *
88
     * @param FormInterface $form
89
     *
90
     * @return mixed
91
     */
92
    public function prepareElement(FormInterface $form)
93
    {
94
        $name = $this->getName();
95
        $id = str_replace(array('[', ']'), array('-', ''), $name);
96
        $this->setAttribute('id', $id);
97
        $this->nameElement->setName($name . '[name]')
98
                          ->setAttribute('id', $id . '-name')
99
                          ->setAttribute('class', 'form-control geolocation');
100
        $this->dataElement->setName($name . '[data]')
101
                          ->setAttribute('id', $id . '-data');
102
        $this->typeElement->setName($name . '[type]')
103
                          ->setAttribute('id', $id . '-type');
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getDataElement()
110
    {
111
        return $this->dataElement;
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function getNameElement()
118
    {
119
        return $this->nameElement;
120
    }
121
122
    public function getTypeElement()
123
    {
124
        return $this->typeElement;
125
    }
126
127
    public function setType($type)
128
    {
129
        $this->typeElement->setValue($type);
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param mixed $value
136
     * @param null  $type
137
     *
138
     * @return $this
139
     */
140
    public function setValue($value, $type=null)
141
    {
142
        if ($value instanceOf Location) {
143
            $value = $this->getConverter()->toValue($value, $type ?: $this->typeElement->getValue());
144
        }
145
        if ('geo' == $value['type'] && empty($value['data'])) {
146
            $lonLat = $this->getConverter()->toCoordinates($value['name']);
147
            foreach($lonLat as $k=>$v) {
148
                 list($lon,$lat) = explode(',', $v, 2);
149
                 $latLon[]=$lat.','.$lon;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$latLon was never initialized. Although not strictly required by PHP, it is generally a good practice to add $latLon = 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...
150
            }
151
            $value['data'] = ['coordinates'=>[ (float) $lat, (float) $lon] ,'type'=>'Point', 'region' => '' ,'postalcode' =>'', 'country' => 'DE'];
0 ignored issues
show
Bug introduced by
The variable $lat 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...
Bug introduced by
The variable $lon 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...
152
        }
153
        if (!is_array($value)) {
154
            $value = explode('|', $value, 2);
155
            $value = [
156
                'name' => $value[0],
157
                'data' => isset($value[1]) ? $value[1] : '',
158
            ];
159
        }
160
161
        $this->nameElement->setValue($value['name']);
162
        $this->dataElement->setValue($value['data']);
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param string $type
169
     *
170
     * @return array|mixed
171
     */
172
    public function getValue($type = 'name')
173
    {
174
        switch ($type) {
175
            case 'entity':
176
            default:
177
                return $this->getConverter()->toEntity($this->dataElement->getValue(), $this->typeElement->getValue());
178
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
179
180
            case 'all':
0 ignored issues
show
Unused Code introduced by
case 'all': return a...getValue()); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
181
                return [
182
                    'name' => $this->nameElement->getValue(),
183
                    'data' => $this->dataElement->getValue(),
184
                    'type' => $this->typeElement->getValue(),
185
                ];
186
                break;
187
188
            case 'name':
189
                return $this->nameElement->getValue();
190
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
191
192
            case 'data':
193
                return $this->dataElement->getValue();
194
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
195
196
            case 'type':
197
                return $this->typeElement->getValue();
198
        }
199
    }
200
}
201