Completed
Pull Request — master (#301)
by personal
02:13
created

DepthOfInheritanceTree   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 46.67 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 21
loc 45
rs 10
wmc 6
lcom 0
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B calculate() 21 36 6

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
namespace Hal\Metric\System\Coupling;
4
5
use Hal\Component\Tree\Graph;
6
use Hal\Component\Tree\Node;
7
use Hal\Component\Tree\Operator\SizeOfTree;
8
use Hal\Metric\ClassMetric;
9
use Hal\Metric\Metrics;
10
use Hal\Metric\ProjectMetric;
11
12
/**
13
 * Estimates DIT
14
 * @see https://www.cse.iitb.ac.in/~rkj/inheritancemetrics.pdf
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class DepthOfInheritanceTree
19
{
20
21
    /**
22
     * @param Metrics $metrics
23
     */
24
    public function calculate(Metrics $metrics)
25
    {
26
27
        $projectMetric = new ProjectMetric('tree');
28
29
        // building graph with parents / childs relations only
30
        $graph = new Graph();
31
32 View Code Duplication
        foreach ($metrics->all() as $metric) {
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...
33
            if (!$metric instanceof ClassMetric) {
34
                continue;
35
            }
36
37
            if (!$graph->has($metric->get('name'))) {
38
                $graph->insert(new Node($metric->get('name')));
39
            }
40
41
            $to = $graph->get($metric->get('name'));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $to is correct as $graph->get($metric->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...
42
43
            foreach ($metric->get('parents') as $parent) {
0 ignored issues
show
Bug introduced by
The expression $metric->get('parents') of type null is not traversable.
Loading history...
44
                if (!$graph->has($parent)) {
45
                    $graph->insert(new Node($parent));
46
                }
47
48
                $from = $graph->get($parent);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $from is correct as $graph->get($parent) (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...
49
50
                $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...
51
            }
52
        }
53
54
        $size = new SizeOfTree($graph);
55
        $averageHeight = $size->getAverageHeightOfGraph();
56
57
        $projectMetric->set('depthOfInheritanceTree', $averageHeight);
58
        $metrics->attach($projectMetric);
59
    }
60
61
62
}
63