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

FileStartLineReview   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 53
ccs 10
cts 14
cp 0.7143
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
A canProcess() 0 3 1
A process() 0 13 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 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
  }