CamelCaseVariableName   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
dl 9
loc 66
rs 10
c 0
b 0
f 0
ccs 17
cts 19
cp 0.8947
wmc 8
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B apply() 0 24 6
A getExceptionsList() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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