forked from object-calisthenics/phpcs-calisthenics-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyTypePerClassLimitSniff.php
More file actions
345 lines (289 loc) · 10.7 KB
/
PropertyTypePerClassLimitSniff.php
File metadata and controls
345 lines (289 loc) · 10.7 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Track the limit of properties of a given set of types per class. Check for untracked property types per class limit too.
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
*/
abstract class ObjectCalisthenics_PropertyTypePerClassLimitSniff implements PHP_CodeSniffer_Sniff
{
/**
* Tracked property type maximum amount.
*
* @var integer
*/
public $trackedMaxCount = 1;
/**
* Tracked property absolute maximum amount.
*
* @var integer
*/
public $trackedAbsoluteMaxCount = 1;
/**
* Untracked property absolute maximum amount.
*
* @var integer
*/
public $untrackedAbsoluteMaxCount = 0;
/**
* Supported list of tokenizers supported by this sniff.
*
* @var array
*/
public $supportedTokenizers = array('PHP');
/**
* Retrieve the list of tracked property types.
*
* @return array
*/
abstract protected function getTrackedPropertyTypeList();
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return integer[]
*/
public function register()
{
return array(
T_CLASS,
T_TRAIT,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$propertyList = $this->getClassPropertyList($phpcsFile, $stackPtr);
// Check for tracked property type absolute amount
if (($error = $this->checkTrackedClassPropertyAmount($propertyList)) !== '') {
$phpcsFile->addError($error, $stackPtr, 'TooManyTrackedProperties');
return;
}
// Check for each tracked property type amount
$errorList = $this->checkTrackedClassPropertyTypeAmount($propertyList);
if ($errorList) {
array_map(
function ($error) use ($phpcsFile, $stackPtr)
{
$phpcsFile->addError($error, $stackPtr, 'TooManyPropertiesOfType');
},
$errorList
);
return;
}
// Check for untracked property type absolute amount
if (($error = $this->checkUntrackedClassPropertyAmount($propertyList)) !== '') {
$phpcsFile->addError($error, $stackPtr, 'TooManyUntrackedProperties');
return;
}
}
/**
* Retrieve the untracked string representation.
*
* @return string
*/
protected function getUntrackedPropertyType()
{
return 'untracked';
}
/**
* Check for tracked class properties amount.
*
* @param array $propertyList
*
* @return string
*/
protected function checkTrackedClassPropertyAmount(array $propertyList)
{
$trackedPropertyList = $this->getTrackedClassPropertyList($propertyList);
$trackedPropertyAmount = count($trackedPropertyList);
if ($trackedPropertyAmount > $this->trackedAbsoluteMaxCount) {
$message = 'You have %d properties declared of "%s" type(s), must be less or equals than %d properties in total';
$error = sprintf($message, $trackedPropertyAmount, implode('", "', $this->getTrackedPropertyTypeList()), $this->trackedAbsoluteMaxCount);
return $error;
}
return '';
}
/**
* Check for tracked class property types amount.
*
* @param array $propertyList
*
* @return array
*/
protected function checkTrackedClassPropertyTypeAmount(array $propertyList)
{
$segregatedPropertyList = $this->getClassPropertiesSegregatedByType($propertyList);
$errorList = array();
foreach ($segregatedPropertyList as $propertyType => $propertyOfTypeList) {
$propertyOfTypeAmount = count($propertyOfTypeList);
if ($propertyOfTypeAmount > $this->trackedMaxCount) {
$message = 'You have %d properties of "%s" type, must be less or equals than %d properties in total';
$error = sprintf($message, $propertyOfTypeAmount, $propertyType, $this->trackedMaxCount);
$errorList[] = $error;
}
}
return $errorList;
}
/**
* Check for untracked class properties amount.
*
* @param array $propertyList
*
* @return string
*/
protected function checkUntrackedClassPropertyAmount(array $propertyList)
{
$untrackedPropertyList = $this->getUntrackedClassPropertyList($propertyList);
$untrackedPropertyAmount = count($untrackedPropertyList);
if ($untrackedPropertyAmount > $this->untrackedAbsoluteMaxCount) {
$message = 'You have %d properties declared of %s type, must be less or equals than %d properties in total';
$error = sprintf($message, $untrackedPropertyAmount, $this->getUntrackedPropertyType(), $this->untrackedAbsoluteMaxCount);
return $error;
}
return '';
}
/**
* Retrieve class properties segregated by data type.
*
* @param array $propertyList
*
* @return array
*/
protected function getClassPropertiesSegregatedByType(array $propertyList)
{
$segregatedPropertyList = array();
foreach ($propertyList as $property) {
if ( ! isset($segregatedPropertyList[$property['type']])) {
$segregatedPropertyList[$property['type']] = array();
}
$segregatedPropertyList[$property['type']][] = $property;
}
return $segregatedPropertyList;
}
// Segregate property types and amount used in class, then loop through and validate.
/**
* Retrieve the list of tracked class properties.
*
* @param array $propertyList List of declared class properties.
*
* @return array
*/
protected function getTrackedClassPropertyList(array $propertyList)
{
$trackedPropertyTypeList = $this->getTrackedPropertyTypeList();
return array_filter(
$propertyList,
function ($property) use ($trackedPropertyTypeList)
{
return in_array($property['type'], $trackedPropertyTypeList);
}
);
}
/**
* Retrieve the list of untracked class properties.
*
* @param array $propertyList List of declared class properties.
*
* @return array
*/
protected function getUntrackedClassPropertyList(array $propertyList)
{
$trackedPropertyTypeList = $this->getTrackedPropertyTypeList();
return array_filter(
$propertyList,
function ($property) use ($trackedPropertyTypeList)
{
return ! in_array($property['type'], $trackedPropertyTypeList);
}
);
}
/**
* Retrieve all declared class properties.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return array
*/
protected function getClassPropertyList(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$pointer = $token['scope_opener'];
$propertyList = array();
while (($pointer = $phpcsFile->findNext(T_VARIABLE, ($pointer + 1), $token['scope_closer'])) !== false) {
$property = $this->createProperty($phpcsFile, $pointer);
if ( ! $property) {
continue;
}
$propertyList[] = $property;
}
return $propertyList;
}
/**
* Create a given declared class property metadata.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return array
*/
private function createProperty(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$property = $tokens[$stackPtr];
// Is it a property or a random variable?
if ( ! (count($property['conditions']) === 1 && in_array(reset($property['conditions']), $this->register()))) {
return null;
}
// If comment couldnt be properly parsed, error was already added.
if (($comment = $this->processMemberComment($phpcsFile, $stackPtr)) === null) {
return null;
}
$varDoc = $comment->getVar();
// If var tag in docblock couldnt be properly read (ie. inheritdoc), error should be added.
if ( ! $varDoc) {
$error = sprintf('Unable to retrieve data type of property "%s"', $property['content']);
$phpcsFile->addError($error, $stackPtr, 'InvalidPropertyType');
return null;
}
return array(
'token' => $property,
'pointer' => $stackPtr,
'type' => $varDoc->getContent(),
'modifiers' => $phpcsFile->getMemberProperties($stackPtr),
);
}
/**
* Process docblock of property and returns its processed information.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return null|\PHP_CodeSniffer_CommentParser_MemberCommentParser
*/
private function processMemberComment(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$commentEnd = $phpcsFile->findPrevious(T_DOC_COMMENT, ($stackPtr - 3));
$commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1;
$commentString = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
// Parse the header comment docblock.
try {
$commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($commentString, $phpcsFile);
$commentParser->parse();
} catch (PHP_CodeSniffer_CommentParser_ParserException $exception) {
$line = ($exception->getLineWithinComment() + $commentStart);
$phpcsFile->addError($exception->getMessage(), $line, 'ErrorParsing');
return null;
}
return $commentParser;
}
}