CamelCasePropertyName   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 22 4
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\ClassAware;
23
24
/**
25
 * This rule class detects properties not named in camelCase.
26
 *
27
 * @author     Francis Besset <[email protected]>
28
 * @since      1.1.0
29
 */
30
class CamelCasePropertyName extends AbstractRule implements ClassAware
31
{
32
    /**
33
     * This method checks if a property is not named in camelCase
34
     * and emits a rule violation.
35
     *
36
     * @param \PHPMD\AbstractNode $node
37
     * @return void
38
     */
39 6
    public function apply(AbstractNode $node)
40
    {
41 6
        $allowUnderscore = $this->getBooleanProperty('allow-underscore');
42
43 6
        $pattern = '/^\$[a-zA-Z][a-zA-Z0-9]*$/';
44 6
        if ($allowUnderscore == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
45 2
            $pattern = '/^\$[_]?[a-zA-Z][a-zA-Z0-9]*$/';
46
        }
47
48 6
        foreach ($node->getProperties() as $property) {
0 ignored issues
show
Documentation Bug introduced by
The method getProperties does not exist on object<PHPMD\AbstractNode>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49 6
            $propertyName = $property->getName();
50
51 6
            if (!preg_match($pattern, $propertyName)) {
52 2
                $this->addViolation(
53 2
                    $node,
54
                    array(
55 6
                        $propertyName,
56
                    )
57
                );
58
            }
59
        }
60 6
    }
61
}
62