Php56Features::enterNode()   C
last analyzed

Complexity

Conditions 26
Paths 20

Size

Total Lines 62
Code Lines 39

Duplication

Lines 16
Ratio 25.81 %

Code Coverage

Tests 59
CRAP Score 26

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 16
loc 62
ccs 59
cts 59
cp 1
rs 5.97
cc 26
eloc 39
nc 20
nop 1
crap 26

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Php56Features.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained through one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
namespace Pvra\Analysers;
18
19
20
use PhpParser\Node;
21
use Pvra\AnalyserAwareInterface;
22
use Pvra\Result\Reason;
23
24
/**
25
 * Class Php56Features
26
 *
27
 * Supported syntax detection:
28
 * * Variadic arguments
29
 * * Argument unpacking
30
 * * Constant scalar expressions
31
 * * The `**`(pow) operator
32
 * * Function and Constant importing via `use`
33
 *
34
 * @package Pvra\Analysers
35
 */
36
class Php56Features extends LanguageFeatureAnalyser implements AnalyserAwareInterface
37
{
38
    /**
39
     * @inheritdoc
40
     */
41 40
    public function enterNode(Node $node)
42
    {
43 40
        if ($this->mode & self::MODE_ADDITION) {
44 19
            if ($node instanceof Node\Stmt\Function_
45 38
                || $node instanceof Node\Stmt\ClassMethod
46 38
                || $node instanceof Node\Expr\Closure
47 19
            ) {
48 26
                if (!empty($node->params)) {
49 26
                    foreach ($node->params as $param) {
50 26
                        if ($param->variadic) {
51 18
                            $this->getResult()->addRequirement(
52 18
                                Reason::VARIADIC_ARGUMENT,
53 22
                                $param->getLine()
54 9
                            );
55 9
                        }
56 13
                    }
57 13
                }
58 13 View Code Duplication
            } elseif ($node instanceof Node\Expr\FuncCall
59 38
                || $node instanceof Node\Expr\MethodCall
60 38
                || $node instanceof Node\Expr\StaticCall
61 19
            ) {
62 26
                if (!empty($node->args)) {
63 26
                    foreach ($node->args as $arg) {
64 26
                        if ($arg->unpack === true) {
65 4
                            $this->getResult()->addRequirement(
66 4
                                Reason::ARGUMENT_UNPACKING,
67 15
                                $arg->getLine()
68 2
                            );
69 2
                        }
70 13
                    }
71 13
                }
72
73 38
            } elseif ($node instanceof Node\Stmt\Const_ || $node instanceof Node\Stmt\ClassConst) {
74 8
                foreach ($node->consts as $const) {
75 8
                    if (!($const->value instanceof Node\Scalar)
76 8
                        && !($const->value instanceof Node\Expr\ClassConstFetch || $const->value instanceof Node\Expr\ConstFetch)
77 4
                    ) {
78 6
                        $this->getResult()->addRequirement(
79 6
                            Reason::CONSTANT_SCALAR_EXPRESSION,
80 7
                            $const->getLine()
81 3
                        );
82 3
                    }
83 4
                }
84 37
            } elseif ($node instanceof Node\Expr\AssignOp\Pow || $node instanceof Node\Expr\BinaryOp\Pow) {
85 4
                $this->getResult()->addRequirement(
86 4
                    Reason::POW_OPERATOR,
87 4
                    $node->getLine()
88 2
                );
89 18
            } elseif ($node instanceof Node\Stmt\Use_) {
90 6
                $cat = null;
91 6
                if ($node->type === Node\Stmt\Use_::TYPE_CONSTANT) {
92 4
                    $cat = Reason::CONSTANT_IMPORT_USE;
93 6
                } elseif ($node->type === Node\Stmt\Use_::TYPE_FUNCTION) {
94 4
                    $cat = Reason::FUNCTION_IMPORT_USE;
95 2
                }
96
97 6
                if ($cat !== null) {
98 4
                    $this->getResult()->addRequirement($cat, $node->getLine());
99 2
                }
100 3
            }
101 19
        }
102 40
    }
103
}
104