Passed
Push — master ( 97112d...839910 )
by Gerrit
01:49 queued 11s
created

BlankLineBeforeElseBlockFixer::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Addiks\MorePhpCsFixers\Whitespace;
14
15
use PhpCsFixer\AbstractFixer;
16
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
17
use PhpCsFixer\FixerDefinition\CodeSample;
18
use PhpCsFixer\FixerDefinition\FixerDefinition;
19
use PhpCsFixer\Tokenizer\Token;
20
use PhpCsFixer\Tokenizer\Tokens;
21
use PhpCsFixer\WhitespacesFixerConfig;
22
23 View Code Duplication
final class BlankLineBeforeElseBlockFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25 7
    public function __construct()
26
    {
27 7
        parent::__construct();
28
29
        # Fixes psalm: PropertyNotSetInConstructor
30 7
        $this->whitespacesConfig = new WhitespacesFixerConfig();
31 7
    }
32
33
    public function getName()
34
    {
35
        return 'Addiks/blank_line_before_else_block';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function getDefinition()
42
    {
43 1
        return new FixerDefinition(
44 1
            'An empty line feed must precede any else or elseif codeblock.',
45
            [
46 1
                new CodeSample(
47 1
                    '<?php
48
if ($a) {
49
    foo();
50
51
} elseif ($b) {
52
    bar();
53
54
} else {
55
    baz();
56
}
57
'
58
                ),
59
            ]
60
        );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function getPriority()
67
    {
68
        // should run after no_blank_lines_after_phpdoc
69 1
        return -26;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 5
    public function isCandidate(Tokens $tokens)
76
    {
77 5
        return $tokens->isTokenKindFound(T_ELSEIF) || $tokens->isTokenKindFound(T_ELSE);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 5
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
84
    {
85
        /** @var string $lineEnding */
86 5
        $lineEnding = $this->whitespacesConfig->getLineEnding();
87
88 5
        foreach ($tokens as $index => $token) {
89
            /** @var Token $token */
90
91 5
            if ($token->isGivenKind([T_ELSE, T_ELSEIF])) {
92 5
                $index = (int)$tokens->getPrevMeaningfulToken($index);
93
94 5
                if ($tokens[$index]->equals('}')) {
95 5
                    --$index;
96
97 5
                    while ($tokens[$index]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
98 1
                        --$index;
99
                    }
100
101
                    /** @var Token $whitespace */
102 5
                    $whitespace = $tokens[$index];
103
104
                    /** @var int $presentNewlines */
105 5
                    $presentNewlines = substr_count($whitespace->getContent(), $lineEnding);
106
107 5
                    if ($whitespace->isWhitespace() && $presentNewlines < 2) {
108 5
                        $tokens[$index] = $this->convertWhitespaceToken($whitespace);
109
                    }
110
                }
111
            }
112
        }
113 5
    }
114
115 3
    private function convertWhitespaceToken(Token $whitespace): Token
116
    {
117
        /** @var string $lineEnding */
118 3
        $lineEnding = $this->whitespacesConfig->getLineEnding();
119
120 3
        return new Token([
121 3
            T_WHITESPACE,
122 3
            substr_replace(
123 3
                $whitespace->getContent(),
124 3
                $lineEnding.$lineEnding,
125 3
                strpos($whitespace->getContent(), $lineEnding),
126 3
                1
127
            ),
128
        ]);
129
    }
130
}
131