These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Webino (http://webino.sk) |
||
4 | * |
||
5 | * @link https://github.com/webino/WebinoDraw for the canonical source repository |
||
6 | * @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
||
7 | * @author Peter Bačinský <[email protected]> |
||
8 | * @license BSD-3-Clause |
||
9 | */ |
||
10 | |||
11 | namespace WebinoDraw\VarTranslator\Operation; |
||
12 | |||
13 | use ArrayObject; |
||
14 | use WebinoDraw\Exception; |
||
15 | use WebinoDraw\VarTranslator\Translation; |
||
16 | use Zend\View\HelperPluginManager; |
||
17 | use Zend\View\Helper\HelperInterface; |
||
18 | |||
19 | /** |
||
20 | * Class Helper |
||
21 | */ |
||
22 | class Helper |
||
23 | { |
||
24 | /** |
||
25 | * @var HelperPluginManager |
||
26 | */ |
||
27 | protected $helpers; |
||
28 | |||
29 | /** |
||
30 | * @param HelperPluginManager $helpers |
||
31 | */ |
||
32 | public function __construct(HelperPluginManager $helpers) |
||
33 | { |
||
34 | $this->helpers = $helpers; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Apply functions and view helpers on variables |
||
39 | * |
||
40 | * Call function if exists else call helper. |
||
41 | * |
||
42 | * @param Translation $translation Variables with values to modify |
||
43 | * @param array $spec Helper options |
||
44 | * @return $this |
||
45 | */ |
||
46 | public function apply(Translation $translation, array $spec) |
||
47 | { |
||
48 | $results = new ArrayObject; |
||
49 | foreach ($spec as $key => $subSpec) { |
||
50 | if ($translation->offsetExists($key)) { |
||
51 | $this->iterateHelperSpec((array) $subSpec, $key, $translation, $results); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $translation->merge($results->getArrayCopy()); |
||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array $spec |
||
61 | * @param mixed $key |
||
62 | * @param Translation $translation |
||
63 | * @param ArrayObject $results |
||
64 | * @return $this |
||
65 | * @throws Exception\InvalidInstructionException |
||
66 | */ |
||
67 | protected function iterateHelperSpec(array $spec, $key, Translation $translation, ArrayObject $results) |
||
68 | { |
||
69 | $varTranslation = $translation->getVarTranslation(); |
||
70 | |||
71 | $joinResult = true; |
||
72 | foreach ($spec as $helper => $options) { |
||
73 | if (null === $options) { |
||
74 | continue; |
||
75 | } |
||
76 | if ('_join_result' === $helper) { |
||
77 | // option to disable the string result joining |
||
78 | $joinResult = (bool) $options; |
||
79 | continue; |
||
80 | } |
||
81 | |||
82 | if (!is_array($options)) { |
||
83 | throw new Exception\InvalidInstructionException( |
||
84 | 'Expected array options for spec ' . print_r($spec, true) |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | if (!empty($options['helper'])) { |
||
89 | // helper is not an options key |
||
90 | $helper = $options['helper']; |
||
91 | unset($options['helper']); |
||
92 | } |
||
93 | |||
94 | $varTranslation->translate($helper); |
||
95 | $this->callCallableHelper($helper, $key, $translation, $results, $options) |
||
96 | or $this->callHelper($helper, $key, $translation, $results, $options, $joinResult); |
||
97 | } |
||
98 | |||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param mixed $helper |
||
104 | * @param mixed $key |
||
105 | * @param Translation $translation |
||
106 | * @param ArrayObject $results |
||
107 | * @param array $options |
||
108 | * @return bool |
||
109 | * @throws Exception\InvalidInstructionException |
||
110 | */ |
||
111 | protected function callCallableHelper( |
||
112 | $helper, |
||
113 | $key, |
||
114 | Translation $translation, |
||
115 | ArrayObject $results, |
||
116 | array $options |
||
117 | ) { |
||
118 | if (!is_callable($helper)) { |
||
119 | return false; |
||
120 | } |
||
121 | |||
122 | $translation->merge($results->getArrayCopy())->getVarTranslation()->translate($options); |
||
123 | if (!is_array($options)) { |
||
124 | throw new Exception\LogicException('Expected options of type array'); |
||
125 | } |
||
126 | |||
127 | $results[$key] = call_user_func_array($helper, $options); |
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Call ZF helper |
||
133 | * |
||
134 | * @param string $helper |
||
135 | * @param mixed $key |
||
136 | * @param Translation $translation |
||
137 | * @param ArrayObject $results |
||
138 | * @param array $options |
||
139 | * @param bool $joinResult |
||
140 | * @return $this |
||
141 | */ |
||
142 | protected function callHelper( |
||
143 | $helper, |
||
144 | $key, |
||
145 | Translation $translation, |
||
146 | ArrayObject $results, |
||
147 | array $options, |
||
148 | $joinResult |
||
149 | ) { |
||
150 | $plugin = $this->helpers->get($helper); |
||
151 | foreach ($options as $func => $calls) { |
||
152 | if (null === $calls) { |
||
153 | continue; |
||
154 | } |
||
155 | |||
156 | foreach ($calls as $params) { |
||
157 | if (null === $params) { |
||
158 | continue; |
||
159 | } |
||
160 | |||
161 | $translation->merge($results->getArrayCopy())->getVarTranslation()->translate($params); |
||
162 | if (!is_array($params)) { |
||
163 | throw new Exception\LogicException('Expected params of type array'); |
||
164 | } |
||
165 | |||
166 | $plugin = call_user_func_array([$plugin, $func], $params); |
||
167 | if (is_string($plugin)) { |
||
168 | break; |
||
169 | } |
||
170 | |||
171 | if (!($plugin instanceof HelperInterface) |
||
0 ignored issues
–
show
|
|||
172 | || is_array($plugin) |
||
173 | || is_int($plugin) |
||
174 | || is_float($plugin) |
||
175 | ) { |
||
176 | // support array results |
||
177 | $results[$key] = $plugin; |
||
178 | return $this; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | empty($results[$key]) |
||
184 | and $results[$key] = null; |
||
185 | |||
186 | if ($joinResult) { |
||
187 | $results[$key].= $plugin; |
||
188 | } else { |
||
189 | $results[$key] = $plugin; |
||
190 | } |
||
191 | |||
192 | return $this; |
||
193 | } |
||
194 | } |
||
195 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.