Completed
Push — master ( af28eb...bd6b4d )
by Shcherbak
03:04
created

LineBeforeClassEndFixer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 73.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
wmc 6
lcom 0
cbo 6
ccs 17
cts 23
cp 0.7391
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createReview() 0 3 1
A getName() 0 3 1
A getDescription() 0 3 1
B process() 0 25 3
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\LineBeforeClassEnd;
4
5
  use Funivan\Cs\Fs\File;
6
  use Funivan\Cs\Report\Report;
7
8
  /**
9
   *
10
   */
11
  class LineBeforeClassEndFixer extends AbstractLineBeforeClassEnd {
12
13
    const NAME = 'php_line_before_class_end_fixer';
14
15
16
    /**
17
     * @return $this
18
     */
19
    public static function createReview() {
20
      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...
21
    }
22
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function getName() {
28
      return self::NAME;
29
    }
30
31
32
    /**
33
     * @return string
34
     */
35
    public function getDescription() {
36
      return 'Set one empty line before class closing tag';
37
    }
38
39
40
    /**
41
     * @param File $file
42
     * @param Report $report
43
     * @void
44
     */
45 4
    public function process(File $file, Report $report) {
46 4
      $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
47 4
      $tokens = $this->getInvalidTokens($collection);
48
49 4
      $emptyLines = "\n" . str_repeat("\n", $this->getLinesNum());
50
51
52 4
      foreach ($tokens as $token) {
53 3
        $report->addMessage($file, $this, 'Set one line before closing tag', $token->getLine());
54 3
        $value = $token->getValue();
55
56 3
        if ($token->getType() !== T_WHITESPACE) {
57 1
          $token->setValue($value . $emptyLines);
58 1
        } else {
59 2
          $lines = explode("\n", $value);
60 2
          $lineStart = current($lines);
61 2
          $lineEnd = end($lines);
62 2
          $token->setValue($lineStart . $emptyLines . $lineEnd);
63
64
        }
65
66 4
      }
67
68 4
      $file->getContent()->set($collection->assemble());
69 4
    }
70
71
  }