FileStartLineReview::getDescription()   A
last analyzed

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