BlankLineBeforeDocCommentFixer::getDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
final class BlankLineBeforeDocCommentFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
24
{
25 6
    public function __construct()
26
    {
27 6
        parent::__construct();
28
29
        # Fixes psalm: PropertyNotSetInConstructor
30 6
        $this->whitespacesConfig = new WhitespacesFixerConfig();
31 6
    }
32
33
    public function getName()
34
    {
35
        return 'Addiks/blank_line_before_doccomment';
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 doc-comment.',
45
            [
46 1
                new CodeSample(
47 1
                    '<?php
48
49
/** @var string $foo */
50
$foo = "Lorem ipsum";
51
52
/** @var string $bar */
53
$bar = "dolor sit amet";
54
'
55
                ),
56
            ]
57
        );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getPriority()
64
    {
65
        // should run after no_blank_lines_after_phpdoc
66 1
        return -26;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function isCandidate(Tokens $tokens)
73
    {
74 4
        return $tokens->isTokenKindFound(T_DOC_COMMENT);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 4
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
81
    {
82
        /** @var string $lineEnding */
83 4
        $lineEnding = $this->whitespacesConfig->getLineEnding();
84
85 4
        foreach ($tokens as $index => $token) {
86
            /** @var Token $token */
87 4
            if ($token->isGivenKind(T_DOC_COMMENT) && $index > 2) {
88 4
                --$index;
89
90 4
                $prevIndex = (int)$tokens->getPrevMeaningfulToken($index);
91
92 4
                if (!$tokens[$prevIndex]->equals('{')) {
93
                    /** @var Token $whitespace */
94 4
                    $whitespace = $tokens[$index];
95
96
                    /** @var int $presentNewlines */
97 4
                    $presentNewlines = substr_count($whitespace->getContent(), $lineEnding);
98
99 4
                    if ($whitespace->isWhitespace() && $presentNewlines < 2) {
100 4
                        $tokens[$index] = $this->convertWhitespaceToken($whitespace);
101
                    }
102
                }
103
            }
104
        }
105 4
    }
106
107 3
    private function convertWhitespaceToken(Token $whitespace): Token
108
    {
109
        /** @var string $lineEnding */
110 3
        $lineEnding = $this->whitespacesConfig->getLineEnding();
111
112 3
        return new Token([
113 3
            T_WHITESPACE,
114 3
            substr_replace(
115 3
                $whitespace->getContent(),
116 3
                $lineEnding.$lineEnding,
117 3
                strpos($whitespace->getContent(), $lineEnding),
118 3
                1
119
            ),
120
        ]);
121
    }
122
}
123