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

ComposerReview::canProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
  namespace Funivan\Cs\Tools\Composer;
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 ComposerReview implements FileTool {
15
16
    const NAME = 'composer_review';
17
18
19
    /**
20
     * @inheritdoc
21
     */
22 2
    public function getName() {
23 2
      return self::NAME;
24
    }
25
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getDescription() {
31
      return 'Validate composer.json file';
32
    }
33
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function canProcess(File $file) {
39
      return (new FileFilter())->name(['!^composer.json$!'])->isValid($file);
40
    }
41
42
43
    /**
44
     * @inheritdoc
45
     */
46 2
    public function process(File $file, Report $report) {
47
48 2
      $process = new Process(sprintf('composer validate %s', $file->getPath()));
49 2
      $process->run();
50
51 2
      if ($process->isSuccessful()) {
52 1
        return;
53
      }
54
55
56 1
      $errorOutput = $process->getErrorOutput();
57 1
      preg_match('!Parse error on line (\d+):!', $errorOutput, $matchedLine);
58 1
      $line = 1;
59 1
      if (isset($matchedLine[1])) {
60 1
        $line = (int) $matchedLine[1];
61
      }
62
63 1
      $report->addMessage($file, $this, 'Invalid composer.json file format', $line);
64 1
    }
65
66
  }