• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/src/Fixer/ControlStructure/NoSuperfluousElseifFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz RumiƄski <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Fixer\ControlStructure;
16

17
use PhpCsFixer\AbstractNoUselessElseFixer;
18
use PhpCsFixer\FixerDefinition\CodeSample;
19
use PhpCsFixer\FixerDefinition\FixerDefinition;
20
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21
use PhpCsFixer\Preg;
22
use PhpCsFixer\Tokenizer\Token;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
27
 */
28
final class NoSuperfluousElseifFixer extends AbstractNoUselessElseFixer
29
{
30
    public function isCandidate(Tokens $tokens): bool
31
    {
32
        return $tokens->isAnyTokenKindsFound([\T_ELSE, \T_ELSEIF]);
13✔
33
    }
34

35
    public function getDefinition(): FixerDefinitionInterface
36
    {
37
        return new FixerDefinition(
3✔
38
            'Replaces superfluous `elseif` with `if`.',
3✔
39
            [
3✔
40
                new CodeSample("<?php\nif (\$a) {\n    return 1;\n} elseif (\$b) {\n    return 2;\n}\n"),
3✔
41
            ]
3✔
42
        );
3✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     *
48
     * Must run before SimplifiedIfReturnFixer.
49
     * Must run after NoAlternativeSyntaxFixer.
50
     */
51
    public function getPriority(): int
52
    {
53
        return parent::getPriority();
1✔
54
    }
55

56
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
57
    {
58
        foreach ($tokens as $index => $token) {
13✔
59
            if ($this->isElseif($tokens, $index) && $this->isSuperfluousElse($tokens, $index)) {
13✔
60
                $this->convertElseifToIf($tokens, $index);
7✔
61
            }
62
        }
63
    }
64

65
    private function isElseif(Tokens $tokens, int $index): bool
66
    {
67
        return
13✔
68
            $tokens[$index]->isGivenKind(\T_ELSEIF)
13✔
69
            || ($tokens[$index]->isGivenKind(\T_ELSE) && $tokens[$tokens->getNextMeaningfulToken($index)]->isGivenKind(\T_IF));
13✔
70
    }
71

72
    private function convertElseifToIf(Tokens $tokens, int $index): void
73
    {
74
        if ($tokens[$index]->isGivenKind(\T_ELSE)) {
7✔
75
            $tokens->clearTokenAndMergeSurroundingWhitespace($index);
3✔
76
        } else {
77
            $tokens[$index] = new Token([\T_IF, 'if']);
7✔
78
        }
79

80
        $whitespace = '';
7✔
81

82
        for ($previous = $index - 1; $previous > 0; --$previous) {
7✔
83
            $token = $tokens[$previous];
7✔
84
            if ($token->isWhitespace() && Preg::match('/(\R\N*)$/', $token->getContent(), $matches)) {
7✔
85
                $whitespace = $matches[1];
6✔
86

87
                break;
6✔
88
            }
89
        }
90

91
        if ('' === $whitespace) {
7✔
92
            return;
1✔
93
        }
94

95
        $previousToken = $tokens[$index - 1];
6✔
96

97
        if (!$previousToken->isWhitespace()) {
6✔
98
            $tokens->insertAt($index, new Token([\T_WHITESPACE, $whitespace]));
×
99
        } elseif (!Preg::match('/\R/', $previousToken->getContent())) {
6✔
100
            $tokens[$index - 1] = new Token([\T_WHITESPACE, $whitespace]);
5✔
101
        }
102
    }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc