Completed
Push — feature/minor-testfile-syntax-... ( db56ec...142740 )
by Juliette
04:05 queued 02:34
created

DynamicAccessToStaticSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
C process() 0 31 7
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\DynamicAccessToStaticSniff.
4
 *
5
 * PHP version 5.3
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
use PHP_CodeSniffer_Tokens as Tokens;
16
17
/**
18
 * \PHPCompatibility\Sniffs\PHP\DynamicAccessToStaticSniff.
19
 *
20
 * As of PHP 5.3, static properties and methods as well as class constants
21
 * can be accessed using a dynamic (variable) class name.
22
 *
23
 * PHP version 5.3
24
 *
25
 * @category PHP
26
 * @package  PHPCompatibility
27
 * @author   Juliette Reinders Folmer <[email protected]>
28
 */
29
class DynamicAccessToStaticSniff extends Sniff
30
{
31
32
    /**
33
     * Returns an array of tokens this test wants to listen for.
34
     *
35
     * @return array
36
     */
37
    public function register()
38
    {
39
        return array(
40
            T_DOUBLE_COLON,
41
        );
42
    }
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                   $stackPtr  The position of the current token in the
49
     *                                         stack passed in $tokens.
50
     *
51
     * @return void
52
     */
53
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        if ($this->supportsBelow('5.2') === false) {
56
            return;
57
        }
58
59
        $tokens       = $phpcsFile->getTokens();
60
        $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
61
62
        // Disregard `static::` as well. Late static binding is reported by another sniff.
63
        if ($tokens[$prevNonEmpty]['code'] === T_SELF
64
            || $tokens[$prevNonEmpty]['code'] === T_PARENT
65
            || $tokens[$prevNonEmpty]['code'] === T_STATIC
66
        ) {
67
            return;
68
        }
69
70
        if ($tokens[$prevNonEmpty]['code'] === T_STRING) {
71
            $prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true);
72
73
            if ($tokens[$prevPrevNonEmpty]['code'] !== T_OBJECT_OPERATOR) {
74
                return;
75
            }
76
        }
77
78
        $phpcsFile->addError(
79
            'Static class properties and methods, as well as class constants, could not be accessed using a dynamic (variable) classname in PHP 5.2 or earlier.',
80
            $stackPtr,
81
            'Found'
82
        );
83
    }
84
}
85