Skip to content

Commit a59c49a

Browse files
authored
Merge pull request #424 from phpmetrics/ui_composer
Composer report, UI
2 parents 67ac2ce + defc1fa commit a59c49a

File tree

8 files changed

+262
-87
lines changed

8 files changed

+262
-87
lines changed

src/Hal/Application/Analyze.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ public function run($files)
136136
// Unit test
137137
(new UnitTesting($this->config, $files))->calculate($metrics);
138138

139+
$this->output->clearln();
140+
139141
//
140142
// Composer
143+
$this->output->write('Executing composer analyzes, requesting https://packagist.org...');
141144
(new Composer($this->config, $files))->calculate($metrics);
142145

143146
$this->output->clearln();

src/Hal/Metric/System/Packages/Composer/Composer.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,21 @@ public function calculate(Metrics $metrics)
4141

4242
$packagist = new Packagist();
4343
foreach ($rawRequirements as $requirement => $version) {
44-
$package = $packagist->get($requirement);
45-
46-
$packages[$requirement] = (object)[
47-
'name' => $requirement,
48-
'required' => $version,
49-
'installed' => isset($rawInstalled[$requirement]) ? $rawInstalled[$requirement] : null,
50-
'latest' => $package->latest,
51-
'license' => $package->license,
52-
'homepage' => $package->homepage,
53-
'zip' => $package->zip,
54-
];
44+
$installed = isset($rawInstalled[$requirement]) ? $rawInstalled[$requirement] : null;
45+
$package = $packagist->get($requirement, $installed);
46+
47+
$package->installed = $installed;
48+
$package->required = $version;
49+
$package->name = $requirement;
50+
$package->status = version_compare($package->required, $package->latest) === -1 ? 'outdated' : 'latest';
51+
$packages[$requirement] = $package;
5552
}
5653

54+
// exclude extensions
55+
$packages = array_filter($packages, function ($package) {
56+
return !preg_match('!(^php$|^ext\-)!', $package->name);
57+
});
58+
5759
$projectMetric->set('packages', $packages);
5860
$projectMetric->set('packages-installed', $rawInstalled);
5961
}

src/Hal/Metric/System/Packages/Composer/Packagist.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ public function get($package)
1717
$response->latest = null;
1818
$response->license = [];
1919
$response->homepage = null;
20+
$response->description = null;
2021
$response->time = null;
2122
$response->zip = null;
2223
$response->compare = null;
24+
$response->type = 'unknown';
25+
$response->github_stars = 0;
26+
$response->github_watchers = 0;
27+
$response->github_forks = 0;
28+
$response->github_open_issues = 0;
29+
$response->download_total = 0;
30+
$response->download_monthly = 0;
31+
$response->download_daily = 0;
32+
$response->favers = 0;
2333

2434
if (!preg_match('/\w+\/\w+/', $package)) {
2535
return $response;
@@ -32,6 +42,18 @@ public function get($package)
3242
return $response;
3343
}
3444

