Completed
Push — feature/changelog-7.1.5 ( 234a48...ac5cd4 )
by Juliette
01:46
created

NewMagicClassConstantSniff::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewMagicClassConstantSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_NewMagicClassConstantSniff.
14
 *
15
 * The special ClassName::class constant is available as of PHP 5.5.0, and allows for
16
 * fully qualified class name resolution at compile.
17
 *
18
 * PHP version 5.5
19
 *
20
 * @category PHP
21
 * @package  PHPCompatibility
22
 * @author   Juliette Reinders Folmer <[email protected]>
23
 */
24
class PHPCompatibility_Sniffs_PHP_NewMagicClassConstantSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
25
{
26
27
    /**
28
     * Returns an array of tokens this test wants to listen for.
29
     *
30
     * @return array
31
     */
32
    public function register()
33
    {
34
        return array(T_STRING);
35
    }
36
37
    /**
38
     * Processes this test, when one of its tokens is encountered.
39
     *
40
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
     * @param int                  $stackPtr  The position of the current token in the
42
     *                                        stack passed in $tokens.
43
     *
44
     * @return void
45
     */
46
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
47
    {
48
        if ($this->supportsBelow('5.4') === false) {
49
            return;
50
        }
51
52
        $tokens = $phpcsFile->getTokens();
53
54
        if (strtolower($tokens[$stackPtr]['content']) !== 'class') {
55
            return;
56
        }
57
58
        $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
59
        if ($prevToken === false || $tokens[$prevToken]['code'] !== T_DOUBLE_COLON) {
60
            return;
61
        }
62
63
        $phpcsFile->addError(
64
            'The magic class constant ClassName::class was not available in PHP 5.4 or earlier',
65
            $stackPtr,
66
            'Found'
67
        );
68
    }
69
}
70