Completed
Push — master ( 26540a...621fd0 )
by personal
04:38 queued 02:26
created

Composer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
namespace Hal\Metric\System\Packages\Composer;
3
4
use Hal\Application\Config\Config;
5
use Hal\Component\File\Finder;
6
use Hal\Metric\Metrics;
7
use Hal\Metric\ProjectMetric;
8
9
/**
10
 * Class Composer
11
 * @package Hal\Metric\System\Packages\Composer
12
 */
13
class Composer
14
{
15
16
    /**
17
     * @var Config
18
     */
19
    private $config;
20
21
    /**
22
     * GitChanges constructor.
23
     * @param array $files
24
     */
25
    public function __construct(Config $config, array $files)
0 ignored issues
show
Unused Code introduced by
The parameter $files is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * @param Metrics $metrics
32
     * @throws ConfigException
33
     */
34
    public function calculate(Metrics $metrics)
35
    {
36
37
        $projectMetric = new ProjectMetric('composer');
38
        $projectMetric->set('packages', []);
39
        $metrics->attach($projectMetric);
40
        $packages = [];
41
        $rawRequirements = [];
42
43
        // find composer.json files
44
        $finder = new Finder(['json'], $this->config->get('exclude'));
0 ignored issues
show
Documentation introduced by
$this->config->get('exclude') is of type null, but the function expects a array.

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...
45
        $files = $finder->fetch($this->config->get('files'));
0 ignored issues
show
Documentation introduced by
$this->config->get('files') is of type null, but the function expects a array.

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...
46
47
        foreach ($files as $filename) {
48
            if (!preg_match('/composer\.json|composer-dist\.json/', $filename)) {
49
                continue;
50
            }
51
            $datas = (object)json_decode(file_get_contents($filename));
52
53
            if (!isset($datas->require)) {
54
                continue;
55
            }
56
57
            $rawRequirements = array_merge($rawRequirements, (array)$datas->require);
58
            break;
59
        }
60
61
        $packagist = new Packagist();
62
        foreach ($rawRequirements as $requirement => $version) {
63
64
            $package = $packagist->get($requirement);
65
66
            $packages[$requirement] = (object)array(
67
                'name' => $requirement,
68
                'required' => $version,
69
                'latest' => $package->latest,
70
                'license' => $package->license,
71
                'homepage' => $package->homepage,
72
                'zip' => $package->zip,
73
            );
74
        }
75
76
        $projectMetric->set('packages', $packages);
77
    }
78
}