Completed
Push — master ( 6897f4...719118 )
by Shcherbak
04:56 queued 01:02
created

LineBeforeClassEndFixer::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\LineBeforeClassEnd;
4
5
  use Funivan\Cs\Fs\File;
6
  use Funivan\Cs\Report\Report;
7
  use Funivan\PhpTokenizer\Token;
8
9
  /**
10
   *
11
   */
12
  class LineBeforeClassEndFixer extends AbstractLineBeforeClassEnd {
13
14
    const NAME = 'php_line_before_class_end_fixer';
15
16
17
    /**
18
     * @return $this
19
     */
20
    public static function createReview() {
21
      return new static(false);
0 ignored issues
show
Unused Code introduced by
The call to LineBeforeClassEndFixer::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
22
    }
23
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function getName() {
29
      return self::NAME;
30
    }
31
32
33
    /**
34
     * @return string
35
     */
36
    public function getDescription() {
37
      return 'Set one empty line before class closing tag';
38
    }
39
40
41
    /**
42
     * @param File $file
43
     * @param Report $report
44
     * @void
45
     */
46 4
    public function process(File $file, Report $report) {
47 4
      $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
48 4
      $tokens = $this->getInvalidTokens($collection);
49
50 4
      $emptyLines = "\n" . str_repeat("\n", $this->getLinesNum());
51
52
53 4
      foreach ($tokens as $token) {
54 3
        $report->addMessage($file, $this, 'Set one line before closing tag', $token->getLine());
55
56 3
        $newValue = $this->getTokenNewValue($token, $emptyLines);
57 3
        $token->setValue($newValue);
58 4
      }
59
60 4
      $file->getContent()->set($collection->assemble());
61 4
    }
62
63
64
    /**
65
     * @param Token $token
66
     * @param string $emptyLines
67
     * @return string
68
     */
69 4
    protected function getTokenNewValue(Token $token, $emptyLines) {
70
71 3
      if ($token->getType() !== T_WHITESPACE) {
72 1
        return $token->getValue() . $emptyLines;
73
      }
74
75 4
      $lines = explode("\n", $token->getValue());
76 2
      $lineStart = current($lines);
77 2
      $lineEnd = end($lines);
78 2
      return $lineStart . $emptyLines . $lineEnd;
79
    }
80
81
  }