Skip to content

Commit

Permalink
Merge pull request #32 from EdgedesignCZ/pdepend-summary-report
Browse files Browse the repository at this point in the history
Reports - pdepend summary and dependencies, open tab from navbar
  • Loading branch information
zdenekdrahos authored Oct 15, 2016
2 parents 6c8991f + 5907205 commit 786e3bb
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 140 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Tool | `--output file` (default) - generated files | `--output cli` |
phploc | [phploc.xml](https://edgedesigncz.github.io/phpqa/report/phploc.xml) | [](https://github.com/sebastianbergmann/phploc#analyse-a-directory-and-print-the-result) |
phpcpd | [phpcpd.xml](https://edgedesigncz.github.io/phpqa/report/phpcpd.xml) | [](https://github.com/sebastianbergmann/phpcpd#usage-example) |
phpcs | [checkstyle.xml](https://edgedesigncz.github.io/phpqa/report/checkstyle.xml) | [full report](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Reporting#printing-full-and-summary-reports) |
pdepend | [pdepend-jdepend.xml](https://edgedesigncz.github.io/phpqa/report/pdepend-jdepend.xml), [pdepend-summary.xml](https://edgedesigncz.github.io/phpqa/report/pdepend-summary.xml), [pdepend-jdepend.svg](https://edgedesigncz.github.io/phpqa/report/pdepend-jdepend.svg), [pdepend-pyramid.svg](https://edgedesigncz.github.io/phpqa/report/pdepend-pyramid.svg) | ✗ |
pdepend | [pdepend-jdepend.xml](https://edgedesigncz.github.io/phpqa/report/pdepend-jdepend.xml), [pdepend-summary.xml](https://edgedesigncz.github.io/phpqa/report/pdepend-summary.xml), [pdepend-dependencies.xml](https://edgedesigncz.github.io/phpqa/report/pdepend-dependencies.xml), [pdepend-jdepend.svg](https://edgedesigncz.github.io/phpqa/report/pdepend-jdepend.svg), [pdepend-pyramid.svg](https://edgedesigncz.github.io/phpqa/report/pdepend-pyramid.svg) | ✗ |
phpmd | [phpmd.xml](https://edgedesigncz.github.io/phpqa/report/phpmd.xml) | [](https://github.com/phpmd/phpmd/blob/master/src/main/php/PHPMD/Renderer/TextRenderer.php#L47) |
phpmetrics | [phpmetrics.html](https://edgedesigncz.github.io/phpqa/report/phpmetrics.html), [phpmetrics.xml](https://edgedesigncz.github.io/phpqa/report/phpmetrics.xml) | [](https://github.com/phpmetrics/PhpMetrics#usage) |

Expand Down
519 changes: 414 additions & 105 deletions app/report/pdepend.xsl

Large diffs are not rendered by default.

53 changes: 51 additions & 2 deletions app/report/phpqa.html.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
{%
set tabs = {
'phpmetrics': {
'link-overview': 'Overview',
'link-score': 'Score',
'link-relations': 'Relations map',
'link-repartition': 'Repartition',
'link-explore': 'Explore',
},
'pdepend': {
'overview': 'Overview',
'packages': 'Packages',
'dependencies': 'Dependencies',
'metrics': 'Metrics',
}
}
%}
<html>
<head>
<title>phpqa report - by EdgedesignCZ</title>
Expand All @@ -18,6 +35,10 @@
height: 100% !important;
border: 0;
}
.dropdown a.dropdown-toggle {
padding-left: 0px !important;
padding-right: 0px !important;
}
</style>
</head>
<body>
Expand All @@ -40,25 +61,53 @@
<ul class="nav navbar-nav nav-tabs" role="tablist">
{% for tool in tools %}
<li{{ loop.first ? ' class=active' : '' }}><a href="#{{ tool }}">{{ tool }}</a></li>
{% if tabs[tool] is defined %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
&nbsp;<span class="caret"></span>&nbsp;
</a>
<ul class="dropdown-menu">
{% for link, tab in tabs[tool] %}
<li><a href="#{{ tool }}" data-iframe-tab="{{ link }}">{{ tab }}</a></li>
{% endfor %}
</ul>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
</nav>

<div class="tab-content">
{% for tool in tools %}
<div role="tabpanel" class="tab-pane{{ loop.first ? ' active' : '' }}" id="{{ tool }}">
<iframe id="{{ tool }}" src="{{ tool }}.html"></iframe>
<iframe id="iframe-{{ tool }}" src="{{ tool }}.html"></iframe>
</div>
{% endfor %}
</div>

<script>
(function () {
var tabsInIframe = {
'#pdepend': function(iframe, tab) {
iframe.$("nav [aria-controls=" + tab + "]").tab('show');
},
'#phpmetrics': function(iframe, tab) {
iframe.document.getElementById(tab).onclick();
},
};
$('.nav a').click(function (e) {
e.preventDefault();
$(this).tab('show');
var target = $(this).data('iframe-tab');
if (target) {
var tabOpener = tabsInIframe[$(this).attr('href')];
var iframe = $(".tab-pane.active iframe")[0].contentWindow;
tabOpener(iframe, target);
}
});
}());
</script>
Expand Down
31 changes: 15 additions & 16 deletions src/CodeAnalysisTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ trait CodeAnalysisTasks
),
'phploc' => array(
'optionSeparator' => ' ',
'transformedXml' => 'phploc.xml',
'xml' => ['phploc.xml'],
),
'phpcs' => array(
'optionSeparator' => '=',
'transformedXml' => 'checkstyle.xml',
'xml' => ['checkstyle.xml'],
'errorsXPath' => '//checkstyle/file/error',
),
'phpmd' => array(
'optionSeparator' => ' ',
'transformedXml' => 'phpmd.xml',
'xml' => ['phpmd.xml'],
'errorsXPath' => '//pmd/file/violation',
),
'pdepend' => array(
'optionSeparator' => '=',
'transformedXml' => 'pdepend-jdepend.xml',
'xml' => ['pdepend-jdepend.xml', 'pdepend-summary.xml', 'pdepend-dependencies.xml'],
),
'phpcpd' => array(
'optionSeparator' => ' ',
'transformedXml' => 'phpcpd.xml',
'xml' => ['phpcpd.xml'],
'errorsXPath' => '//pmd-cpd/duplication',
),
);
Expand Down Expand Up @@ -149,7 +149,7 @@ private function phpcpd(RunningTool $tool)
'min-tokens' => $this->config->value('phpcpd.minTokens'),
);
if ($this->options->isSavedToFiles) {
$args['log-pmd'] = escapePath($tool->transformedXml);
$args['log-pmd'] = $tool->getEscapedXmlFile();
}
return $args;
}
Expand All @@ -169,7 +169,7 @@ private function phpcs(RunningTool $tool)
);
if ($this->options->isSavedToFiles) {
$args['report'] = 'checkstyle';
$args['report-file'] = escapePath($tool->transformedXml);
$args['report-file'] = $tool->getEscapedXmlFile();
} else {
$args['report'] = 'full';
}
Expand All @@ -181,6 +181,7 @@ private function pdepend()
return array(
'jdepend-xml' => $this->options->toFile('pdepend-jdepend.xml'),
'summary-xml' => $this->options->toFile('pdepend-summary.xml'),
'dependency-xml' => $this->options->toFile('pdepend-dependencies.xml'),
'jdepend-chart' => $this->options->toFile('pdepend-jdepend.svg'),
'overview-pyramid' => $this->options->toFile('pdepend-pyramid.svg'),
$this->options->ignore->pdepend(),
Expand All @@ -198,7 +199,7 @@ private function phpmd(RunningTool $tool)
$this->options->ignore->phpmd()
);
if ($this->options->isSavedToFiles) {
$args['reportfile'] = escapePath($tool->transformedXml);
$args['reportfile'] = $tool->getEscapedXmlFile();
}
return $args;
}
Expand All @@ -224,14 +225,12 @@ private function phpmetrics(RunningTool $tool)
private function buildHtmlReport()
{
foreach ($this->usedTools as $tool) {
if ($tool->transformedXml) {
$tool->htmlReport = $this->options->rawFile("{$tool}.html");
xmlToHtml(
$tool->transformedXml,
$this->config->path("report.{$tool}"),
$tool->htmlReport
);
}
$tool->htmlReport = $this->options->rawFile("{$tool}.html");
xmlToHtml(
$tool->getXmlFiles(),
$this->config->path("report.{$tool}"),
$tool->htmlReport
);
}
twigToHtml(
'phpqa.html.twig',
Expand Down
3 changes: 1 addition & 2 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public function buildRunningTools(array $tools)
if (array_key_exists($tool, $this->allowedTools)) {
$preload = [
'allowedErrorsCount' => $this->allowedTools[$tool],
'transformedXml' => array_key_exists('transformedXml', $config)
? $this->rawFile($config['transformedXml']) : ''
'xml' => array_key_exists('xml', $config) ? array_map([$this, 'rawFile'], $config['xml']) : []
];
$allowed[$tool] = new RunningTool($tool, $preload + $config);
}
Expand Down
27 changes: 21 additions & 6 deletions src/RunningTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class RunningTool
private $tool;
private $optionSeparator;

private $xmlFiles;
private $errorsXPath;
private $allowedErrorsCount;

public $transformedXml;
public $htmlReport;

public function __construct($tool, array $toolConfig)
{
$config = $toolConfig + [
'optionSeparator' => '=',
'transformedXml' => '',
'xml' => [],
'errorsXPath' => '',
'allowedErrorsCount' => null
];
$this->tool = $tool;
$this->optionSeparator = $config['optionSeparator'];
$this->transformedXml = $config['transformedXml'];
$this->xmlFiles = $config['xml'];
$this->errorsXPath = $config['errorsXPath'];
$this->allowedErrorsCount = $config['allowedErrorsCount'];
}
Expand All @@ -46,16 +46,31 @@ public function analyzeResult()
{
if (!$this->errorsXPath) {
return [true, ''];
} elseif (!file_exists($this->transformedXml)) {
} elseif (!file_exists($this->getMainXml())) {
return [false, 0];
}

$xml = simplexml_load_file($this->transformedXml);
$xml = simplexml_load_file($this->getMainXml());
$errorsCount = count($xml->xpath($this->errorsXPath));
$isOk = $errorsCount <= $this->allowedErrorsCount || !is_numeric($this->allowedErrorsCount);
return [$isOk, $errorsCount];
}


public function getXmlFiles()
{
return $this->xmlFiles;
}

public function getEscapedXmlFile()
{
return escapePath($this->getMainXml());
}

private function getMainXml()
{
return reset($this->xmlFiles);
}

public function __toString()
{
return $this->tool;
Expand Down
16 changes: 14 additions & 2 deletions src/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ function twigToHtml($template, array $params, $outputFile)
file_put_contents($outputFile, $html);
}

function xmlToHtml($input, $style, $outputFile)
function xmlToHtml(array $xmlDocuments, $style, $outputFile)
{
if (!$xmlDocuments) {
return;
}

convertPhpErrorsToExceptions();
try {
$rootXml = array_shift($xmlDocuments);
$xml = new DOMDocument();
$xml->load($input);
$xml->load($rootXml);

foreach ($xmlDocuments as $file) {
$anotherXml = new DOMDocument();
$anotherXml->load($file);
$xml->documentElement->appendChild($xml->importNode($anotherXml->documentElement, true));
}

$xsl = new DOMDocument();
$xsl->load($style);

Expand Down
16 changes: 12 additions & 4 deletions tests/Report/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public function setUp()
$this->phplocXsl = __DIR__ . "/../../app/report/phploc.xsl";
}

public function testShouldConvertTwigToHtml()
public function testConvertTwigToHtml()
{
twigToHtml("phpqa.html.twig", array('tools' => array()), $this->output);
assertThat(file_get_contents($this->output), containsString('phpqa'));
}

/** @dataProvider provideXml */
public function testShouldConvertXmlToHtml($xml, $assertOutput)
public function testConvertXmlToHtml($xml, $assertOutput)
{
xmlToHtml(__DIR__ . "/{$xml}", $this->phplocXsl, $this->output);
xmlToHtml([__DIR__ . "/{$xml}"], $this->phplocXsl, $this->output);
assertThat(file_get_contents($this->output), $assertOutput);
}

Expand All @@ -34,8 +34,16 @@ public function provideXml()
);
}

public function testIgnoreMissingXmlDocuments()
{
xmlToHtml([], $this->phplocXsl, $this->output);
assertThat(file_exists($this->output), is(false));
}

public function tearDown()
{
unlink($this->output);
if (file_exists($this->output)) {
unlink($this->output);
}
}
}
4 changes: 2 additions & 2 deletions tests/RunningToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testMarkSuccessWhenXPathIsNotDefined()
public function testMarkFailureWhenXmlFileDoesNotExist()
{
$tool = new RunningTool('tool', [
'transformedXml' => 'non-existent.xml',
'xml' => ['non-existent.xml'],
'errorsXPath' => '//errors/error',
]);
assertThat($tool->analyzeResult(), is([false, 0]));
Expand All @@ -32,7 +32,7 @@ public function testMarkFailureWhenXmlFileDoesNotExist()
public function testCompareAllowedCountWithErrorsCountFromXml($allowedErrors, $isOk)
{
$tool = new RunningTool('tool', [
'transformedXml' => 'tests/Error/errors.xml',
'xml' => ['tests/Error/errors.xml'],
'errorsXPath' => '//errors/error',
'allowedErrorsCount' => $allowedErrors
]);
Expand Down

0 comments on commit 786e3bb

Please sign in to comment.