-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathInstantiateNewClassesSniff.php
157 lines (137 loc) · 3.15 KB
/
InstantiateNewClassesSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Joomla! Coding Standard
*
* @package Joomla.CodingStandard
* @copyright Copyright (C) 2015 Open Source Matters, Inc. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
/**
* Ensures that new classes are instantiated without brackets if they do not have any parameters.
*
* @since 1.0
*/
class Joomla_Sniffs_Classes_InstantiateNewClassesSniff implements PHP_CodeSniffer_Sniff
{
/**
* If true, short Array Syntax for php 5.4+ will be allow for Instantiating New Classes if they are found in the code.
* this should be removed when all Joomla projects no longer need to support php 5.3
*
* @var boolean
*/
public $shortArraySyntax = false;
/**
* Registers the token types that this sniff wishes to listen to.
*
* @return array
*/
public function register()
{
return array(T_NEW);
}
/**
* Process the tokens that this sniff is listening for.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param integer $stackPtr The position in the stack where the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$running = true;
$valid = false;
$started = false;
$cnt = $stackPtr + 1;
do
{
if (false === isset($tokens[$cnt]))
{
$running = false;
}
else
{
switch ($tokens[$cnt]['code'])
{
case T_SEMICOLON :
case T_COMMA :
$valid = true;
$running = false;
break;
case T_OPEN_PARENTHESIS :
$started = true;
break;
case T_VARIABLE :
case T_STRING :
case T_LNUMBER :
case T_CONSTANT_ENCAPSED_STRING :
case T_DOUBLE_QUOTED_STRING :
case T_ARRAY :
case T_TRUE :
case T_FALSE :
case T_NULL :
if ($started === true)
{
$valid = true;
$running = false;
}
break;
case T_CLOSE_PARENTHESIS :
if ($started === false)
{
$valid = true;
}
$running = false;
break;
case T_WHITESPACE :
break;
case T_OPEN_SHORT_ARRAY :
if ($this->shortArraySyntax === true)
{
if ($started === true)
{
$valid = true;
$running = false;
}
}
break;
}
$cnt++;
}
}
while ($running === true);
if ($valid === false)
{
$error = 'Instanciating new class without parameters does not require brackets.';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NewClass');
if ($fix === true)
{
$classNameEnd = $phpcsFile->findNext(
array(
T_VARIABLE,
T_WHITESPACE,
T_NS_SEPARATOR,
T_STRING,
T_SELF,
),
($stackPtr + 1),
null,
true,
null,
true
);
$phpcsFile->fixer->beginChangeset();
if ($tokens[($stackPtr + 3)]['code'] === T_WHITESPACE)
{
$phpcsFile->fixer->replaceToken(($stackPtr + 3), '');
}
for ($i = $classNameEnd; $i < $cnt; $i++)
{
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
}
}