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

PhpFileStartLineReview::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\FileStartLine;
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
10
  /**
11
   * @author Ivan Shcherbak <[email protected]> 2016
12
   */
13
  class PhpFileStartLineReview implements FileTool {
14
15
    const NAME = 'php_file_start_line_review';
16
17
    const REGEXP = [
18
      '~^<\?php(\s+|$)~',
19
      '~^<\?(\s+|$)~',
20
      '~#!/usr/bin/env php(\s+|$)~',
21
    ];
22
23
24
    /**
25
     * @inheritdoc
26
     */
27 4
    public function getName() {
28 4
      return self::NAME;
29
    }
30
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function getDescription() {
36
      return 'Check php files first line';
37
    }
38
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function canProcess(File $file) {
44
      return (new FileFilter())->notDeleted()->extension(['php'])->isValid($file);
45
    }
46
47
48
    /**
49
     * @inheritdoc
50
     */
51 4
    public function process(File $file, Report $report) {
52 4
      $fileContent = $file->getContent()->get();
53
54 4
      foreach (self::REGEXP as $regexp) {
55 4
        if (preg_match($regexp, $fileContent) === 1) {
56 4
          return;
57
        }
58
      }
59
60 2
      $message = 'File must begin with `<?php` or `<?` or `#!/usr/bin/env php`';
61 2
      $report->addMessage($file, $this, $message, 1);
62
63 2
    }
64
65
  }