Completed
Pull Request — master (#540)
by Wim
03:02 queued 01:30
created

DeprecatedMagicAutoloadSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 22 5
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\DeprecatedMagicAutoloadSniff.
4
 *
5
 * PHP version 7.2
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Wim Godden <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\DeprecatedMagicAutoloadSniff.
18
 *
19
 * @category PHP
20
 * @package  PHPCompatibility
21
 * @author   Wim Godden <[email protected]>
22
 */
23
class DeprecatedMagicAutoloadSniff extends Sniff
24
{
25
    /**
26
     * @var array
27
     */
28
    private $excludeScopes = array(
29
        'T_CLASS'      => true,
30
        'T_ANON_CLASS' => true,
31
        'T_INTERFACE'  => true,
32
        'T_TRAIT'      => true,
33
        'T_NAMESPACE'  => true, // = Only scoped namespaces, non-scoped still needs to be checked in another way.
34
    );
35
    
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(T_FUNCTION);
44
    }//end register()
45
46
    /**
47
     * Processes this test, when one of its tokens is encountered.
48
     *
49
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
     * @param int                   $stackPtr  The position of the current token in the
51
     *                                         stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56
    {
57
        if ($this->supportsAbove('7.2') === false) {
58
            return;
59
        }
60
61
        $funcName = $phpcsFile->getDeclarationName($stackPtr);
62
        
63
        if (strtolower($funcName) !== '__autoload') {
64
            return;
65
        }
66
67
        if ($this->validDirectScope($phpcsFile, $stackPtr, $this->excludeScopes) === true) {
68
            return;
69
        }
70
71
        if ($this->determineNamespace($phpcsFile, $stackPtr) != '') {
72
            return;
73
        }
74
75
        $phpcsFile->addWarning('Use of __autoload() function is deprecated since PHP 7.2', $stackPtr, 'Found');
76
    }//end process()
77
78
}//end class
79