Completed
Pull Request — master (#70)
by Bill
01:53
created

NullCoalescingSniff   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 10 2
B couldBeNullCoalescing() 0 22 6
1
<?php declare(strict_types = 1);
2
3
namespace Codor\Sniffs\Syntax;
4
5
use PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer_File;
7
8
class NullCoalescingSniff implements PHP_CodeSniffer_Sniff
9
{
10
11
    /**
12
     * Returns the token types that this sniff is interested in.
13
     * @return array
14
     */
15
    public function register(): array
16
    {
17
        return [T_ISSET];
18
    }
19
20
    /**
21
     * Processes the tokens that this sniff is interested in.
22
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
23
     * @param integer              $stackPtr  The position in the stack where
24
     *                                    the token was found.
25
     * @return void
26
     */
27
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
28
    {
29
        $this->phpcsFile = $phpcsFile;
0 ignored issues
show
Bug introduced by
The property phpcsFile does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        $tokens          = $phpcsFile->getTokens();
31
        $index           = $stackPtr;
32
33
        if ($this->couldBeNullCoalescing($tokens, $index)) {
34
            $phpcsFile->addError("Ternery found where Null Coalescing operator will work.", $stackPtr);
35
        }
36
    }
37
38
    /**
39
     * Determines if the current line has a ternary
40
     * operator that could be converted to a
41
     * null coalescing operator.
42
     * @param  array   $tokens Tokens.
43
     * @param  integer $index  Current index.
44
     * @return boolean
45
     */
46
    protected function couldBeNullCoalescing($tokens, $index): bool
47
    {
48
        $questionMarkFound = false;
49
        $semiColinFound = false;
50
51
        while (true) {
52
            if ($tokens[$index]['type'] == 'T_SEMICOLON') {
53
                break;
54
            }
55
56
            if ($tokens[$index]['type'] == 'T_INLINE_THEN') {
57
                $questionMarkFound = true;
58
            }
59
60
            if ($tokens[$index]['type'] == 'T_INLINE_ELSE') {
61
                $semiColinFound = true;
62
            }
63
            $index++;
64
        }
65
66
        return $questionMarkFound && $semiColinFound;
67
    }
68
}
69