Skip to content

Commit 6bc4bb9

Browse files
committed
Resolving conflicts
2 parents bbcc738 + 5519dd9 commit 6bc4bb9

File tree

10 files changed

+2282
-2
lines changed

10 files changed

+2282
-2
lines changed

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@
4646
"phpunit/phpunit": "^8.0|^9.0",
4747
"rector/rector": "0.11.56",
4848
"symfony/var-dumper": "^4.2|^5.0|^6.0",
49-
"thecodingmachine/phpstan-strict-rules": "^0.12.0"
49+
"roave/no-floaters": "^1.1",
50+
"symplify/easy-coding-standard": "^6.0",
51+
"thecodingmachine/phpstan-strict-rules": "^0.11.0",
52+
"twig/twig": "^2.0"
5053
},
5154
"suggest": {
52-
"ext-simplexml": "It is needed for the checkstyle formatter"
55+
"ext-simplexml": "It is needed for the checkstyle formatter",
56+
"twig/twig": "Twig:^2.0 is needed to support the HTML formattter"
5357
},
5458
"autoload-dev": {
5559
"psr-4": {

dashboard.html

Lines changed: 2102 additions & 0 deletions
Large diffs are not rendered by default.

docs/get-started.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,26 @@ For changing the output format you can add the `format` flag. The following form
133133
- console
134134
- json
135135
- checkstyle
136+
- html
136137

137138
```bash
138139
./vendor/bin/phpinsights analyse --format=json
139140
```
140141

142+
::: Twig for HTML
143+
The HTML formatter requires Twig to render.
144+
So you have to add it do your dependencies if you want to use it.
145+
`composer require --dev twig/twig: ^2.0`
146+
:::
147+
141148
## Saving output to file
142149

143150
You can pipe the result to a file or to anywhere you like.
144151
A common use case is parsing the output formatted as json to a json file.
145152

146153
```bash
147154
./vendor/bin/phpinsights analyse --format=json > test.json
155+
./vendor/bin/phpinsights analyse --format=html > test.html
148156
```
149157

150158
When piping the result remember to add the no interaction flag `-n`, as the part where you need to interact is also getting piped. (the json format does not have any interaction)

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ parameters:
2525
- '#Access to an undefined property PHP_CodeSniffer\\Sniffs\\Sniff::\$#'
2626
- '#NunoMaduro\\PhpInsights\\Application\\Console\\Formatters\\Json has an unused parameter \$input#'
2727
- '#NunoMaduro\\PhpInsights\\Application\\Console\\Formatters\\Checkstyle has an unused parameter \$input#'
28+
- '#NunoMaduro\\PhpInsights\\Application\\Console\\Formatters\\Html has an unused parameter \$input#'
2829
- '#In method "NunoMaduro\\PhpInsights\\Domain\\File::process", caught "Throwable" must be rethrown#'
2930
- '#In method "NunoMaduro\\PhpInsights\\Domain\\FileProcessors\\FixerFileProcessor::processFile", caught "Throwable" must be rethrown#'
3031
- '#Empty catch block. If you are sure this is meant to be empty, please add a "// @ignoreException" comment in the catch block.#'

src/Application/Console/Formatters/FormatResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class FormatResolver
2020
'checkstyle' => Checkstyle::class,
2121
'github-action' => GithubAction::class,
2222
'codeclimate' => CodeClimate::class,
23+
'html' => Html::class,
2324
];
2425

2526
public static function resolve(
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NunoMaduro\PhpInsights\Application\Console\Formatters;
6+
7+
use Exception;
8+
use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
9+
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Twig\Environment as Twig;
13+
use Twig\Loader\FilesystemLoader;
14+
use Twig\TwigFilter;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class Html implements Formatter
20+
{
21+
/** @var OutputInterface */
22+
private $output;
23+
24+
public function __construct(InputInterface $input, OutputInterface $output)
25+
{
26+
$this->output = $output;
27+
}
28+
29+
/**
30+
* Format the result to the desired format.
31+
*
32+
* @param InsightCollection $insightCollection
33+
* @param string $dir
34+
* @param array<string> $metrics
35+
*
36+
* @throws Exception
37+
*/
38+
public function format(
39+
InsightCollection $insightCollection,
40+
string $dir,
41+
array $metrics
42+
): void {
43+
$this->output->write($this->getTwig()->render('dashboard.html.twig', [
44+
'dir' => $dir,
45+
'results' => $insightCollection->results(),
46+
'insights' => $insightCollection,
47+
]), false, OutputInterface::OUTPUT_RAW);
48+
}
49+
50+
private function getTwig(): Twig
51+
{
52+
$loader = new FilesystemLoader(__DIR__.'/../../../../views');
53+
$twig = new Twig($loader, [
54+
'cache' => false,
55+
'debug' => true,
56+
]);
57+
58+
$twig->addFilter(new TwigFilter('sluggify', static function (string $slug): string {
59+
$slug = preg_replace('/<(.*?)>/u', '', (string) $slug);
60+
$slug = preg_replace('/[\'"‘’“”]/u', '', (string) $slug);
61+
$slug = mb_strtolower((string) $slug, 'UTF-8');
62+
63+
preg_match_all('/[\p{L}\p{N}\.]+/u', (string) $slug, $words);
64+
65+
return implode('-', array_filter($words[0]));
66+
}));
67+
68+
return $twig;
69+
}
70+
}

src/Domain/Insights/InsightCollection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function getCollector(): Collector
3838
return $this->collector;
3939
}
4040

41+
/**
42+
* @return array<string, array<\NunoMaduro\PhpInsights\Domain\Contracts\Insight>>
43+
*/
44+
public function getInsightsPerMetric(): array
45+
{
46+
return $this->insightsPerMetric;
47+
}
48+
4149
/**
4250
* Gets all insights.
4351
*

views/dashboard.html.twig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" />
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css" integrity="sha256-zmfNZmXoNWBMemUOo1XUGFfc0ihGGLYdgtJS3KCr/l0=" crossorigin="anonymous" />
9+
10+
<title>PHP Insights</title>
11+
</head>
12+
<body>
13+
<div class="container-fluid">
14+
15+
<h1>PHP Insights</h1>
16+
17+
<p>
18+
<time datetime="{{ 'now'|date('Y-m-d H:i:s') }}">{{ 'now'|date('Y-m-d') }}</time>
19+
<code>{{ dir }}</code>
20+
</p>
21+
22+
<div class="row">
23+
<div class="col">{% include 'partials/stats_per_metric.html.twig' with {'id': 'quality', 'title': 'code quality', 'score': results.codeQuality} %}</div>
24+
<div class="col">{% include 'partials/stats_per_metric.html.twig' with {'id': 'complexity', 'title': 'complexity', 'score': results.complexity} %}</div>
25+
<div class="col">{% include 'partials/stats_per_metric.html.twig' with {'id': 'structure', 'title': 'structure', 'score': results.structure} %}</div>
26+
<div class="col">{% include 'partials/stats_per_metric.html.twig' with {'id': 'style', 'title': 'style', 'score': results.style} %}</div>
27+
</div>
28+
29+
<div class="alert alert-light">
30+
score scale:
31+
<span class="text-danger">■</span> 1 - 49
32+
<span class="text-warning">■</span> 50 - 79
33+
<span class="text-success">■</span> 80 - 100
34+
</div>
35+
36+
{% include 'partials/insights_per_metric.html.twig' with {'id': 'quality', 'title': 'code quality', 'metricNamespacePart': '\\Code\\'} %}
37+
{% include 'partials/insights_per_metric.html.twig' with {'id': 'complexity', 'title': 'complexity', 'metricNamespacePart': '\\Complexity\\'} %}
38+
{% include 'partials/insights_per_metric.html.twig' with {'id': 'structure', 'title': 'structure', 'metricNamespacePart': '\\Architecture\\'} %}
39+
{% include 'partials/insights_per_metric.html.twig' with {'id': 'style', 'title': 'style', 'metricNamespacePart': '\\Style\\'} %}
40+
41+
</div>
42+
43+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
44+
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha256-fzFFyH01cBVPYzl16KT40wqjhgPtq6FFUB6ckN2+GGw=" crossorigin="anonymous"></script>
45+
</body>
46+
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h2 id="{{ id }}">{{ title }}</h2>
2+
<div class="list-group mb-3">
3+
{% for metric, insightsPerMetric in insights.insightsPerMetric %}
4+
{% if metricNamespacePart in metric %}
5+
{% for insight in insightsPerMetric %}
6+
{% if insight.hasIssue() %}
7+
<div class="list-group-item">
8+
<div class="clearfix" data-toggle="collapse" href="#code-{{ insight.insightClass|sluggify }}" >
9+
<i class="fas text-danger fa-times"></i>
10+
<strong>{{ insight.title }}</strong>
11+
<span class="text-muted">({{ insight.details|length }})</span>
12+
<code class="float-right">{{ insight.insightClass }}</code>
13+
</div>
14+
<div class="collapse" id="code-{{ insight.insightClass|sluggify }}">
15+
<ul>
16+
{% for detail in insight.details %}
17+
<li>
18+
{% if detail.hasFile() %}
19+
<code>
20+
{{ detail.file }}{% if detail.hasLine() %}:{{ detail.line }}{% endif %}{% if detail.hasFunction() %}:{{ detail.function }}{% endif %}
21+
</code>
22+
{% endif %}
23+
{% if detail.hasMessage() %}
24+
<span>{{ detail.message }}</span>
25+
{% endif %}
26+
</li>
27+
{% endfor %}
28+
</ul>
29+
</div>
30+
</div>
31+
{% endif %}
32+
{% endfor %}
33+
{% endif %}
34+
{% endfor %}
35+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="alert text-center alert-{{ score >= 80 ? 'success' : (score >= 50 ? 'warning' : 'danger') }}">
2+
<strong>{{ title }}</strong>
3+
<a href="#{{ id }}" class="alert-link"><i class="fas fa-anchor"></i></a>
4+
<div class="lead">{{ score|number_format(1, '.') }} %</div>
5+
</div>

0 commit comments

Comments
 (0)