ConstructorNameSniff   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C processTokenWithinScope() 0 70 12
A processTokenOutsideScope() 0 4 1
1
<?php
2
/**
3
 * CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Thomas Ernest <[email protected]>
10
 * @copyright 2011 Thomas Ernest
11
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
namespace CodeIgniter\Sniffs\NamingConventions;
16
17
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
18
use PHP_CodeSniffer\Files\File;
19
20
/**
21
 * CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
22
 *
23
 * Favor PHP 4 constructor syntax, which uses "function ClassName()".
24
 * Avoid PHP 5 constructor syntax, which uses "function __construct()".
25
 *
26
 * @todo Try to avoid overly long and verbose names.
27
 *
28
 * @category  PHP
29
 * @package   PHP_CodeSniffer
30
 * @author    Thomas Ernest <[email protected]>
31
 * @copyright 2010 Thomas Ernest
32
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
33
 * @link      http://pear.php.net/package/PHP_CodeSniffer
34
 */
35
class ConstructorNameSniff extends AbstractScopeSniff
36
{
37
38
39
    public $php5Constructors = '1';
40
41
42
    /**
43
     * Constructs the test with the tokens it wishes to listen for.
44
     *
45
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
50
51
    }//end __construct()
52
53
54
    /**
55
     * Processes this test when one of its tokens is encountered.
56
     *
57
     * @param File $phpcsFile The current file being scanned.
58
     * @param int                  $stackPtr  The position of the current token
59
     *                                        in the stack passed in $tokens.
60
     * @param int                  $currScope A pointer to the start of the scope.
61
     *
62
     * @return void
63
     */
64
    protected function processTokenWithinScope(
65
        File $phpcsFile,
66
        $stackPtr,
67
        $currScope
68
    ) {
69
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
70
        $className  = $phpcsFile->getDeclarationName($currScope);
71
72
	$isPhp4Constructor = strcasecmp($methodName, $className) === 0;
73
	$isPhp5Constructor = strcasecmp($methodName, '__construct') === 0;
74
        if ($this->php5Constructors != '0') {
75
            if ($isPhp4Constructor) {
76
                $error = "PHP4 style constructors are not allowed; use \"__construct\" instead";
77
                $phpcsFile->addError($error, $stackPtr);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
78
            }
79
        } else {
80
            if ($isPhp5Constructor) {
81
                $error = "PHP5 style constructors are not allowed; use \"$className\" instead";
82
                $phpcsFile->addError($error, $stackPtr);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
83
            }
84
        }
85
        if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) {
86
            return;
87
        }
88
89
        $tokens = $phpcsFile->getTokens();
90
91
        $parentClassName = $phpcsFile->findExtendedClassName($currScope);
92
        $wrongConstructor = '';
93
        // prepares the error message and wrong constructor
94
        if ($this->php5Constructors != '0') {
95
            $error = 'PHP4 style calls to parent constructors are not allowed.';
96
            $error = "$error Please use \"parent::__construct\" instead.";
97
            if (false !== $parentClassName) {
98
                $wrongConstructor = $parentClassName;
99
            }
100
            // Else $wrongConstructor will be empty
101
            // and the test expression will always be false.
102
            // It doesn't check that no parent method should be called
103
            // when no parent class is defined.
104
        } else {
105
            $error = 'PHP5 style calls to parent constructors are not allowed.';
106
            if (false !== $parentClassName) {
107
                $error = "$error Please use \"parent::$parentClassName\" instead.";
108
            }
109
            $wrongConstructor = '__construct';
110
        }
111
112
        // looks for the use of a wrong constructor.
113
        $endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
114
        $doubleColonIndex = $phpcsFile->findNext(
115
            array(T_DOUBLE_COLON),
116
            $stackPtr,
117
            $endFunctionIndex
118
        );
119
        while ($doubleColonIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $doubleColonIndex of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
120
            if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
121
                && $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor
122
            ) {
123
                $phpcsFile->addError($error, ($doubleColonIndex + 1));
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
124
            }
125
126
            $doubleColonIndex = $phpcsFile->findNext(
127
                array(T_DOUBLE_COLON),
128
                $doubleColonIndex + 1,
129
                $endFunctionIndex
130
            );
131
        }
132
133
    }//end processTokenWithinScope()
134
135
	protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
136
	{
137
		// TODO: Implement processTokenOutsideScope() method.
138
	}
139
140
}//end class
141
142
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
143