Completed
Push — master ( cd19a9...523c7b )
by Shcherbak
02:32
created

PhpFileStartLineReview::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\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 2
          return;
57
        }
58 3
      }
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
  }