|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP Mess Detector. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under BSD License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Manuel Pichler <[email protected]> |
|
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
15
|
|
|
* @link http://phpmd.org/ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PHPMD\Rule\Design; |
|
19
|
|
|
|
|
20
|
|
|
use PHPMD\AbstractNode; |
|
21
|
|
|
use PHPMD\AbstractRule; |
|
22
|
|
|
use PHPMD\Rule\FunctionAware; |
|
23
|
|
|
use PHPMD\Rule\MethodAware; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This rule class checks for excessive long function and method parameter lists. |
|
27
|
|
|
*/ |
|
28
|
|
|
class LongParameterList extends AbstractRule implements FunctionAware, MethodAware |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* This method checks the number of arguments for the given function or method |
|
32
|
|
|
* node against a configured threshold. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \PHPMD\AbstractNode $node |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function apply(AbstractNode $node) |
|
38
|
|
|
{ |
|
39
|
8 |
|
$threshold = $this->getIntProperty('minimum'); |
|
40
|
8 |
|
$count = $node->getParameterCount(); |
|
|
|
|
|
|
41
|
8 |
|
if ($count < $threshold) { |
|
42
|
4 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
$exceptions = $this->getExceptionsList(); |
|
46
|
|
|
|
|
47
|
4 |
|
if (in_array($node->getName(), $exceptions, true)) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
$this->addViolation( |
|
52
|
4 |
|
$node, |
|
53
|
|
|
array( |
|
54
|
4 |
|
$node->getType(), |
|
55
|
4 |
|
$node->getName(), |
|
56
|
4 |
|
$count, |
|
57
|
4 |
|
$threshold |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Gets array of exceptions from property |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
4 |
|
private function getExceptionsList() |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
4 |
|
$exceptions = $this->getStringProperty('exceptions'); |
|
71
|
4 |
|
} catch (\OutOfBoundsException $e) { |
|
72
|
4 |
|
$exceptions = ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
return explode(',', $exceptions); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: