Completed
Push — master ( 2cf7e5...0496dc )
by Shcherbak
02:23
created

SpacesInEmptyLinesFixer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 68
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
B process() 0 31 5
A replaceEmptyLines() 0 3 1
1
<?php
2
3
  namespace Funivan\Cs\Tools\SpacesInEmptyLines;
4
5
  use Funivan\Cs\FileFinder\FileInfo;
6
  use Funivan\Cs\Message\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(FileInfo $file, Report $report) {
37 4
      $stripTokens = $this->findTokens($file);
38
39
40 4
      $invalidTokensNum = $stripTokens->count();
41
42 4
      $lastInvalidToken = $this->getLastInvalidToken($file);
43
44 4
      if (null !== $lastInvalidToken) {
45 2
        $invalidTokensNum++;
46 3
      }
47
48 4
      if ($invalidTokensNum === 0) {
49 1
        return;
50
      }
51
52 3
      $report->addNotice($file, $this, 'Find empty lines with spaces: ' . $invalidTokensNum);
53
54 3
      foreach ($stripTokens as $token) {
55 2
        $value = $token->getValue();
56 2
        $token->setValue($this->replaceEmptyLines($value));
57 3
      }
58
59 3
      if (null !== $lastInvalidToken) {
60 2
        $value = $lastInvalidToken->getValue();
61 2
        $value = $this->replaceEmptyLines($value);
62 2
        $value = preg_replace('![ ]+(\n*)$!', '$1', $value);
63 2
        $value = preg_replace('!^[ ]+!', '', $value);
64 2
        $lastInvalidToken->setValue($value);
65 2
      }
66 3
    }
67
68
69
    /**
70
     * @param string $value
71
     * @return string
72
     */
73 3
    protected function replaceEmptyLines($value) {
74 3
      return preg_replace('!\n([ ]+)\n!', "\n\n", $value);
75
    }
76
77
  }
78