Completed
Push — master ( ca9312...bb4483 )
by Дмитрий
12:32
created

NestedTernary   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 4
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 14 3
A getRegister() 0 6 1
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Expression;
4
5
use PhpParser\Node\Expr;
6
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
7
use PHPSA\Analyzer\Pass;
8
use PHPSA\Context;
9
10
class NestedTernary implements Pass\AnalyzerPassInterface
11
{
12
    use DefaultMetadataPassTrait;
13
14
    const DESCRIPTION = 'Discourages the use of nested ternaries.';
15
16
    /**
17
     * @param Expr\Ternary $expr
18
     * @param Context $context
19
     * @return bool
20
     */
21
    public function pass(Expr\Ternary $expr, Context $context)
22
    {
23
        if ($expr->if instanceof Expr\Ternary || $expr->else instanceof Expr\Ternary) {
24
            $context->notice(
25
                'nested_ternary',
26
                'Nested ternaries are confusing you should use if instead.',
27
                $expr
28
            );
29
30
            return true;
31
        }
32
33
        return false;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getRegister()
40
    {
41
        return [
42
            Expr\Ternary::class,
43
        ];
44
    }
45
}
46