These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Ivory Google Map package. |
||
5 | * |
||
6 | * (c) Eric GELOEN <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please read the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Ivory\GoogleMap\Helper\Renderer; |
||
13 | |||
14 | use Ivory\GoogleMap\Helper\Formatter\Formatter; |
||
15 | use Ivory\JsonBuilder\JsonBuilder; |
||
16 | |||
17 | /** |
||
18 | * @author GeLo <[email protected]> |
||
19 | */ |
||
20 | class LoaderRenderer extends AbstractJsonRenderer |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $language; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | private $key; |
||
31 | |||
32 | /** |
||
33 | * @param string $language |
||
34 | * @param string|null $key |
||
35 | */ |
||
36 | public function __construct(Formatter $formatter, JsonBuilder $jsonBuilder, $language = 'en', $key = null) |
||
37 | { |
||
38 | parent::__construct($formatter, $jsonBuilder); |
||
39 | |||
40 | $this->setLanguage($language); |
||
41 | $this->setKey($key); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getLanguage() |
||
48 | { |
||
49 | return $this->language; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $language |
||
54 | */ |
||
55 | public function setLanguage($language) |
||
56 | { |
||
57 | $this->language = $language; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function hasKey() |
||
64 | { |
||
65 | return null !== $this->key; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return string|null |
||
70 | */ |
||
71 | public function getKey() |
||
72 | { |
||
73 | return $this->key; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string|null $key |
||
78 | */ |
||
79 | public function setKey($key) |
||
80 | { |
||
81 | $this->key = $key; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | * @param string $callback |
||
87 | * @param string[] $libraries |
||
88 | * @param bool $newLine |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function render( |
||
93 | $name, |
||
94 | $callback, |
||
95 | array $libraries = [], |
||
96 | $newLine = true |
||
97 | ) { |
||
98 | $formatter = $this->getFormatter(); |
||
99 | $jsonBuilder = $this->getJsonBuilder(); |
||
100 | |||
101 | $parameters = ['language' => $this->language]; |
||
102 | |||
103 | if ($this->hasKey()) { |
||
104 | $parameters['key'] = $this->key; |
||
105 | } |
||
106 | |||
107 | if (!empty($libraries)) { |
||
108 | $parameters['libraries'] = implode(',', $libraries); |
||
109 | } |
||
110 | |||
111 | $jsonBuilder |
||
112 | ->setValue('[other_params]', urldecode(http_build_query($parameters, '', '&'))) |
||
113 | ->setValue('[callback]', $callback, false); |
||
114 | |||
115 | return $formatter->renderClosure($formatter->renderCall($formatter->renderProperty('google', 'load'), [ |
||
116 | $formatter->renderEscape('maps'), |
||
117 | $formatter->renderEscape('3'), |
||
118 | $jsonBuilder->build(), |
||
119 | ]), [], $name, true, $newLine); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $callback |
||
124 | * @param array|null $libraries |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function renderSource($callback, $libraries) |
||
129 | { |
||
130 | if ($this->hasKey()) { |
||
131 | $arguments['key'] = $this->key; |
||
0 ignored issues
–
show
|
|||
132 | } |
||
133 | |||
134 | if ($libraries) { |
||
135 | $arguments['libraries'] = implode(',', $libraries); |
||
0 ignored issues
–
show
The variable
$arguments 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
Loading history...
|
|||
136 | } |
||
137 | |||
138 | $arguments['callback'] = $callback; |
||
139 | |||
140 | return 'https://maps.googleapis.com/maps/api/js?'.http_build_query($arguments); |
||
141 | } |
||
142 | } |
||
143 |
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:
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 thebar
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.