Failed Conditions
Push — test-scrutinizer-coverage ( 1fb662...215aeb )
by Juliette
03:50
created

NewAnonymousClassesSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 62
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 4
A register() 0 8 2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewAnonymousClasses.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_NewAnonymousClasses.
14
 *
15
 * Anonymous classes are supported in PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category PHP
20
 * @package  PHPCompatibility
21
 * @author   Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_NewAnonymousClassesSniff 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...
24
{
25
26
    /**
27
     * Tokens which in various PHP versions indicate the `class` keyword.
28
     *
29
     * The dedicated anonymous class token is added from the `register()`
30
     * method if the token is available.
31
     *
32
     * @var array
33
     */
34
    private $indicators = array(
35
        T_CLASS => T_CLASS,
36
    );
37
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43 3
    public function register()
44
    {
45 3
        if (defined('T_ANON_CLASS')) {
46 3
            $this->indicators[T_ANON_CLASS] = T_ANON_CLASS;
47 3
        }
48
49 3
        return array(T_NEW);
50
    }//end register()
51
52
53
    /**
54
     * Processes this test, when one of its tokens is encountered.
55
     *
56
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
57
     * @param int                  $stackPtr  The position of the current token in the
58
     *                                        stack passed in $tokens.
59
     *
60
     * @return void
61
     */
62 2
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
63
    {
64 2
        if ($this->supportsBelow('5.6') === false) {
65 1
            return;
66
        }
67
68 1
        $tokens       = $phpcsFile->getTokens();
69 1
        $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
70 1
        if ($nextNonEmpty === false || isset($this->indicators[$tokens[$nextNonEmpty]['code']]) === false) {
71 1
            return;
72
        }
73
74
        // Still here ? In that case, it is an anonymous class.
75 1
        $phpcsFile->addError(
76 1
            'Anonymous classes are not supported in PHP 5.6 or earlier',
77 1
            $stackPtr,
78
            'Found'
79 1
        );
80
81 1
    }//end process()
82
83
84
}//end class
85