Completed
Push — master ( 819344...58056e )
by Carlos
02:20
created

Lint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasSyntaxError() 0 4 1
A getSyntaxError() 0 10 2
A parseError() 0 13 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/phplint.
5
 *
6
 * (c) 2016 overtrue <[email protected]>
7
 */
8
9
namespace Overtrue\PHPLint\Process;
10
11
use Symfony\Component\Process\Process;
12
13
/**
14
 * Class Lint.
15
 */
16
class Lint extends Process
17
{
18
    /**
19
     * @return bool
20
     */
21
    public function hasSyntaxError()
22
    {
23
        return strpos($this->getOutput(), 'No syntax errors detected') === false;
24
    }
25
26
    /**
27
     * @return bool|string
28
     */
29
    public function getSyntaxError()
30
    {
31
        if ($this->hasSyntaxError()) {
32
            list(, $out) = explode("\n", $this->getOutput());
33
34
            return $this->parseError($out);
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Parse error message.
42
     *
43
     * @param  string $message
44
     *
45
     * @return array
46
     */
47
    public function parseError($message)
48
    {
49
        $pattern = '/^Parse error:\s*(?:\w+ error,\s*)?(?<error>.+?)\s+in\s+.+?\s*line\s+(?<line>\d+)/';
50
51
        preg_match($pattern, $message, $match);
52
53
        $match = array_merge(['error' => 'Unknown', 'line' => 0], $match);
54
55
        return [
56
            'error' => $match['error'],
57
            'line' => $match['line'],
58
        ];
59
    }
60
}
61