|
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\Controversial; |
|
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 detects parameters not named in camelCase. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Francis Besset <[email protected]> |
|
29
|
|
|
* @since 1.1.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
class CamelCaseParameterName extends AbstractRule implements MethodAware, FunctionAware |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* This method checks if a parameter is not named in camelCase |
|
35
|
|
|
* and emits a rule violation. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \PHPMD\AbstractNode $node |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function apply(AbstractNode $node) |
|
41
|
|
|
{ |
|
42
|
|
|
foreach ($node->getParameters() as $parameter) { |
|
|
|
|
|
|
43
|
|
|
$exceptions = $this->getExceptionsList(); |
|
44
|
|
|
if (in_array(substr($parameter->getName(), 1), $exceptions)) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
if (!preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $parameter->getName())) { |
|
48
|
|
|
$this->addViolation( |
|
49
|
|
|
$node, |
|
50
|
|
|
array( |
|
51
|
|
|
$parameter->getName(), |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
private function getExceptionsList() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
$exceptions = $this->getStringProperty('exceptions'); |
|
62
|
|
|
} catch (\OutOfBoundsException $e) { |
|
63
|
|
|
$exceptions = ''; |
|
64
|
|
|
} |
|
65
|
|
|
return explode(',', $exceptions); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
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: