Completed
Push — master ( 0a92dc...4f5269 )
by ARCANEDEV
02:09
created

LogParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 21 4
A extractDate() 0 4 1
A parseRawData() 0 13 2
A hasLogLevel() 0 4 1
A populateEntries() 0 12 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
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class LogParser
16
{
17
    /* -----------------------------------------------------------------
18
     |  Constants
19
     | -----------------------------------------------------------------
20
     */
21
22
    const REGEX_DATE_PATTERN     = '\d{4}(-\d{2}){2}';
23
    const REGEX_TIME_PATTERN     = '\d{2}(:\d{2}){2}';
24
    const REGEX_DATETIME_PATTERN = self::REGEX_DATE_PATTERN.' '.self::REGEX_TIME_PATTERN;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * Parsed data.
33
     *
34
     * @var array
35
     */
36
    protected static $parsed = [];
37
38
    /* -----------------------------------------------------------------
39
     |  Main Methods
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Parse file content.
45
     *
46
     * @param  string  $raw
47
     *
48
     * @return array
49
     */
50 378
    public static function parse($raw)
51
    {
52 378
        static::$parsed          = [];
53 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...
54
55
        // @codeCoverageIgnoreStart
56
        if ( ! is_array($headings)) {
57
            return static::$parsed;
58
        }
59
        // @codeCoverageIgnoreEnd
60
61 378
        foreach ($headings as $heading) {
62 378
            for ($i = 0, $j = count($heading); $i < $j; $i++) {
63 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...
64
            }
65
        };
66
67 378
        unset($headings, $data);
68
69 378
        return array_reverse(static::$parsed);
70
    }
71
72
    /* -----------------------------------------------------------------
73
     |  Other Methods
74
     | -----------------------------------------------------------------
75
     */
76
77
    /**
78
     * Extract the date.
79
     *
80
     * @param  string  $string
81
     *
82
     * @return string
83
     */
84 390
    public static function extractDate(string $string): string
85
    {
86 390
        return preg_replace('/.*('.self::REGEX_DATE_PATTERN.').*/', '$1', $string);
87
    }
88
89
    /**
90
     * Parse raw data.
91
     *
92
     * @param  string  $raw
93
     *
94
     * @return array
95
     */
96 378
    private static function parseRawData($raw)
97
    {
98 378
        $pattern = '/\['.self::REGEX_DATETIME_PATTERN.'\].*/';
99 378
        preg_match_all($pattern, $raw, $headings);
100 378
        $data    = preg_split($pattern, $raw);
101
102 378
        if ($data[0] < 1) {
103 378
            $trash = array_shift($data);
104 378
            unset($trash);
105
        }
106
107 378
        return [$headings, $data];
108
    }
109
110
    /**
111
     * Populate entries.
112
     *
113
     * @param  array  $heading
114
     * @param  array  $data
115
     * @param  int    $key
116
     */
117 378
    private static function populateEntries($heading, $data, $key)
118
    {
119 378
        foreach (LogLevels::all() as $level) {
120 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...
121
                static::$parsed[] = [
122 378
                    'level'  => $level,
123 378
                    'header' => $heading[$key],
124 378
                    'stack'  => $data[$key]
125
                ];
126
            }
127
        }
128 378
    }
129
130
    /**
131
     * Check if header has a log level.
132
     *
133
     * @param  string  $heading
134
     * @param  string  $level
135
     *
136
     * @return bool
137
     */
138 378
    private static function hasLogLevel($heading, $level)
139
    {
140 378
        return Str::contains($heading, strtoupper(".{$level}:"));
141
    }
142
}
143