Php56Features   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 68
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 26
c 3
b 0
f 0
lcom 1
cbo 3
dl 16
loc 68
ccs 59
cts 59
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C enterNode() 16 62 26

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
 * 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