| Conditions | 25 |
| Paths | 16376 |
| Total Lines | 163 |
| Code Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 234 | public function getDeltaFeed(?string $cursor = null, int $limit = 250, array $attributes = [], ?NodeInterface $node = null): array |
||
| 235 | { |
||
| 236 | $this->user->findNewShares(); |
||
| 237 | |||
| 238 | $attributes = array_merge( |
||
| 239 | ['id', 'directory', 'deleted', 'path', 'changed', 'created', 'owner'], |
||
| 240 | $attributes |
||
| 241 | ); |
||
| 242 | |||
| 243 | $cursor = $this->decodeCursor($cursor); |
||
| 244 | |||
| 245 | if (null === $cursor || 'initial' === $cursor[0]) { |
||
| 246 | return $this->buildFeedFromCurrentState($cursor, $limit, $attributes, $node); |
||
| 247 | } |
||
| 248 | |||
| 249 | try { |
||
| 250 | if (0 === $cursor[3]) { |
||
| 251 | $filter = $this->getDeltaFilter(); |
||
| 252 | } else { |
||
| 253 | //check if delta entry actually exists |
||
| 254 | if (0 === $this->db->delta->count(['_id' => new ObjectId($cursor[3])])) { |
||
| 255 | return $this->buildFeedFromCurrentState(null, $limit, $attributes, $node); |
||
| 256 | } |
||
| 257 | |||
| 258 | $filter = $this->getDeltaFilter(); |
||
| 259 | $filter = [ |
||
| 260 | '$and' => [ |
||
| 261 | ['timestamp' => ['$gte' => new UTCDateTime($cursor[4])]], |
||
| 262 | ['_id' => ['$gt' => new ObjectId($cursor[3])]], |
||
| 263 | $filter, |
||
| 264 | ], |
||
| 265 | ]; |
||
| 266 | } |
||
| 267 | |||
| 268 | $result = $this->db->delta->find($filter, [ |
||
| 269 | 'skip' => (int) $cursor[1], |
||
| 270 | 'limit' => (int) $limit, |
||
| 271 | 'sort' => ['timestamp' => 1], |
||
| 272 | ]); |
||
| 273 | |||
| 274 | $left = $this->db->delta->count($filter, [ |
||
| 275 | 'skip' => (int) $cursor[1], |
||
| 276 | 'sort' => ['timestamp' => 1], |
||
| 277 | ]); |
||
| 278 | |||
| 279 | $result = $result->toArray(); |
||
| 280 | $count = count($result); |
||
| 281 | $list = []; |
||
| 282 | $last_id = $cursor[3]; |
||
| 283 | $last_ts = $cursor[4]; |
||
| 284 | } catch (\Exception $e) { |
||
| 285 | return $this->buildFeedFromCurrentState(null, $limit, $attributes, $node); |
||
| 286 | } |
||
| 287 | |||
| 288 | $cursor = $cursor[1] += $limit; |
||
| 289 | $has_more = ($left - $count) > 0; |
||
| 290 | if (false === $has_more) { |
||
| 291 | $cursor = 0; |
||
| 292 | } |
||
| 293 | |||
| 294 | foreach ($result as $log) { |
||
| 295 | if (false === $has_more) { |
||
| 296 | $last_id = (string) $log['_id']; |
||
| 297 | $last_ts = (string) $log['timestamp']; |
||
| 298 | } |
||
| 299 | |||
| 300 | try { |
||
| 301 | $log_node = $this->fs->findNodeById($log['node'], null, NodeInterface::DELETED_EXCLUDE); |
||
| 302 | if (null !== $node && !$node->isSubNode($log_node)) { |
||
| 303 | continue; |
||
| 304 | } |
||
| 305 | |||
| 306 | //include share children after a new reference was added, otherwise the children would be lost if the cursor is newer |
||
| 307 | //than the create timestamp of the share reference |
||
| 308 | if ('addCollectionReference' === $log['operation'] && $log_node->isReference()) { |
||
| 309 | $members = $this->fs->findNodesByFilter([ |
||
| 310 | 'shared' => $log_node->getShareId(), |
||
| 311 | 'deleted' => false, |
||
| 312 | ]); |
||
| 313 | |||
| 314 | foreach ($members as $share_member) { |
||
| 315 | $member_attrs = $this->decorator->decorate($share_member, $attributes); |
||
| 316 | $list[$member_attrs['path']] = $member_attrs; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | $fields = $this->decorator->decorate($log_node, $attributes); |
||
| 321 | |||
| 322 | if (array_key_exists('previous', $log)) { |
||
| 323 | if (array_key_exists('parent', $log['previous'])) { |
||
| 324 | if ($log['previous']['parent'] === null) { |
||
| 325 | $previous_path = DIRECTORY_SEPARATOR.$log['name']; |
||
| 326 | } else { |
||
| 327 | $parent = $this->fs->findNodeById($log['previous']['parent']); |
||
| 328 | $previous_path = $parent->getPath().DIRECTORY_SEPARATOR.$log['name']; |
||
| 329 | } |
||
| 330 | } elseif (array_key_exists('name', $log['previous'])) { |
||
| 331 | if (null === $log['parent']) { |
||
| 332 | $previous_path = DIRECTORY_SEPARATOR.$log['previous']['name']; |
||
| 333 | } else { |
||
| 334 | $parent = $this->fs->findNodeById($log['parent']); |
||
| 335 | $previous_path = $parent->getPath().DIRECTORY_SEPARATOR.$log['previous']['name']; |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | $list[$fields['path']] = $fields; |
||
| 339 | |||
| 340 | continue; |
||
| 341 | } |
||
| 342 | |||
| 343 | $deleted_node = [ |
||
| 344 | 'id' => (string) $log['node'], |
||
| 345 | 'deleted' => true, |
||
| 346 | 'created' => null, |
||
| 347 | 'changed' => Helper::DateTimeToUnix($log['timestamp']), |
||
| 348 | 'path' => $previous_path, |
||
| 349 | 'directory' => $fields['directory'], |
||
| 350 | ]; |
||
| 351 | |||
| 352 | $list[$previous_path] = $deleted_node; |
||
| 353 | $list[$fields['path']] = $fields; |
||
| 354 | } else { |
||
| 355 | $list[$fields['path']] = $fields; |
||
| 356 | } |
||
| 357 | } catch (ForbiddenException $e) { |
||
| 358 | //no delta entriy for a node where we do not have access to |
||
| 359 | } catch (\Exception $e) { |
||
| 360 | try { |
||
| 361 | if (null === $log['parent']) { |
||
| 362 | $path = DIRECTORY_SEPARATOR.$log['name']; |
||
| 363 | } else { |
||
| 364 | $parent = $this->fs->findNodeById($log['parent']); |
||
| 365 | $path = $parent->getPath().DIRECTORY_SEPARATOR.$log['name']; |
||
| 366 | } |
||
| 367 | |||
| 368 | $entry = [ |
||
| 369 | 'id' => (string) $log['node'], |
||
| 370 | 'deleted' => true, |
||
| 371 | 'created' => null, |
||
| 372 | 'changed' => Helper::DateTimeToUnix($log['timestamp']), |
||
| 373 | 'path' => $path, |
||
| 374 | ]; |
||
| 375 | |||
| 376 | if ('deleteCollection' === substr($log['operation'], 0, 16)) { |
||
| 377 | $entry['directory'] = true; |
||
| 378 | } elseif ('deleteFile' === substr($log['operation'], 0, 10)) { |
||
| 379 | $entry['directory'] = false; |
||
| 380 | } |
||
| 381 | |||
| 382 | $list[$path] = $entry; |
||
| 383 | } catch (\Exception $e) { |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | $cursor = base64_encode('delta|'.$cursor.'|0|'.$last_id.'|'.$last_ts); |
||
| 389 | |||
| 390 | return [ |
||
| 391 | 'reset' => false, |
||
| 392 | 'cursor' => $cursor, |
||
| 393 | 'has_more' => $has_more, |
||
| 394 | 'nodes' => array_values($list), |
||
| 395 | ]; |
||
| 396 | } |
||
| 397 | |||
| 643 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: