Completed
Push — master ( 816b60...a0065d )
by Shcherbak
03:15
created

PhpSyntaxCheckReview::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 8.5806
cc 4
eloc 18
nc 4
nop 2
crap 4
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\SyntaxCheck;
4
5
  use Funivan\Cs\FileTool\FileTool;
6
  use Funivan\Cs\Fs\File;
7
  use Funivan\Cs\Fs\FileFilter;
8
  use Funivan\Cs\Report\Report;
9
  use Symfony\Component\Process\Process;
10
11
  /**
12
   * @author Ivan Shcherbak <[email protected]> 2016
13
   */
14
  class PhpSyntaxCheckReview implements FileTool {
15
16
    const NAME = 'php_syntax_check_review';
17
18
19
    /**
20
     * @inheritdoc
21
     */
22 3
    public function getName() {
23 3
      return self::NAME;
24
    }
25
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getDescription() {
31
      return 'Check php and html files syntax using standard php lint tool';
32
    }
33
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function canProcess(File $file) {
39
      return (new FileFilter())->notDeleted()->extension(['php', 'phtml', 'html'])->isValid($file);
40
    }
41
42
43
    /**
44
     * @inheritdoc
45
     */
46 3
    public function process(File $file, Report $report) {
47 3
      $cmd = sprintf('php --syntax-check %s', $file->getPath());
48
49 3
      $process = new Process($cmd);
50 3
      $process->run();
51
52
      # Create the array of outputs and remove empty values.
53 3
      $output = array_filter(explode(PHP_EOL, $process->getOutput()));
54
55 3
      if ($process->isSuccessful()) {
56 1
        return;
57
      }
58
59 2
      $regex = '!\s+on line (\d+)!';
60
61 2
      $needle = 'Parse error: syntax error, ';
62 2
      foreach (array_slice($output, 0, count($output) - 1) as $error) {
63 2
        $raw = ucfirst(substr($error, strlen($needle)));
64 2
        $message = str_replace(' in ' . $file->getPath(), '', $raw);
65
66 2
        $line = 0;
67 2
        preg_match($regex, $message, $lineMatch);
68 2
        if (isset($lineMatch[1])) {
69 2
          $line = (int) $lineMatch[1];
70 2
          $message = preg_replace($regex, '', $message);
71
        }
72
73 2
        $report->addMessage($file, $this, $message, $line);
74
      }
75
76 2
    }
77
78
  }