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

sanmai / phpstan-rules / 16255432326

14 Jul 2025 12:54AM UTC coverage: 94.488%. Remained the same
16255432326

Pull #16

github

web-flow
Merge b94d040ef into 0b3535e3f
Pull Request #16: Improve tests, fix bugs

10 of 10 new or added lines in 3 files covered. (100.0%)

7 existing lines in 3 files now uncovered.

120 of 127 relevant lines covered (94.49%)

26.17 hits per line

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

93.75
/src/Rules/NoNestedIfStatementsRule.php
1
<?php
2

3
/**
4
 * Copyright 2025 Alexey Kopytko <alexey@kopytko.com>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

19
declare(strict_types=1);
20

21
namespace Sanmai\PHPStanRules\Rules;
22

23
use PhpParser\Node;
24
use PhpParser\Node\Stmt\If_;
25
use PHPStan\Analyser\Scope;
26
use PHPStan\Rules\Rule;
27
use PHPStan\Rules\RuleErrorBuilder;
28
use Override;
29

30
use function count;
31

32
/**
33
 * @implements Rule<If_>
34
 */
35
final class NoNestedIfStatementsRule implements Rule
36
{
37
    public const ERROR_MESSAGE = 'Nested if statements should be avoided. Consider using guard clauses, combining conditions with &&, or extracting to a method.';
38

39
    #[Override]
40
    public function getNodeType(): string
41
    {
42
        return If_::class;
24✔
43
    }
44

45
    /**
46
     * @param If_ $node
47
     * @return list<\PHPStan\Rules\IdentifierRuleError>
48
     */
49
    #[Override]
50
    public function processNode(Node $node, Scope $scope): array
51
    {
52
        // Skip if this if has elseif branches (more complex control flow)
53
        if ([] !== $node->elseifs) {
24✔
54
            return [];
24✔
55
        }
56

57
        $statements = $node->stmts;
24✔
58
        if (1 !== count($statements)) {
24✔
59
            return [];
24✔
60
        }
61

62
        $onlyStatement = $statements[0];
24✔
63

64
        // Check if the only statement is another if
65
        if (!$onlyStatement instanceof If_) {
24✔
66
            return [];
24✔
67
        }
68

69
        // Check if the nested if has elseif
70
        if ([] !== $onlyStatement->elseifs) {
24✔
UNCOV
71
            return [];
×
72
        }
73

74
        return [
24✔
75
            RuleErrorBuilder::message(self::ERROR_MESSAGE)
24✔
76
                ->identifier('sanmai.noNestedIf')
24✔
77
                ->build(),
24✔
78
        ];
24✔
79
    }
80
}
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