DevelopmentCodeFragment   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 16 4
A getSuspectImages() 0 13 1
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\Design;
19
20
use PHPMD\AbstractNode;
21
use PHPMD\AbstractRule;
22
use PHPMD\Node\MethodNode;
23
use PHPMD\Rule\FunctionAware;
24
use PHPMD\Rule\MethodAware;
25
26
/**
27
 * This rule class detects possible development code fragments that were left
28
 * into the code.
29
 *
30
 * @link https://github.com/phpmd/phpmd/issues/265
31
 * @since 2.3.0
32
 */
33
class DevelopmentCodeFragment extends AbstractRule implements MethodAware, FunctionAware
34
{
35
    /**
36
     * This method checks if a given function or method contains an eval-expression
37
     * and emits a rule violation when it exists.
38
     *
39
     * @param \PHPMD\AbstractNode $node
40
     * @return void
41
     */
42 8
    public function apply(AbstractNode $node)
43
    {
44 8
        foreach ($node->findChildrenOfType('FunctionPostfix') as $postfix) {
45 5
            $image = strtolower($postfix->getImage());
46 5
            if (false === in_array($image, $this->getSuspectImages())) {
47 1
                continue;
48
            }
49
50 4
            $image = $node->getImage();
51 4
            if ($node instanceof MethodNode) {
52 2
                $image = sprintf('%s::%s', $node->getParentName(), $node->getImage());
53
            }
54
55 4
            $this->addViolation($postfix, array($node->getType(), $image, $postfix->getImage()));
56
        }
57 8
    }
58
59
    /**
60
     * Returns an array with function images that are normally only used during
61
     * development.
62
     *
63
     * @return array
64
     */
65 5
    private function getSuspectImages()
66
    {
67 5
        return array_map(
68 5
            'strtolower',
69 5
            array_map(
70 5
                'trim',
71 5
                explode(
72 5
                    ',',
73 5
                    $this->getStringProperty('unwanted-functions')
74
                )
75
            )
76
        );
77
    }
78
}
79