Completed
Push — master ( 58b9b1...400fee )
by Shcherbak
04:25
created

FileStartLineReview::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 FileStartLineReview 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 5
    public function getName() {
28 5
      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 5
    public function process(File $file, Report $report) {
52 5
      $fileContent = $file->getContent()->get();
53
54 5
      foreach (self::REGEXP as $regexp) {
55 5
        if (preg_match($regexp, $fileContent) === 1) {
56 5
          return;
57
        }
58
      }
59
60 3
      $message = 'File must begin with `<?php` or `<?` or `#!/usr/bin/env php`';
61 3
      $report->addMessage($file, $this, $message, 1);
62
63 3
    }
64
65
  }