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

LineBeforeClassEndFixer::process()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.8571
cc 3
eloc 15
nc 3
nop 2
crap 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
  }