Skip to content

Commit

Permalink
add code style and static test analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
spl1nes committed Nov 24, 2019
1 parent cfb94e4 commit f376ad1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 15 deletions.
22 changes: 14 additions & 8 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public function __construct(array $argv)
$destination = ($key = \array_search('-d', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$testLog = ($key = \array_search('-u', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$langArray = ($key = \array_search('-l', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$basePath = ($key = \array_search('-b', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');

if (isset($help) || !isset($destination) || !isset($testLog) || !isset($langArray)) {
if (isset($help) || !isset($destination) || !isset($testLog) || !isset($langArray) || !isset($basePath)) {
$this->printUsage();

return;
Expand All @@ -26,6 +27,7 @@ public function __construct(array $argv)
$destination = \rtrim($destination, '/\\');
$testLog = \rtrim($testLog, '/\\');
$langArray = \rtrim($langArray, '/\\');
$basePath = \rtrim($basePath, '/\\');

if (!\file_exists($testLog)) {
echo 'File ' . $testLog . ' doesn\'t exist.' . "\n";
Expand All @@ -37,7 +39,7 @@ public function __construct(array $argv)
return;
}

$this->createReport($destination, $testLog, $langArray, $argv);
$this->createReport($basePath, $destination, $testLog, $langArray, $argv);
}

private function setupHandlers() : void
Expand All @@ -63,11 +65,12 @@ private function setupHandlers() : void
\mb_internal_encoding('UTF-8');
}

private function createReport(string $destination, string $testLog, string $langArray, array $argv) : void
private function createReport(string $basePath, string $destination, string $testLog, string $langArray, array $argv) : void
{
$template = ($key = \array_search('-t', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$codeCoverage = ($key = \array_search('-c', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$version = ($key = \array_search('-v', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$codeStyle = ($key = \array_search('-s', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');
$codeAnalysis = ($key = \array_search('-a', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" ');

if ($template !== null && !\file_exists($template)) {
echo 'File ' . $template . ' doesn\'t exist.' . "\n";
Expand All @@ -79,17 +82,20 @@ private function createReport(string $destination, string $testLog, string $lang
return;
}

$this->reportController = new ReportController($destination, $testLog, $langArray, $template, $codeCoverage, $argv);
$this->reportController = new ReportController($basePath, $destination, $testLog, $langArray, $template, $codeCoverage, $codeStyle, $codeAnalysis, $argv);
$this->reportController->createReport();
}

private function printUsage() : void
{
echo 'Usage: -d <DESTINATION_PATH> -t <TEMPLATE> -u <JUNIT_UNIT_TEST_LOG> -c <CODE_COVERAGE_REPORT> -l <LANGUAGE_FILE>' . "\n\n";
echo 'Usage: -b <BASE_PATH> -d <DESTINATION_PATH> -t <TEMPLATE> -u <JUNIT_UNIT_TEST_LOG> -c <CODE_COVERAGE_REPORT> -l <LANGUAGE_FILE>' . "\n\n";
echo "\t" . '-b Base directory path of the project (=absolute root path)' . "\n";
echo "\t" . '-d Destination directory' . "\n";
echo "\t" . '-t Template of the test report (has to be a directory containing a `index.tpl.php` which is rendered as `html` file during the generation process) (*optional* no theme definition will use the default theme).' . "\n";
echo "\t" . '-u Unit test log (`junit` style)' . "\n";
echo "\t" . '-c Code coverage source (`coverage-clover`) (*optional*)' . "\n";
echo "\t" . '-u Unit test log (phpunit `junit`)' . "\n";
echo "\t" . '-c Code coverage source (phpunit `coverage-clover`) (*optional*)' . "\n";
echo "\t" . '-s Code style source (code sniffer `junit`) (*optional*)' . "\n";
echo "\t" . '-a Code analysis source (phpstan `coverage-clover`) (*optional*)' . "\n";
echo "\t" . '-l Language file (`php array`)' . "\n";
}
}
91 changes: 85 additions & 6 deletions src/Application/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,36 @@

class ReportController
{
private $basePath = '';
private $destination = '';
private $testLog = '';
private $langArray = [];
private $template = null;
private $data = [];
private $codeCoverage = null;
private $codeStyle = null;
private $codeAnalysis = null;

public function __construct(
string $basePath,
string $destination,
string $testLog,
string $langArray,
?string $template,
?string $codeCoverage,
?string $codeStyle,
?string $codeAnalysis,
?array $data
) {
$this->basePath = $basePath;
$this->destination = $destination;
$this->testLog = $testLog;
$this->langArray = include $langArray;
$this->template = $template;
$this->data = $data ?? '';
$this->codeCoverage = $codeCoverage; // not used so far.
$this->codeCoverage = $codeCoverage;
$this->codeStyle = $codeStyle;
$this->codeAnalysis = $codeAnalysis;
}

public function createReport() : void
Expand All @@ -49,11 +58,16 @@ public function createReport() : void
$this->handleTests($testReportData, $domTest, $testView);
$this->handleSuits($testReportData, $domTest, $testView);

if (\file_exists($this->codeCoverage)) {
$domCoverage = new \DOMDocument();
$domCoverage->loadXML(\file_get_contents($this->codeCoverage));
if ($this->codeCoverage !== null && \file_exists($this->codeCoverage)) {
$this->handleCoverage($testReportData, $testView);
}

if ($this->codeStyle !== null && \file_exists($this->codeStyle)) {
$this->handleStyle($testReportData, $testView);
}

$this->handleCoverage($testReportData, $domCoverage, $testView);
if ($this->codeAnalysis !== null && \file_exists($this->codeAnalysis)) {
$this->handleAnalysis($testReportData, $testView);
}

$testView->setTestResult($testReportData);
Expand Down Expand Up @@ -198,8 +212,11 @@ private function handleSuits(array &$testReportData, $dom, $testView) : void
}
}

private function handleCoverage(array &$testReportData, $dom, $testView) : void
private function handleCoverage(array &$testReportData, $testView) : void
{
$dom = new \DOMDocument();
$dom->loadXML(\file_get_contents($this->codeCoverage));

$classes = $dom->getElementsByTagName('class');
foreach ($classes as $class) {
$metrics = $class->getElementsByTagName('metrics');
Expand Down Expand Up @@ -234,6 +251,68 @@ private function handleCoverage(array &$testReportData, $dom, $testView) : void
}
}

private function handleStyle(array &$testReportData, $testView) : void
{
$dom = new \DOMDocument();
$dom->loadXML(\file_get_contents($this->codeStyle));

$cutoff = \strlen($this->basePath);

$classes = $dom->getElementsByTagName('testsuite');
foreach ($classes as $class) {
$className = $class->getAttribute('name');
$ending = \stripos($className, '.');
$className = \ltrim(\substr($className, $cutoff, $ending - $cutoff), '/');
$className = \str_replace('/', '\\', $className) . 'Test';
$exploded = \explode('\\', $className);

\array_splice($exploded, 1, 0, 'tests');
$className = \implode('\\', $exploded);

$testView->incrementStyleFiles();
$testView->addStyleErrors((int) $class->getAttribute('errors'));
$testView->addStyleFailures((int) $class->getAttribute('failures'));

if (!isset($this->langArray[$className])) {
continue;
}

$testReportData[$className]['styleerrors'] = (int) $class->getAttribute('errors');
$testReportData[$className]['stylefailures'] = (int) $class->getAttribute('failures');
}
}

private function handleAnalysis(array &$testReportData, $testView) : void
{
$json = \json_decode(\file_get_contents($this->codeAnalysis), true);

if (!isset($json['files'])) {
return;
}

$cutoff = \strlen($this->basePath);

foreach ($json['files'] as $name => $file) {
$className = $name;
$ending = \stripos($className, '.');
$className = \ltrim(\substr($className, $cutoff, $ending - $cutoff), '/');
$className = \str_replace('/', '\\', $className) . 'Test';
$exploded = \explode('\\', $className);

\array_splice($exploded, 1, 0, 'tests');
$className = \implode('\\', $exploded);

$testView->incrementStaticFileErrors();
$testView->addStaticErrors((int) $file['errors']);

if (!isset($this->langArray[$className])) {
continue;
}

$testReportData[$className]['staticerrors'] = (int) $file['errors'];
}
}

private function createOutputDir() : void
{
if (!\file_exists($this->destination)) {
Expand Down
32 changes: 32 additions & 0 deletions src/Application/Views/TestView.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ class TestView
protected $conditionals = 0;
protected $conditionalsCovered = 0;

protected $styleFiles = 0;
protected $styleErrors = 0;
protected $styleFailures = 0;

protected $staticFileErrors = 0;
protected $staticErrors = 0;

public function addStaticErrors(int $staticErrors) : void
{
$this->staticErrors += $staticErrors;
}

public function incrementStaticFileErrors() : void
{
++$this->staticFileErrors;
}

public function incrementStyleFiles() : void
{
++$this->styleFiles;
}

public function addStyleErrors(int $styleErrors) : void
{
$this->styleErrors += $styleErrors;
}

public function addStyleFailures(int $styleFailures) : void
{
$this->styleFailures += $styleFailures;
}

public function addMethods(int $methods) : void
{
$this->methods += $methods;
Expand Down
36 changes: 35 additions & 1 deletion src/Theme/index.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<table>
<thead>
<tr>
<th><?= $this->getText(':testing_summary_coverage'); ?>
<th><?= $this->getText(':description'); ?>
<th><?= $this->getText(':total'); ?>
<th><?= $this->getText(':successful'); ?>
<th><?= $this->getText(':skipps'); ?>
Expand All @@ -99,6 +99,22 @@
<td><?= $this->warnings; ?>
<td><?= $this->failures; ?>
<td><?= $this->errors; ?>
<tr>
<th><?= $this->getText(':static_tests'); ?>
<td><?= $this->styleFiles; ?>
<td><?= $this->styleFiles - $this->staticFileErrors; ?>
<td>0
<td>0
<td>0
<td><?= $this->staticFileErrors; ?>
<tr>
<th><?= $this->getText(':code_style'); ?>
<td><?= $this->styleFiles; ?>
<td><?= $this->styleFiles - $this->styleErrors; ?>
<td>0
<td><?= $this->styleFailures; ?>
<td>0
<td><?= $this->styleErrors; ?>
</table>

<p><?= $this->getText(':testing_summary_desc_2'); ?> <strong><?= $this->assertions; ?></strong></p>
Expand Down Expand Up @@ -143,6 +159,7 @@
<table>
<thead>
<tr>
<th><?= $this->getText(':description'); ?>
<th><?= $this->getText(':total'); ?>
<th><?= $this->getText(':successful'); ?>
<th><?= $this->getText(':skipps'); ?>
Expand All @@ -151,12 +168,29 @@
<th><?= $this->getText(':errors'); ?>
<tbody>
<tr>
<td><?= $this->getText(':tests'); ?>
<td><?= $result['tests']; ?>
<td><?= $result['tests'] - $result['skips'] - $result['failures'] - $result['errors']; ?>
<td><?= $result['skips']; ?>
<td><?= $result['warnings']; ?>
<td><?= $result['failures']; ?>
<td><?= $result['errors']; ?>
<tr>
<td><?= $this->getText(':static_tests'); ?>
<td>1
<td><?= $result['staticerrors'] > 0 ? 0 : 1; ?>
<td>0
<td>0
<td>0
<td><?= $result['staticerrors'] ?? 0; ?>
<tr>
<td><?= $this->getText(':code_style'); ?>
<td>1
<td><?= $result['styleerrors'] > 0 || $result['stylefailures'] > 0 ? 0 : 1; ?>
<td>0
<td>0
<td><?= $result['stylefailures'] ?? 0; ?>
<td><?= $result['styleerrors'] ?? 0; ?>
</table>
</section>
<?php else : ?>
Expand Down

0 comments on commit f376ad1

Please sign in to comment.