Completed
Push — master ( bfc368...43106f )
by Shcherbak
02:26
created

LineAfterOpenTagFixer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
B process() 0 36 5
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\LineAfterOpenTag;
4
5
  use Funivan\Cs\FileFinder\FileInfo;
6
  use Funivan\Cs\Message\Report;
7
  use Funivan\PhpTokenizer\Token;
8
9
  /**
10
   *
11
   */
12
  class LineAfterOpenTagFixer extends AbstractLineAfterOpenTag {
13
14
    const NAME_FIXER = 'php_line_after_open_tag_fixer';
15
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public function getName() {
21
      return self::NAME_FIXER;
22
    }
23
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function getDescription() {
29
      return 'Set one empty line after php open tag';
30
    }
31
32
33
    /**
34
     * @param FileInfo $file
35
     * @param Report $report
36
     * @void
37
     */
38
    public function process(FileInfo $file, Report $report) {
39
40
      $items = $this->getInvalidStartTokens($file);
41
      if (count($items) === 0) {
42
        return;
43
      }
44
45
      //$report->addNotice($file, $this, 'Add empty line after tag');
46
47
48
      foreach ($items as $tokenInfo) {
49
        /** @var Token $token */
50
        $whitespace = $tokenInfo->getWhitespace();
51
52
        $token = $tokenInfo->getToken();
53
        $tokenValue = $token->getValue();
54
        $whitespaceValue = $whitespace->getValue() ? $whitespace->getValue() : '';
55
        $whitespace->remove();
56
57
58
        preg_match('!([ ]+)$!', $whitespaceValue, $endSpaces);
59
60
61
        $whitespaceValue = !empty($endSpaces[1]) ? $endSpaces[1] : '';
62
63
64
        $lines = explode("\n", $tokenValue);
65
        $tokenValue = reset($lines);
66
67
68
        $append = $tokenValue . "\n\n" . $whitespaceValue;
69
70
        $token->setValue($append);
71
        $report->addNotice($file, $this, 'Set one empty line after php open tag', $token->getValue());
72
      }
73
    }
74
75
  }