Completed
Push — master ( bdaaad...4f4372 )
by personal
06:57 queued 04:36
created

LcomVisitor::traverse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Hal\Metric\Class_\Structural;
3
4
use Hal\Component\Tree\Graph;
5
use Hal\Component\Tree\Node as TreeNode;
6
use Hal\Metric\Metrics;
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt;
9
use PhpParser\NodeVisitorAbstract;
10
11
/**
12
 * Lack of cohesion of methods
13
 *
14
 * Class ExternalsVisitor
15
 * @package Hal\Metric\Class_\Coupling
16
 */
17
class LcomVisitor extends NodeVisitorAbstract
18
{
19
20
    /**
21
     * @var Metrics
22
     */
23
    private $metrics;
24
25
    /**
26
     * ClassEnumVisitor constructor.
27
     * @param Metrics $metrics
28
     */
29
    public function __construct(Metrics $metrics)
30
    {
31
        $this->metrics = $metrics;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function leaveNode(Node $node)
38
    {
39
        if ($node instanceof Stmt\Class_) {
40
41
            // we build a graph of internal dependencies in class
42
            $graph = new Graph();
43
            $class = $this->metrics->get($node->namespacedName->toString());
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Class_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
Are you sure the assignment to $class is correct as $this->metrics->get($nod...spacedName->toString()) (which targets Hal\Metric\Metrics::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
45
            foreach ($node->stmts as $stmt) {
46
                if ($stmt instanceof Stmt\ClassMethod) {
47
48
                    if (!$graph->has($stmt->name . '()')) {
49
                        $graph->insert(new TreeNode($stmt->name . '()'));
50
                    }
51
                    $from = $graph->get($stmt->name . '()');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $from is correct as $graph->get($stmt->name . '()') (which targets Hal\Component\Tree\Graph::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
53
                    \iterate_over_node($stmt, function ($node) use ($from, &$graph) {
54
55 View Code Duplication
                        if ($node instanceof Node\Expr\PropertyFetch && isset($node->var->name) && $node->var->name == 'this') {
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
                            $name = getNameOfNode($node);
57
                            // use of attribute $this->xxx;
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
                            if (!$graph->has($name)) {
59
                                $graph->insert(new TreeNode($name));
60
                            }
61
                            $to = $graph->get($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $to is correct as $graph->get($name) (which targets Hal\Component\Tree\Graph::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
                            $graph->addEdge($from, $to);
0 ignored issues
show
Documentation introduced by
$from is of type null, but the function expects a object<Hal\Component\Tree\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$to is of type null, but the function expects a object<Hal\Component\Tree\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
                            return;
64
                        }
65
66
67
                        if ($node instanceof Node\Expr\MethodCall) {
68 View Code Duplication
                            if (!$node->var instanceof Node\Expr\New_ && isset($node->var->name) && getNameOfNode($node->var) === 'this') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
                                // use of method call $this->xxx();
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
                                // use of attribute $this->xxx;
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
                                $name = getNameOfNode($node->name) . '()';
72
                                if (!$graph->has($name)) {
73
                                    $graph->insert(new TreeNode($name));
74
                                }
75
                                $to = $graph->get($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $to is correct as $graph->get($name) (which targets Hal\Component\Tree\Graph::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
                                $graph->addEdge($from, $to);
0 ignored issues
show
Documentation introduced by
$from is of type null, but the function expects a object<Hal\Component\Tree\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$to is of type null, but the function expects a object<Hal\Component\Tree\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
                                return;
78
                            }
79
                        }
80
81
                    });
82
                }
83
            }
84
85
            // we count paths
86
            $paths = 0;
87
            foreach ($graph->all() as $node) {
88
                $paths += $this->traverse($node);
89
            }
90
91
            $class->set('lcom', $paths);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $class (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
        }
93
    }
94
95
    /**
96
     * Traverse node, and return 1 if node has not been visited yet
97
     *
98
     * @param TreeNode $node
99
     * @return int
100
     */
101
    private function traverse(TreeNode $node)
102
    {
103
        if ($node->visited) {
104
            return 0;
105
        }
106
        $node->visited = true;
107
108
        foreach ($node->getAdjacents() as $adjacent) {
109
            $this->traverse($adjacent);
110
        }
111
112
        return 1;
113
    }
114
}
115