Passed
Pull Request — master (#18)
by Andru
03:44
created

LongParameterList::apply()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 3.0032
rs 9.536
c 0
b 0
f 0
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\Rule\FunctionAware;
23
use PHPMD\Rule\MethodAware;
24
25
/**
26
 * This rule class checks for excessive long function and method parameter lists.
27
 */
28
class LongParameterList extends AbstractRule implements FunctionAware, MethodAware
29
{
30
    /**
31
     * This method checks the number of arguments for the given function or method
32
     * node against a configured threshold.
33
     *
34
     * @param \PHPMD\AbstractNode $node
35
     * @return void
36
     */
37 8
    public function apply(AbstractNode $node)
38
    {
39 8
        $threshold = $this->getIntProperty('minimum');
40 8
        $count = $node->getParameterCount();
0 ignored issues
show
Documentation Bug introduced by
The method getParameterCount 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...
41 8
        if ($count < $threshold) {
42 4
            return;
43
        }
44
45 4
        $exceptions = $this->getExceptionsList();
46
47 4
        if (in_array($node->getName(), $exceptions, true)) {
48
            return;
49
        }
50
51 4
        $this->addViolation(
52
            $node,
53
            array(
54 4
                $node->getType(),
55 4
                $node->getName(),
56 4
                $count,
57 4
                $threshold
58
            )
59
        );
60 4
    }
61
62
    /**
63
     * Gets array of exceptions from property
64
     *
65
     * @return array
66
     */
67 4
    private function getExceptionsList()
68
    {
69
        try {
70 4
            $exceptions = $this->getStringProperty('exceptions');
71 4
        } catch (\OutOfBoundsException $e) {
72 4
            $exceptions = '';
73
        }
74
75 4
        return explode(',', $exceptions);
76
    }
77
}
78