Completed
Push — php70 ( 6836d5...a375e4 )
by Wim
02:25
created

DeprecatedPHP4StyleConstructorsSniff::process()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 9
eloc 20
c 3
b 0
f 1
nc 15
nop 2
dl 0
loc 36
rs 4.909
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
        $newConstructorFound = false;
43
        $oldConstructorFound = false;
44
        while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
45
            $funcNamePos = $phpcsFile->findNext(T_STRING, $nextFunc);
46
            
47
            if ($tokens[$funcNamePos]['content'] === '__construct') {
48
                $newConstructorFound = true;
49
            }
50
51
            if ($this->supportsAbove('7.0')) {
52
                if ($funcNamePos !== false && $tokens[$funcNamePos]['content'] === $className) {
53
                    $oldConstructorFound = true;
54
                }
55
            }
56
        }
57
        
58
        if ($newConstructorFound === false && $oldConstructorFound === true) {
59
            $phpcsFile->addError('Deprecated PHP4 style constructor are not supported since PHP7', $funcNamePos);
0 ignored issues
show
Bug introduced by
The variable $funcNamePos does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Security Bug introduced by
It seems like $funcNamePos defined by $phpcsFile->findNext(T_STRING, $nextFunc) on line 45 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...
60
        }
61
    }
62
}
63