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

LineBeforeClassEndFixer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 70
ccs 18
cts 24
cp 0.75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createReview() 0 3 1
A getName() 0 3 1
A getDescription() 0 3 1
A process() 0 16 2
A getTokenNewValue() 0 11 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
  }