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

keradus / PHP-CS-Fixer / 16253811376

13 Jul 2025 09:50PM UTC coverage: 94.794% (+0.001%) from 94.793%
16253811376

push

github

web-flow
Merge branch 'master' into sf8

2272 of 2348 new or added lines in 338 files covered. (96.76%)

7 existing lines in 5 files now uncovered.

28259 of 29811 relevant lines covered (94.79%)

45.39 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
final class NoSuperfluousElseifFixer extends AbstractNoUselessElseFixer
26
{
27
    public function isCandidate(Tokens $tokens): bool
28
    {
29
        return $tokens->isAnyTokenKindsFound([\T_ELSE, \T_ELSEIF]);
13✔
30
    }
31

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

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

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

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

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

77
        $whitespace = '';
7✔
78

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

84
                break;
6✔
85
            }
86
        }
87

88
        if ('' === $whitespace) {
7✔
89
            return;
1✔
90
        }
91

92
        $previousToken = $tokens[$index - 1];
6✔
93

94
        if (!$previousToken->isWhitespace()) {
6✔
NEW
95
            $tokens->insertAt($index, new Token([\T_WHITESPACE, $whitespace]));
×
96
        } elseif (!Preg::match('/\R/', $previousToken->getContent())) {
6✔
97
            $tokens[$index - 1] = new Token([\T_WHITESPACE, $whitespace]);
5✔
98
        }
99
    }
100
}
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