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 variables not named in camelCase. |
27
|
|
|
* |
28
|
|
|
* @author Francis Besset <[email protected]> |
29
|
|
|
* @since 1.1.0 |
30
|
|
|
*/ |
31
|
|
|
class SnakeCaseVariableName extends AbstractRule implements MethodAware, FunctionAware |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $exceptions = array( |
37
|
|
|
'$php_errormsg', |
38
|
|
|
'$http_response_header', |
39
|
|
|
'$GLOBALS', |
40
|
|
|
'$_SERVER', |
41
|
|
|
'$_GET', |
42
|
|
|
'$_POST', |
43
|
|
|
'$_FILES', |
44
|
|
|
'$_COOKIE', |
45
|
|
|
'$_SESSION', |
46
|
|
|
'$_REQUEST', |
47
|
|
|
'$_ENV', |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This method checks if a variable is not named in camelCase |
52
|
|
|
* and emits a rule violation. |
53
|
|
|
* |
54
|
|
|
* @param \PHPMD\AbstractNode $node |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function apply(AbstractNode $node) |
58
|
|
|
{ |
59
|
|
View Code Duplication |
foreach ($node->findChildrenOfType('Variable') as $variable) { |
|
|
|
|
60
|
|
|
$image = $variable->getImage(); |
61
|
|
|
|
62
|
|
|
if (in_array($image, $this->exceptions)) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (preg_match('/^\$[_a-z][a-z_0-9]*$/', $image)) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($variable->getParent()->isInstanceOf('PropertyPostfix')) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->addViolation($node, array($image)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.