|
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) |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
45
|
|
|
$files = $finder->fetch($this->config->get('files')); |
|
|
|
|
|
|
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
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.