Conditions | 9 |
Paths | 73 |
Total Lines | 93 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 4 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
155 | public function log($level, $template, array $context = []) { |
||
156 | if ($level > $this->limit) { |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | // Convert PSR3-style messages to SafeMarkup::format() style, so they can be |
||
161 | // translated too in runtime. |
||
162 | $message_placeholders = $this->parser->parseMessagePlaceholders($template, $context); |
||
163 | |||
164 | // If code location information is all present, as for errors/exceptions, |
||
165 | // then use it to build the message template id. |
||
166 | $type = $context['channel']; |
||
167 | $location_info = [ |
||
168 | '%type' => 1, |
||
169 | '@message' => 1, |
||
170 | '%function' => 1, |
||
171 | '%file' => 1, |
||
172 | '%line' => 1, |
||
173 | ]; |
||
174 | if (!empty(array_diff_key($location_info, $message_placeholders))) { |
||
175 | $this->enhanceLogEntry($message_placeholders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10)); |
||
176 | } |
||
177 | $file = $message_placeholders['%file']; |
||
178 | $line = $message_placeholders['%line']; |
||
179 | $function = $message_placeholders['%function']; |
||
180 | $key = "${type}:${level}:${file}:${line}:${function}"; |
||
181 | $template_id = md5($key); |
||
182 | |||
183 | $selector = ['_id' => $template_id]; |
||
184 | $update = [ |
||
185 | '_id' => $template_id, |
||
186 | 'type' => Unicode::substr($context['channel'], 0, 64), |
||
187 | 'message' => $template, |
||
188 | 'severity' => $level, |
||
189 | ]; |
||
190 | $options = ['upsert' => TRUE]; |
||
191 | $template_result = $this->database |
||
192 | ->selectCollection(static::TEMPLATE_COLLECTION) |
||
193 | ->replaceOne($selector, $update, $options); |
||
194 | // Only add the template if if has not already been added. |
||
195 | if ($this->requestTracking) { |
||
196 | $request_id = $this->requestStack |
||
197 | ->getCurrentRequest() |
||
198 | ->server |
||
199 | ->get('UNIQUE_ID'); |
||
200 | |||
201 | if (isset($this->templates[$template_id])) { |
||
202 | $this->templates[$template_id]++; |
||
203 | } |
||
204 | else { |
||
205 | $this->templates[$template_id] = 1; |
||
206 | $selector = ['_id' => $request_id]; |
||
207 | $update = ['$addToSet' => ['templates' => $template_id]]; |
||
208 | $this->trackerCollection()->updateOne($selector, $update, $options); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $event_collection = $this->eventCollection($template_id); |
||
213 | if ($template_result->getUpsertedCount()) { |
||
214 | // Capped collections are actually size-based, not count-based, so "items" |
||
215 | // is only a maximum, assuming event documents weigh 1kB, but the actual |
||
216 | // number of items stored may be lower if items are heavier. |
||
217 | // We do not use 'autoindexid' for greater speed, because: |
||
218 | // - it does not work on replica sets, |
||
219 | // - it is deprecated in MongoDB 3.2 and going away in 3.4. |
||
220 | $options = [ |
||
221 | 'capped' => TRUE, |
||
222 | 'size' => $this->items * 1024, |
||
223 | 'max' => $this->items, |
||
224 | ]; |
||
225 | $this->database->createCollection($event_collection->getCollectionName(), $options); |
||
226 | } |
||
227 | |||
228 | foreach ($message_placeholders as &$placeholder) { |
||
229 | if ($placeholder instanceof MarkupInterface) { |
||
|
|||
230 | $placeholder = Xss::filterAdmin($placeholder); |
||
231 | } |
||
232 | } |
||
233 | $event = [ |
||
234 | 'hostname' => Unicode::substr($context['ip'], 0, 128), |
||
235 | 'link' => $context['link'], |
||
236 | 'location' => $context['request_uri'], |
||
237 | 'referer' => $context['referer'], |
||
238 | 'timestamp' => $context['timestamp'], |
||
239 | 'user' => ['uid' => $context['uid']], |
||
240 | 'variables' => $message_placeholders, |
||
241 | ]; |
||
242 | if ($this->requestTracking) { |
||
243 | // Fetch the current request on each event to support subrequest nesting. |
||
244 | $event['requestTracking_id'] = $request_id; |
||
245 | } |
||
246 | $event_collection->insertOne($event); |
||
247 | } |
||
248 | |||
350 |
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.