45+
$response->type = $json->package->type;
46+
$response->description = $json->package->description;
47+
$response->type = $json->package->type;
48+
$response->github_stars = $json->package->github_stars;
49+
$response->github_watchers = $json->package->github_watchers;
50+
$response->github_forks = $json->package->github_forks;
51+
$response->github_open_issues = $json->package->github_open_issues;
52+
$response->download_total = $json->package->downloads->total;
53+
$response->download_monthly = $json->package->downloads->monthly;
54+
$response->download_daily = $json->package->downloads->daily;
55+
$response->favers = $json->package->favers;
56+
3557
// get latest version
3658
$latest = '0.0.0';
3759
foreach ((array)$json->package->versions as $version => $datas) {

src/Hal/Report/Html/Reporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function generate(Metrics $metrics)
8888
$this->renderPage($this->templateDir . '/html_report/violations.php', $logDir . '/violations.html', $consolidated, $history);
8989
$this->renderPage($this->templateDir . '/html_report/packages.php', $logDir . '/packages.html', $consolidated, $history);
9090
$this->renderPage($this->templateDir . '/html_report/package_relations.php', $logDir . '/package_relations.html', $consolidated, $history);
91+
$this->renderPage($this->templateDir . '/html_report/composer.php', $logDir . '/composer.html', $consolidated, $history);
9192
if ($this->config->has('git')) {
9293
$this->renderPage($this->templateDir . '/html_report/git.php', $logDir . '/git.html', $consolidated, $history);
9394
}

templates/html_report/_footer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
Violations (<?php echo $sum->violations->total;?>)
3232
</a>
3333
</li>
34+
<li>
35+
<a href="composer.html">
36+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>
37+
Composer
38+
</a>
39+
</li>
3440
<?php if($config->has('junit')) { ?>
3541
<li>
3642
<a href="junit.html">

templates/html_report/composer.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
require __DIR__ . '/_header.php';
3+
4+
$packages = isset($project['composer']['packages']) ? $project['composer']['packages'] : [];
5+
usort($packages, function ($a, $b) {
6+
return strcmp($a->name, $b->name);
7+
});
8+
if ([] === $packages) {
9+
echo '<div class="row"><div class="column"><div class="bloc">No composer.json file found in this project</div></div></div>';
10+
require __DIR__ . '/_footer.php';
11+
return;
12+
}
13+
?>
14+
15+
<div class="row">
16+
<div class="column">
17+
<div class="bloc">
18+
<h4><?php echo count($packages); ?> Composer dependencies</h4>
19+
20+
<div class="list">
21+
<?php foreach ($packages as $package) { ?>
22+
<div class="list-item">
23+
<div class="list-item-title">
24+
<a target="_blank" href="https://packagist.org/packages/<?php echo $package->name; ?>">
25+
<?php echo $package->name; ?>
26+
</a>
27+
</div>
28+
<div class="help">
29+
<div class="help-inner">
30+
<?php if ('outdated' === $package->status) { ?>
31+
<span class="help-warning" style="position: absolute; right: 10px;">This package should be updated.</span>
32+
<?php } ?>
33+
<span class="badge"><?php echo $package->type; ?></span>
34+
<?php echo $package->description; ?>
35+
</div>
36+
</div>
37+
38+
<div class="list-item-content">
39+
<?php $width = sprintf('%d%%', round(100 / 9, 0)); ?>
40+
<table class="table-metrics">
41+
<tr>
42+
<td width="<?php echo $width; ?>">
43+
<div class="card-number"><?php echo $package->required; ?></div>
44+
<div class="card-label">Required version</div>
45+
</td>
46+
<td width="<?php echo $width; ?>">
47+
<div class="card-number"><?php echo $package->installed; ?></div>
48+
<div class="card-label">Installed</div>
49+
</td>
50+
<td width="<?php echo $width; ?>">
51+
<div class="card-number"><?php echo $package->latest; ?></div>
52+
<div class="card-label">Latest</div>
53+
</td>
54+
<td width="<?php echo $width; ?>">
55+
<div
56+
class="card-number"><?php echo number_format($package->github_stars, 0); ?></div>
57+
<div class="card-label">Github stars</div>
58+
</td>
59+
<td width="<?php echo $width; ?>">
60+
<div
61+
class="card-number"><?php echo number_format($package->github_forks, 0); ?></div>
62+
<div class="card-label">Github forks</div>
63+
</td>
64+
<td width="<?php echo $width; ?>">
65+
<div
66+
class="card-number"><?php echo number_format($package->github_open_issues, 0); ?></div>
67+
<div class="card-label">Github open issues</div>
68+
</td>
69+
<td width="<?php echo $width; ?>">
70+
<div
71+
class="card-number"><?php echo number_format($package->download_total, 0); ?></div>
72+
<div class="card-label">Total downloads</div>
73+
</td>
74+
<td width="<?php echo $width; ?>">
75+
<div
76+
class="card-number"><?php echo number_format($package->download_monthly, 0); ?></div>
77+
<div class="card-label">Monthly downloads</div>
78+
</td>
79+
<td width="<?php echo $width; ?>">
80+
<div class="card-number">
81+
<?php foreach ($package->license as $license) { ?>
82+
<a target="_blank"
83+
href="https://spdx.org/licenses/<?php echo $license; ?>.html"><?php echo $license; ?></a>
84+
<?php } ?>
85+
</div>
86+
<div class="card-label">License(s)</div>
87+
</td>
88+
</tr>
89+
</table>
90+
</div>
91+
</div>
92+
<?php } ?>
93+
</div>
94+
</div>
95+
</div>
96+
</div>
97+
98+
<?php require __DIR__ . '/_footer.php'; ?>

templates/html_report/css/style.css

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ a {
295295
font-size: 0.8em;
296296
position: absolute;
297297
right: 10px;
298-
bottom: 10px;
298+
top: 10px;
299299
}
300300
.progress svg {
301301
vertical-align:middle;;
@@ -340,7 +340,33 @@ a[target="_blank"]::before {
340340
color: #333;
341341
text-align: left;
342342
}
343-
343+
.bloc .number, .bloc .number-alternate {
344+
min-height: 55px;
345+
}
346+
.bloc .chart-in-number {
347+
margin-top:1em;
348+
}
349+
.bloc .bloc-action {
350+
background-color: #f3f7fa;
351+
text-align: center;
352+
padding:10px 0;
353+
margin:0 -15px -15px -15px;
354+
-webkit-border-bottom-right-radius: 3px;
355+
-webkit-border-bottom-left-radius: 3px;
356+
-moz-border-radius-bottomright: 3px;
357+
-moz-border-radius-bottomleft: 3px;
358+
border-bottom-right-radius: 3px;
359+
border-bottom-left-radius: 3px;
360+
font-size: 0.9em;
361+
color: #95999c;
362+
}
363+
.bloc .bloc-action a {
364+
color: #48566c;
365+
text-decoration: none;
366+
}
367+
.bloc .bloc-action a:hover {
368+
color: #000;
369+
}
344370
.bloc .label {
345371
color: #333;
346372
text-align: left;
@@ -380,6 +406,39 @@ a[target="_blank"]::before {
380406
.column-help {
381407
margin-bottom: 0 !important;
382408
}
409+
410+
/* ----- list ----- */
411+
.list {
412+
text-align: left;
413+
}
414+
.list-item {
415+
padding:1em;
416+
position: relative;
417+
-webkit-border-radius: 4px;
418+
-moz-border-radius: 4px;
419+
border-radius: 4px;
420+
}
421+
.list-item-title {
422+
font-weight: bold;
423+
}
424+
.list-item:hover {
425+
background-color: #EBF8FF;
426+
}
427+
.table-metrics {
428+
margin: 0.5em;
429+
text-align: center;
430+
}
431+
.table-metrics td {
432+
text-align: center;
433+
}
434+
.table-metrics .card-number {
435+
font-weight: bold;
436+
}
437+
.table-metrics .card-label {
438+
color: #333;
439+
font-size: 0.9em;
440+
}
441+
383442
/* -------- charts ---------------- */
384443
.tooltip {
385444
position: absolute;
@@ -596,3 +655,9 @@ footer {
596655
max-height: 400px !important;
597656
}
598657
}
658+
659+
660+
/* ------ composer ----- */
661+
.help-warning {
662+
background-color: #fbd38d;
663+
}

0 commit comments

Comments
 (0)