Completed
Pull Request — master (#283)
by Oskar
04:36
created

PackageDependencies::getPackageOfClass()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.2
1
<?php
2
3
namespace Hal\Metric\Package;
4
5
use Hal\Metric\ClassMetric;
6
use Hal\Metric\InterfaceMetric;
7
use Hal\Metric\Metric;
8
use Hal\Metric\Metrics;
9
use Hal\Metric\PackageMetric;
10
11
class PackageDependencies
12
{
13
    public function calculate(Metrics $metrics)
14
    {
15
        $classes = array_filter($metrics->all(), function (Metric $metric) {
16
            return $metric instanceof ClassMetric || $metric instanceof InterfaceMetric;
17
        });
18
19
        foreach ($classes as $each) {
20
            $this->increaseDependencies($each, $metrics);
21
        }
22
    }
23
24
    private function increaseDependencies(Metric $class, Metrics $metrics)
25
    {
26
        if (! $class->has('package') || ! $class->has('externals')) {
27
            return;
28
        }
29
        $incomingPackage = $metrics->get($class->get('package')); /* @var $incomingPackage PackageMetric */
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $incomingPackage is correct as $metrics->get($class->get('package')) (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...
30
        foreach ($class->get('externals') as $outgoingClassName) {
31
            // same package?
32
            if (in_array($outgoingClassName, $incomingPackage->getClasses())) {
33
                continue;
34
            }
35
            $outgoingPackageName = $this->getPackageOfClass($outgoingClassName, $metrics);
36
            $incomingPackage->addOutgoingClassDependency($outgoingClassName, $outgoingPackageName);
37
            $outgoingPackage = $metrics->get($outgoingPackageName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $outgoingPackage is correct as $metrics->get($outgoingPackageName) (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...
38
39
            if ($outgoingPackage instanceof PackageMetric) {
40
                $outgoingPackage->addIncomingClassDependency($class->getName(), $incomingPackage->getName());
41
            }
42
43
        }
44
    }
45
46
    private function getPackageOfClass($className, Metrics $metrics)
47
    {
48
        if ($metrics->has($className) && $metrics->get($className)->has('package')) {
0 ignored issues
show
Bug introduced by
The method has cannot be called on $metrics->get($className) (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...
49
            return $metrics->get($className)->get('package');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $metrics->get($className) (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...
50
        }
51
        if (strpos($className, '\\') === false) {
52
            return '\\';
53
        }
54
        $parts = explode('\\', $className);
55
        array_pop($parts);
56
        return implode('\\', $parts) . '\\';
57
    }
58
}
59