Completed
Pull Request — php70 (#109)
by
unknown
04:00 queued 01:08
created

DeprecatedPHP4StyleConstructorsSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 27 5
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Koen Eelen <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff.
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Koen Eelen <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff extends PHPCompatibility_Sniff {
20
    public function register()
21
    {
22
        return array(T_CLASS);
23
24
    }//end register()
25
26
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
27
        $tokens = $phpcsFile->getTokens();
28
29
        $class = $tokens[$stackPtr];
30
31
        if(!IsSet($class['scope_closer'])) {
32
            return;
33
        }
34
35
        $scopeCloser = $class['scope_closer'];
36
37
        //get the name of the class
38
        $classNamePos = $phpcsFile->findNext(T_STRING, $stackPtr);
39
        $className = $tokens[$classNamePos]['content'];
40
41
        $nextFunc = $stackPtr;
42
        while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
43
            $fundNamePos = $phpcsFile->findNext(T_STRING, $nextFunc);
44
45
            if ($this->supportsAbove('7.0')) {
46
                if ($tokens[$fundNamePos]['content'] === $className) {
47
                    $phpcsFile->addError('Deprecated PHP4 style constructor are not supported since PHP7',
48
                        $fundNamePos);
0 ignored issues
show
Security Bug introduced by
It seems like $fundNamePos defined by $phpcsFile->findNext(T_STRING, $nextFunc) on line 43 can also be of type false; however, PHP_CodeSniffer_File::addError() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
49
                }
50
            }
51
        }
52
    }
53
}
54