This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.php
More file actions
562 lines (516 loc) · 22.7 KB
/
Main.php
File metadata and controls
562 lines (516 loc) · 22.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
/**
* Holds the main initialization and computation functions
*
* @package PHPConsistent
* @author Wim Godden <wim@wimgodden.be>
* @copyright 2009-2014 Wim Godden <wim@wimgodden.be>
* @license https://www.gnu.org/licenses/lgpl.html The LGPL 3 License
* @link http://phpconsistent.cu.be/
* @version 0.2
*
*/
class PHPConsistent_Main
{
const LOG_TO_NULL = 0;
const LOG_TO_FILE = 1;
const LOG_TO_FIREPHP = 2;
const LOG_TO_PHPUNIT = 3;
/**
* Temporary trace file for xDebug
* @var string
*/
protected $_traceFile = '';
/**
* Depth of checks
* @var int
*/
protected $_depth = 10;
/**
* Ignore null values as parameters
* @var boolean
*/
protected $_ignorenull = false;
/**
* Log method to use. See above constants LOG_TO_*
* @var int
*/
protected $_log;
/**
* Log location (like filename)
* @var string
*/
protected $_logLocation;
/**
* Array of filenames not to process calls FROM - will be matched *name*
* @var array
*/
protected $_ignoredFileNames;
/**
* Array of classes to ignore method calls TO - will be matched *name*
* @var array
*/
protected $_ignoredClassNames;
/**
* Array of functions to ignore - will be matched *name*
* @var array
*/
protected $_ignoredFunctions;
/**
* List of failures to report back, if required for logging method
* @var array
*/
protected $_failures = array();
protected $_starttime;
/**
* Initialize PHPConsistent, return PHPConsistent object
*
* @param string $traceFile
* @param number $depth
* @param string $ignorenull
* @param int $log
* @param string $log_location
* @param array $ignoredFileNames
* @param array $ignoredClassNames
* @param array $ignoredFunctions
* @return PHPConsistent_Main
*/
public function __construct($traceFile = null, $depth = 2, $ignorenull = false, $log = self::LOG_TO_NULL, $logLocation = null, $ignoredFileNames = array(), $ignoredClassNames = array(), $ignoredFunctions = array())
{
$this->_traceFile = $traceFile;
$this->_depth = $depth;
$this->_ignorenull = $ignorenull;
$this->_log = $log;
$this->_logLocation = $logLocation;
$this->_ignoredFileNames = $ignoredFileNames;
$this->_ignoredClassNames = $ignoredClassNames;
$this->_ignoredFunctions = $ignoredFunctions;
}
/**
* Start PHPConsistent run
*/
public function start()
{
$this->_starttime = microtime(true);
if (extension_loaded('xdebug') === false) {
return false;
}
if (is_null($this->_traceFile) === true) {
$this->_traceFile = tempnam(sys_get_temp_dir(), 'PHPConsistent_');
}
ini_set('xdebug.auto_trace', 1);
ini_set('xdebug.trace_format', 1);
ini_set('xdebug.collect_return', 1);
ini_set('xdebug.collect_params', 1);
ini_set('xdebug.trace_options', 0);
xdebug_start_trace($this->_traceFile);
if ($this->_log === self::LOG_TO_FIREPHP) {
ob_start();
break;
}
if ($this->_log !== self::LOG_TO_PHPUNIT) {
register_shutdown_function(array($this, 'stop'));
register_shutdown_function(array($this, 'analyze'));
}
return true;
}
/**
* Stop PHPConsistent run
*/
public function stop()
{
if (extension_loaded('xdebug') === false) {
return false;
}
xdebug_stop_trace();
return true;
}
/**
* Analyze PHPConsistent data from trace file
*/
public function analyze()
{
if (!file_exists($this->_traceFile)) {
return false;
}
$this->processFunctionCalls();
unlink($this->_traceFile);
if (file_exists($this->_traceFile . '.xt')) {
unlink($this->_traceFile . '.xt');
}
if (count($this->_failures) > 0) {
return $this->_failures;
} else {
return array();
}
}
/**
* Compares 2 types, including classes
* First type is the actual type, retrieved from the Xdebug trace
* Second type is the type (or types) specified in the docblock of the function/method
*
* @param string $callType Can be 'class ClassName' or just the type itself
* @param string $docblockType The type to compare to
* @return boolean True if matched, false if not
*/
protected function compareTypes($callType, $docblockType)
{
if (trim($callType) == '???') {
return true;
}
/**
* Main loop, comparing for all types specified in docblock
* Multiple types must be separated by a pipe (|)
*/
$docblockTypes = explode('|', $docblockType);
foreach ($docblockTypes as $docblockType) {
$docblockType = trim($docblockType);
if ($docblockType == 'mixed') { // If the allowed type is mixed, it doesn't matter what was sent, everything is correct
return true;
}
preg_match_all('/\S+/', $callType, $callTypes); // Split up by words to get class names (if any)
if ($callTypes[0][0] == 'class') { // If it's a class, we will get its parent classes and interface, since they are allowed types as well
if ($docblockType == 'object') { // For Symfony 2, they seem to do this in a few places in their DI stuff
return true;
}
$className = $callTypes[0][1];
$implements = class_implements($className);
$parents = class_parents($className);
$callTypes = array_merge(array($className), $implements, $parents);
} else {
$callTypes = array($callTypes[0][0]);
}
$foundMatch = false;
foreach ($callTypes as $callType) {
$callTypeFiltered = preg_match('/[a-z]+/', $callType, $callType); // Filter out the number of characters in a string, like string(42)
$callType = $callType[0];
switch ($callType) {
case 'null':
if ($this->_ignorenull === true) {
return true;
}
break;
case $docblockType:
return true;
break;
case 'float':
if ($docblockType == 'double' || $docblockType == 'number') {
return true;
}
break;
case 'double':
if ($docblockType == 'float' || $docblockType == 'number') {
return true;
}
break;
case 'int': // This may seem weird, but we consider that since there's no data loss when passing an int as a float, it's ok
case 'long': // Long doesn't exist in PHP, but it seems Xdebug passes it as long in the trace file, which is why it's here
if ($docblockType == 'int' || $docblockType == 'integer' || $docblockType == 'long' || $docblockType == 'number' || $docblockType == 'float') {
return true;
}
break;
case 'bool':
if ($docblockType == 'boolean') {
return true;
}
break;
}
}
}
return false;
}
/**
* Process the function calls from the Xdebug trace file
*/
protected function processFunctionCalls()
{
$returnStack = array();
if (!file_exists($this->_traceFile . '.xt') || ($handle = fopen($this->_traceFile . '.xt', 'r')) === false) {
return;
}
// See http://xdebug.org/docs/all_settings#trace_format
// Note : classes are specified as 'class <classname>'
while ($dataLine = fgetcsv($handle, null, "\t")) {
if (is_numeric($dataLine[0])) {
$minimumLevel = $dataLine[0];
break;
}
}
while ($dataLine = fgetcsv($handle, null, "\t")) {
// For convenience and readability
$tracedataLevel = $dataLine[0];
if (!is_numeric($tracedataLevel)) { // We've reached the end of the trace file
break;
}
if ($tracedataLevel > $minimumLevel + $this->_depth) { // We don't analyze this deep
continue;
}
$tracedataIsReturn = $dataLine[2];
if (isset($dataLine[5])) {
$tracedataFunctionName = $dataLine[5];
$splitClass = explode('->', $tracedataFunctionName);
if (count($splitClass) > 1) { // It's a method
foreach ($this->_ignoredClassNames as $classname) {
if (strpos($splitClass[0], $classname) !== false) {
continue 2;
}
}
foreach ($this->_ignoredFunctions as $functionname) {
if (strpos($splitClass[1], $functionname) !== false) {
continue 2;
}
}
} else {
foreach ($this->_ignoredFunctions as $functionname) {
if (strpos($splitClass[0], $functionname) !== false) {
continue 2;
}
}
}
} else {
$tracedataFunctionName = '';
}
if (isset($dataLine[6])) {
$tracedataUserDefined = $dataLine[6];
} else {
$tracedataUserDefined = '';
}
if (isset($dataLine[7])) {
$tracedataIncludeFilename = $dataLine[7];
} else {
$tracedataIncludeFilename = '';
}
if (isset($dataLine[8])) {
$tracedataFilename = $dataLine[8];
foreach ($this->_ignoredFileNames as $filename) {
if (strpos($tracedataFilename, $filename) !== false) {
continue 2;
}
}
} else {
$tracedataFilename = '';
}
if (isset($dataLine[9])) {
$tracedataLinenumber = $dataLine[9];
} else {
$tracedataLinenumber = '';
}
if ($tracedataIsReturn == 0) { // It's a function/method call
if (strpos($tracedataFunctionName, '{closure:') !== false) {
continue;
}
preg_match(
'/(?P<classOrFunction>\w+){0,1}(?:\:\:|->){0,1}(?P<method>\w+){0,1}/',
$tracedataFunctionName,
$functionCall
);
$docBlock = false; // getDocComment will return false if no docblock is present
if (!isset($functionCall['method']) && function_exists($functionCall['classOrFunction'])) { // It's a function
$calledFunction = $functionCall['classOrFunction'];
$func = new ReflectionFunction($functionCall['classOrFunction']);
$docBlock = $func->getDocComment();
$numberOfParameters = $func->getNumberOfParameters();
$functionParameters = $func->getParameters();
} elseif (isset($functionCall['method']) && method_exists($functionCall['classOrFunction'], $functionCall['method'])) { // It's a method
$calledFunction = $functionCall['classOrFunction'] . '->' . $functionCall['method'];
$method = new ReflectionMethod($functionCall['classOrFunction'], $functionCall['method']);
$docBlock = $method->getDocComment();
$numberOfParameters = $method->getNumberOfParameters();
$functionParameters = $method->getParameters();
} // else it's an internal function, which we're unable to verify at this point
if ($docBlock !== false) {
$foundReturn = false;
preg_match_all('/\s*\*\s*@(?P<tag>phpconsistent-ignore)/', $docBlock, $noTypeCheck, PREG_SET_ORDER); // Check if @phpconsistent-ignore was in docblock
if (count($noTypeCheck) == 0) {
// Main docblock parameter and return type loop
preg_match_all('/\s*\*\s*@(?P<tag>param|return)\s+(?P<type>\S+)\s+(?P<paramName>\$?\w+)?/', $docBlock, $docBlockVars, PREG_SET_ORDER);
$documentedParameters = 0;
$docblockParams = array();
for ($cntDocBlockTag = 0; $cntDocBlockTag < count($docBlockVars); $cntDocBlockTag++) {
if ($docBlockVars[$cntDocBlockTag]['tag'] == "param") {
$documentedParameters++;
if (isset($docBlockVars[$cntDocBlockTag]['paramName'])) {
$docblockParams[] = $docBlockVars[$cntDocBlockTag]['paramName'];
} else {
$docblockParams[] = '';
}
if (isset($dataLine[11 + $cntDocBlockTag])) { // Was the call made with this parameter ?
$foundMatch = $this->compareTypes($dataLine[11 + $cntDocBlockTag], $docBlockVars[$cntDocBlockTag]['type']);
if ($foundMatch === false) {
$this->addParamTypeFailure(
$tracedataFilename,
$tracedataLinenumber,
$calledFunction,
($cntDocBlockTag + 1),
$docBlockVars[$cntDocBlockTag]['paramName'],
$docBlockVars[$cntDocBlockTag][2],
$dataLine[11 + $cntDocBlockTag]
);
}
}
} else { // Put the expected return type on the stack for later use
$returnStack[] = array(
'fileName' => $tracedataFilename,
'fileLine' => $tracedataLinenumber,
'calledFunction' => $calledFunction,
'parameterNumber' => null,
'parameterName' => null,
'expectedType' => $docBlockVars[$cntDocBlockTag]['type'],
'calledType' => null,
'noTypeCheck' => 0
);
$foundReturn = true;
}
}
}
if ($numberOfParameters != $documentedParameters) {
$this->addParamCountMismatchFailure($tracedataFilename, $tracedataLinenumber, $calledFunction, $documentedParameters, $numberOfParameters);
}
for ($cntDocBlockParam = 0; $cntDocBlockParam < count($docblockParams); $cntDocBlockParam++) {
if (!isset($functionParameters[$cntDocBlockParam])) {
$this->addParamMissingFailure($tracedataFilename, $tracedataLinenumber, $calledFunction, $cntDocBlockParam, $docblockParams[$cntDocBlockParam]);
} elseif ($docblockParams[$cntDocBlockParam] != '$' . $functionParameters[$cntDocBlockParam]->getName()) {
$this->addParamNameMismatchFailure($tracedataFilename, $tracedataLinenumber, $calledFunction, $cntDocBlockParam, $docblockParams[$cntDocBlockParam], '$' . $functionParameters[$cntDocBlockParam]->getName());
}
}
if ($foundReturn === false) { // Put this function/method call on the stack
$returnStack[] = array(
'fileName' => $tracedataFilename,
'fileLine' => $tracedataLinenumber,
'calledFunction' => $calledFunction,
'parameterNumber' => null,
'parameterName' => null,
'expectedType' => null,
'calledType' => null,
'noTypeCheck' => count($noTypeCheck)
);
}
} else {
$returnStack[] = array(
'fileName' => $tracedataFilename,
'fileLine' => $tracedataLinenumber,
'calledFunction' => $calledFunction,
'parameterNumber' => null,
'parameterName' => null,
'expectedType' => null,
'calledType' => null,
'noTypeCheck' => 1
);
}
} else { // It's a return
// If this isn't set, we have a version of Xdebug that hasn't been patched for Xdebug bug #416
// Requires Xdebug with code after https://github.com/xdebug/xdebug/pull/79
if ($tracedataFunctionName != '') {
$returnPop = array_pop($returnStack);
if ($returnPop['noTypeCheck'] == 0 && $returnPop['expectedType'] != '') {
preg_match('/(?P<type>\w+)\s?(?P<class>\w+)?/', $tracedataFunctionName, $returnTypes);
if (preg_match('/^\'.*/', $tracedataFunctionName) > 0) { // Strings are not extracted with the above regular expression, but ALWAYS start with a quote in Xdebug trace output
$returnPop['calledType'] = 'string';
} elseif ($returnTypes['type'] == 'NULL') {
$returnPop['calledType'] = 'null';
} elseif ($returnTypes['type'] == 'class') {
$returnPop['calledType'] = 'class ' . $returnTypes['class'];
} elseif ($returnTypes['type'] == 'TRUE' || $returnTypes['type'] == 'FALSE') {
$returnPop['calledType'] = 'bool';
} elseif ($returnTypes['type'] == 'array') {
$returnPop['calledType'] = 'array';
} elseif (is_numeric($returnTypes['type'])) {
$returnPop['calledType'] = 'int';
} else {
$returnPop['calledType'] = 'unknown';
}
if (!$this->compareTypes($returnType, $returnPop['expectedType'])) {
$this->addParamTypeFailure($returnPop['fileName'], $returnPop['fileLine'], $returnPop['calledFunction'], null, null, $returnPop['expectedType'], $returnPop['calledType']);
}
}
}
}
}
fclose($handle);
}
/**
* Add a failure about incorrect parameter type
* @param string $fileName
* @param int $fileLine
* @param string $calledFunction
* @param int $parameterNumber
* @param string $parameterName
* @param string $expectedType
* @param string $calledType
*/
protected function addParamTypeFailure($fileName, $fileLine, $calledFunction, $parameterNumber, $parameterName, $expectedType, $calledType)
{
$data = 'Invalid type calling ' . $calledFunction . ' : parameter ' . $parameterNumber . ' (' . $parameterName . ') should be of type ' . $expectedType . ' but got ' . $calledType . ' instead';
$this->reportFailure($fileName, $fileLine, $data);
}
/**
* Add a failure about a missing parameter in the function/method declaration
* @param string $fileName
* @param int $fileLine
* @param string $calledFunction
* @param int $parameterNumber
* @param string $parameterName
*/
protected function addParamMissingFailure($fileName, $fileLine, $calledFunction, $parameterNumber, $parameterName)
{
$data = 'Missing parameter calling ' . $calledFunction . ' : parameter ' . $parameterNumber . ' (' . $parameterName . ') does not appear in the function/method declaration';
$this->reportFailure($fileName, $fileLine, $data);
}
/**
* Add a failure about mismatching parameter names
* @param string $fileName
* @param int $fileLine
* @param stirng $calledFunction
* @param int $parameterNumber
* @param string $expectedName
* @param string $calledName
*/
protected function addParamNameMismatchFailure($fileName, $fileLine, $calledFunction, $parameterNumber, $expectedName, $calledName)
{
$data = 'Parameter names in function definition and docblock don\'t match when calling ' . $calledFunction . ' : parameter ' . $parameterNumber . ' (' . $calledName . ') should be called ' . $expectedName . ' according to docblock';
$this->reportFailure($fileName, $fileLine, $data);
}
/**
* Add a failure about mismatching parameter count
* @param string $fileName
* @param int $fileLine
* @param string $calledFunction
* @param int $expectedCount
* @param int $actualCount
*/
protected function addParamCountMismatchFailure($fileName, $fileLine, $calledFunction, $expectedCount, $actualCount)
{
$data = 'Parameter count in function definition and docblock don\'t match when calling ' . $calledFunction . ' : function has ' . $actualCount . ' but should be ' . $expectedCount . ' according to docblock';
$this->reportFailure($fileName, $fileLine, $data);
}
/**
* Output to the chosen reporting system
* @param string $fileName
* @param int $fileLine
* @param string $data
*/
protected function reportFailure($fileName, $fileLine, $data)
{
switch ($this->_log) {
case self::LOG_TO_FILE:
file_put_contents(
$this->_logLocation,
$data . ' - in ' . $fileName . ' (line ' . $fileLine . ')',
FILE_APPEND
);
break;
case self::LOG_TO_FIREPHP:
require_once 'FirePHPCore/FirePHP.class.php';
$firephp = FirePHP::getInstance(true);
$firephp->warn($fileName . ' (line ' . $fileLine . ')', $data);
break;
case self::LOG_TO_PHPUNIT;
$this->_failures[] = array(
'fileName' => $fileName,
'fileLine' => $fileLine,
'data' => $data
);
break;
}
}
}