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

DeprecatedMagicAutoloadSniff::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 11
nc 5
nop 2
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
     * Scopes to look for when testing using validDirectScope
27
     *
28
     * @var array
29
     */
30
    private $checkForScopes = array(
31
        'T_CLASS'      => true,
32
        'T_ANON_CLASS' => true,
33
        'T_INTERFACE'  => true,
34
        'T_TRAIT'      => true,
35
        'T_NAMESPACE'  => true,
36
    );
37
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43
    public function register()
44
    {
45
        return array(T_FUNCTION);
46
    }//end register()
47
48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
     * @param int                   $stackPtr  The position of the current token in the
53
     *                                         stack passed in $tokens.
54
     *
55
     * @return void
56
     */
57
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
    {
59
        if ($this->supportsAbove('7.2') === false) {
60
            return;
61
        }
62
63
        $funcName = $phpcsFile->getDeclarationName($stackPtr);
64
        
65
        if (strtolower($funcName) !== '__autoload') {
66
            return;
67
        }
68
69
        if ($this->validDirectScope($phpcsFile, $stackPtr, $this->checkForScopes) === true) {
70
            return;
71
        }
72
73
        if ($this->determineNamespace($phpcsFile, $stackPtr) != '') {
74
            return;
75
        }
76
77
        $phpcsFile->addWarning('Use of __autoload() function is deprecated since PHP 7.2', $stackPtr, 'Found');
78
    }//end process()
79
80
}//end class
81