CamelCaseVariableName::getExceptionsList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
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 CamelCaseVariableName 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 4
    public function apply(AbstractNode $node)
58
    {
59 4
        foreach ($node->findChildrenOfType('Variable') as $variable) {
60 4
            $image = $variable->getImage();
61
62 4
            if (in_array($image, $this->exceptions)) {
63
                continue;
64
            }
65
66 4
            if (preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $image)) {
67 1
                continue;
68
            }
69
70 3
            if ($variable->getParent()->isInstanceOf('PropertyPostfix')) {
71 1
                continue;
72
            }
73 2
            $exceptions = $this->getExceptionsList();
74 2
            if (in_array(substr($image, 1), $exceptions)) {
75
                continue;
76
            }
77
78 2
            $this->addViolation($node, array($image));
79
        }
80 4
    }
81
    
82
    /**
83
     * Gets array of exceptions from property
84
     *
85
     * @return array
86
     */
87 2 View Code Duplication
    private function getExceptionsList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
88
    {
89
        try {
90 2
            $exceptions = $this->getStringProperty('exceptions');
91 2
        } catch (\OutOfBoundsException $e) {
92 2
            $exceptions = '';
93
        }
94 2
        return explode(',', $exceptions);
95
    }
96
}
97