SpacesInEmptyLinesFixer::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
  namespace Funivan\Cs\Tools\SpacesInEmptyLines;
4
5
  use Funivan\Cs\Fs\File;
6
  use Funivan\Cs\Report\Report;
7
8
  /**
9
   */
10
  class SpacesInEmptyLinesFixer extends AbstractSpacesInEmptyLines {
11
12
    const NAME = 'spaces_in_empty_lines_fixes';
13
14
15
    /**
16
     * @codeCoverageIgnore
17
     * @inheritdoc
18
     */
19
    public function getName() {
20
      return self::NAME;
21
    }
22
23
24
    /**
25
     * @codeCoverageIgnore
26
     * @return string
27
     */
28
    public function getDescription() {
29
      return 'Remove spaces in empty lines';
30
    }
31
32
33
    /**
34
     * @inheritdoc
35
     */
36 4
    public function process(File $file, Report $report) {
37 4
      $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
38 4
      $stripTokens = $this->findTokens($collection);
39
40
41 4
      $invalidTokensNum = $stripTokens->count();
42
43 4
      $lastInvalidToken = $this->getLastInvalidToken($collection);
44
45 4
      if (null !== $lastInvalidToken) {
46 2
        $invalidTokensNum++;
47
      }
48
49 4
      if ($invalidTokensNum === 0) {
50 1
        return;
51
      }
52
53 3
      $report->addMessage($file, $this, 'Find empty lines with spaces: ' . $invalidTokensNum);
54
55 3
      foreach ($stripTokens as $token) {
56 2
        $value = $token->getValue();
57 2
        $token->setValue($this->replaceEmptyLines($value));
58
      }
59
60 3
      if (null !== $lastInvalidToken) {
61 2
        $value = $lastInvalidToken->getValue();
62 2
        $value = $this->replaceEmptyLines($value);
63 2
        $value = preg_replace('![ ]+(\n*)$!', '$1', $value);
64 2
        $value = preg_replace('!^[ ]+!', '', $value);
65 2
        $lastInvalidToken->setValue($value);
66
      }
67
68 3
      $file->getContent()->set($collection->assemble());
69 3
    }
70
71
72
    /**
73
     * @param string $value
74
     * @return string
75
     */
76 3
    protected function replaceEmptyLines($value) {
77 3
      return preg_replace('!\n([ ]+)\n!', "\n\n", $value);
78
    }
79
80
  }
81