Completed
Pull Request — master (#167)
by Juliette
02:58
created

DeprecatedPHP4StyleConstructorsSniff::process()   C

Complexity

Conditions 13
Paths 16

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 56
rs 6.6843
cc 13
eloc 28
nc 16
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 {
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...
20
21
    public function register()
22
    {
23
        return array(T_CLASS);
24
25
    }//end register()
26
27
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
28
    {
29
        if ($this->supportsAbove('7.0') === false) {
30
            return;
31
        }
32
33
        if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') {
34
            /*
35
             * Namespaced methods with the same name as the class are treated as
36
             * regular methods, so we can bow out if we're in a namespace.
37
             *
38
             * Note: the exception to this is PHP 5.3.0-5.3.2. This is currently
39
             * not dealt with.
40
             */
41
            return;
42
        }
43
44
        $tokens = $phpcsFile->getTokens();
45
46
        $class = $tokens[$stackPtr];
47
48
        if(!IsSet($class['scope_closer'])) {
49
            return;
50
        }
51
52
        $scopeCloser = $class['scope_closer'];
53
        $className   = $phpcsFile->getDeclarationName($stackPtr);
54
55
        if (empty($className) || is_string($className) === false) {
56
            return;
57
        }
58
59
        $nextFunc = $stackPtr;
60
        $newConstructorFound = false;
61
        $oldConstructorFound = false;
62
        $oldConstructorPos   = false;
63
        while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
64
            $funcName = $phpcsFile->getDeclarationName($nextFunc);
65
            if (empty($funcName) || is_string($funcName) === false) {
66
                continue;
67
            }
68
69
            if ($funcName === '__construct') {
70
                $newConstructorFound = true;
71
            }
72
73
            if ($funcName === $className) {
74
                $oldConstructorFound = true;
75
                $oldConstructorPos   = $phpcsFile->findNext(T_STRING, $nextFunc);
76
            }
77
        }
78
79
        if ($newConstructorFound === false && $oldConstructorFound === true) {
80
            $phpcsFile->addError('Deprecated PHP4 style constructor are not supported since PHP7', $oldConstructorPos);
0 ignored issues
show
Security Bug introduced by
It seems like $oldConstructorPos 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?
Loading history...
81
        }
82
    }
83
}
84