ViewPanel::logView()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
8
9
class ViewPanel extends AbstractSubscriablePanel implements IAjaxPanel
10
{
11
    /**
12
     * $limit.
13
     *
14
     * @var int
15
     */
16
    public $limit = 50;
17
    /**
18
     * $views.
19
     *
20
     * @var array
21
     */
22
    protected $views = [];
23
24
    /**
25
     * subscribe.
26
     */
27 2
    protected function subscribe()
28
    {
29 2
        if (version_compare($this->laravel->version(), 5.4, '>=') === true) {
30
            $this->laravel['events']->listen('composing:*', function ($key, $payload) {
31 1
                $this->logView($payload[0]);
32 1
            });
33
        } else {
34
            $this->laravel['events']->listen('composing:*', function ($payload) {
35 1
                $this->logView($payload);
36 1
            });
37
        }
38 2
    }
39
40
    /**
41
     * logView.
42
     *
43
     * @param  \Illuminate\Contracts\View\View
44
     * @return string
45
     */
46 2
    protected function logView($view)
47
    {
48 2
        $name = $view->getName();
49 2
        $data = $this->limitCollection(Arr::except($view->getData(), ['__env', 'app']));
50 2
        $path = static::editorLink($view->getPath());
51 2
        preg_match('/href=\"(.+)\"/', $path, $m);
52 2
        $path = (count($m) > 1) ? '(<a href="'.$m[1].'">source</a>)' : '';
53 2
        $this->views[] = compact('name', 'data', 'path');
54 2
    }
55
56
    /**
57
     * limitCollection.
58
     *
59
     * @param array $data
60
     * @return array
61
     */
62 2
    protected function limitCollection($data)
63
    {
64 2
        $results = [];
65 2
        foreach ($data as $key => $value) {
66 2
            if (is_array($value) === true && count($value) > $this->limit) {
67 2
                $value = array_slice($value, 0, $this->limit);
68
            }
69
70 2
            if ($value instanceof Collection && $value->count() > $this->limit) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Support\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
71 2
                $value = $value->take($this->limit);
72
            }
73
74 2
            $results[$key] = $value;
75
        }
76
77 2
        return $results;
78
    }
79
80
    /**
81
     * getAttributes.
82
     *
83
     * @return array
84
     */
85 2
    protected function getAttributes()
86
    {
87
        return [
88 2
            'rows' => $this->views,
89
        ];
90
    }
91
}
92