Completed
Push — master ( 19af7f...765e83 )
by ARCANEDEV
14s queued 11s
created

LogParser::populateEntries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LogViewer\Helpers;
6
7
use Arcanedev\LogViewer\Utilities\LogLevels;
8
use Illuminate\Support\Str;
9
10
/**
11
 * Class     LogParser
12
 *
13
 * @package  Arcanedev\LogViewer\Helpers
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class LogParser
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const REGEX_DATE_PATTERN     = '\d{4}(-\d{2}){2}';
24
    const REGEX_TIME_PATTERN     = '\d{2}(:\d{2}){2}';
25
    const REGEX_DATETIME_PATTERN = self::REGEX_DATE_PATTERN.' '.self::REGEX_TIME_PATTERN;
26
27
    /* -----------------------------------------------------------------
28
     |  Properties
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * Parsed data.
34
     *
35
     * @var array
36
     */
37
    protected static $parsed = [];
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Parse file content.
46
     *
47
     * @param  string  $raw
48
     *
49
     * @return array
50
     */
51 378
    public static function parse($raw)
52
    {
53 378
        static::$parsed          = [];
54 378
        list($headings, $data) = static::parseRawData($raw);
0 ignored issues
show
Bug introduced by
Since parseRawData() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of parseRawData() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
55
56
        // @codeCoverageIgnoreStart
57
        if ( ! is_array($headings)) {
58
            return static::$parsed;
59
        }
60
        // @codeCoverageIgnoreEnd
61
62 378
        foreach ($headings as $heading) {
63 378
            for ($i = 0, $j = count($heading); $i < $j; $i++) {
64 378
                static::populateEntries($heading, $data, $i);
0 ignored issues
show
Bug introduced by
Since populateEntries() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of populateEntries() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
65
            }
66
        };
67
68 378
        unset($headings, $data);
69
70 378
        return array_reverse(static::$parsed);
71
    }
72
73
    /* -----------------------------------------------------------------
74
     |  Other Methods
75
     | -----------------------------------------------------------------
76
     */
77
78
    /**
79
     * Extract the date.
80
     *
81
     * @param  string  $string
82
     *
83
     * @return string
84
     */
85 390
    public static function extractDate(string $string): string
86
    {
87 390
        return preg_replace('/.*('.self::REGEX_DATE_PATTERN.').*/', '$1', $string);
88
    }
89
90
    /**
91
     * Parse raw data.
92
     *
93
     * @param  string  $raw
94
     *
95
     * @return array
96
     */
97 378
    private static function parseRawData($raw)
98
    {
99 378
        $pattern = '/\['.self::REGEX_DATETIME_PATTERN.'\].*/';
100 378
        preg_match_all($pattern, $raw, $headings);
101 378
        $data    = preg_split($pattern, $raw);
102
103 378
        if ($data[0] < 1) {
104 378
            $trash = array_shift($data);
105 378
            unset($trash);
106
        }
107
108 378
        return [$headings, $data];
109
    }
110
111
    /**
112
     * Populate entries.
113
     *
114
     * @param  array  $heading
115
     * @param  array  $data
116
     * @param  int    $key
117
     */
118 378
    private static function populateEntries($heading, $data, $key)
119
    {
120 378
        foreach (LogLevels::all() as $level) {
121 378
            if (static::hasLogLevel($heading[$key], $level)) {
0 ignored issues
show
Bug introduced by
Since hasLogLevel() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of hasLogLevel() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
122 378
                static::$parsed[] = [
123 378
                    'level'  => $level,
124 378
                    'header' => $heading[$key],
125 378
                    'stack'  => $data[$key]
126
                ];
127
            }
128
        }
129 378
    }
130
131
    /**
132
     * Check if header has a log level.
133
     *
134
     * @param  string  $heading
135
     * @param  string  $level
136
     *
137
     * @return bool
138
     */
139 378
    private static function hasLogLevel($heading, $level)
140
    {
141 378
        return Str::contains($heading, strtoupper(".{$level}:"));
142
    }
143
}
144