-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
625 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - CI-Report-Converter | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package CI-Report-Converter | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @link https://github.com/JBZoo/CI-Report-Converter | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CiReportConverter\Converters; | ||
|
||
use JBZoo\CiReportConverter\Formats\Source\FileRef; | ||
use JBZoo\CiReportConverter\Formats\Source\SourceCaseOutput; | ||
use JBZoo\CiReportConverter\Formats\Source\SourceSuite; | ||
use JBZoo\CiReportConverter\Formats\Xml; | ||
use JBZoo\Data\Data; | ||
|
||
use function JBZoo\Data\data; | ||
|
||
/** | ||
* Class PmdCpdConverter | ||
* @package JBZoo\CiReportConverter\Converters | ||
*/ | ||
class PmdCpdConverter extends AbstractConverter | ||
{ | ||
public const TYPE = 'pmd-cpd'; | ||
public const NAME = 'PmdCpd.xml'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function toInternal(string $source): SourceSuite | ||
{ | ||
$xmlAsArray = data(Xml::dom2Array(Xml::createDomDocument($source))); | ||
|
||
$sourceSuite = new SourceSuite($this->rootSuiteName ?: 'PMD Copy/Paste Detector'); | ||
|
||
foreach ($xmlAsArray->find('_children.0._children') as $duplication) { | ||
$duplication = data($duplication); | ||
$lines = self::getLinesNumber($duplication); | ||
$tokens = self::getTokens($duplication); | ||
|
||
$mainFile = $this->getFileByIndex($duplication); | ||
$errorTitle = "Duplicate code found (lines={$lines}, tokens={$tokens})"; | ||
|
||
$case = $sourceSuite->addTestCase(self::getTestCaseName($mainFile, $lines)); | ||
$case->file = $mainFile->fullpath; | ||
$case->line = $mainFile->line; | ||
$case->warning = new SourceCaseOutput('Warning', $errorTitle, $this->getDetails($duplication)); | ||
} | ||
|
||
return $sourceSuite; | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @param int $index | ||
* @return FileRef | ||
*/ | ||
private function getFileByIndex(Data $duplication, int $index = 0): FileRef | ||
{ | ||
$fileRef = new FileRef(); | ||
$fileRef->fullpath = $duplication->findString("_children.{$index}._attrs.path"); | ||
$fileRef->line = $duplication->findInt("_children.{$index}._attrs.line"); | ||
$fileRef->name = $this->cleanFilepath($fileRef->fullpath ?: ''); | ||
|
||
return $fileRef; | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @return FileRef[] | ||
*/ | ||
private function getFileList(Data $duplication): array | ||
{ | ||
$list = []; | ||
|
||
foreach ($duplication->getArray('_children') as $index => $child) { | ||
if (isset($child['_node']) && $child['_node'] === 'file') { | ||
$list[] = $this->getFileByIndex($duplication, $index); | ||
} | ||
} | ||
|
||
return $list; | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @return string | ||
*/ | ||
private static function getCodeFragment(Data $duplication): string | ||
{ | ||
foreach ($duplication->getArray('_children') as $child) { | ||
if (isset($child['_node']) && $child['_node'] === 'codefragment') { | ||
return $child['_text'] ?? 'Undefined Code Fragment'; | ||
} | ||
} | ||
|
||
return 'Undefined Code Fragment'; | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @return int | ||
*/ | ||
private static function getLinesNumber(Data $duplication): int | ||
{ | ||
return $duplication->findInt('_attrs.lines'); | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @return int | ||
*/ | ||
private static function getTokens(Data $duplication): int | ||
{ | ||
return $duplication->findInt('_attrs.tokens'); | ||
} | ||
|
||
/** | ||
* @param Data $duplication | ||
* @return string | ||
*/ | ||
private function getDetails(Data $duplication): string | ||
{ | ||
$fileList = $this->getFileList($duplication); | ||
|
||
$lines = self::getLinesNumber($duplication); | ||
$tokens = self::getTokens($duplication); | ||
$fileNumber = count($fileList); | ||
|
||
$filesAsString = ''; | ||
foreach ($fileList as $fileRef) { | ||
$filePoint = self::getFileName($fileRef, $lines); | ||
$filesAsString .= "- {$filePoint}\n"; | ||
} | ||
|
||
return implode("\n", [ | ||
'', | ||
"Found {$lines} cloned lines in {$fileNumber} files ({$tokens} tokens):", | ||
$filesAsString, | ||
'', | ||
"Code Fragment:", | ||
'', | ||
'```', | ||
self::getCodeFragment($duplication), | ||
'```', | ||
]); | ||
} | ||
|
||
/** | ||
* @param FileRef $fileRef | ||
* @param int $lines | ||
* @return string | ||
*/ | ||
private static function getTestCaseName(FileRef $fileRef, int $lines): string | ||
{ | ||
$startLine = (int)$fileRef->line; | ||
$endLine = $startLine + $lines; | ||
$place = $startLine === $endLine ? (string)$startLine : "{$startLine}-{$endLine}"; | ||
|
||
if ($lines > 0) { | ||
return "{$fileRef->name}:{$place} ({$lines} lines)"; | ||
} | ||
|
||
return "{$fileRef->name}:{$place}"; | ||
} | ||
|
||
/** | ||
* @param FileRef $fileRef | ||
* @param int $lines | ||
* @return string | ||
*/ | ||
private static function getFileName(FileRef $fileRef, int $lines = 0): string | ||
{ | ||
$startLine = (int)$fileRef->line; | ||
$endLine = $startLine + $lines; | ||
$place = $startLine === $endLine ? (string)$startLine : "{$startLine}-{$endLine}"; | ||
|
||
return "{$fileRef->fullpath}:{$place}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - CI-Report-Converter | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package CI-Report-Converter | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @link https://github.com/JBZoo/CI-Report-Converter | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CiReportConverter\Formats\Source; | ||
|
||
use JBZoo\CiReportConverter\Formats\AbstractNode; | ||
|
||
/** | ||
* Class FileRef | ||
* @package JBZoo\CiReportConverter\Formats\Source | ||
* | ||
* @property string $name | ||
* @property string|null $fullpath | ||
* @property int|null $line | ||
* @property int|null $column | ||
*/ | ||
class FileRef extends AbstractNode | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $meta = [ | ||
'name' => ['string'], // It's relative path to file | ||
'fullpath' => ['string'], | ||
'line' => ['int'], | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFullName(): string | ||
{ | ||
$result = (string)$this->fullpath; | ||
if ($this->line > 0) { | ||
$result .= ":{$this->line}"; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getShortName(): string | ||
{ | ||
$result = $this->name; | ||
if ($this->line > 0) { | ||
$result .= " on line {$this->line}"; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - CI-Report-Converter | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package CI-Report-Converter | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @link https://github.com/JBZoo/CI-Report-Converter | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit; | ||
|
||
use JBZoo\CiReportConverter\Converters\JUnitConverter; | ||
use JBZoo\CiReportConverter\Converters\PmdCpdConverter; | ||
use JBZoo\CiReportConverter\Converters\TeamCityTestsConverter; | ||
|
||
/** | ||
* Class ConverterPmdCpdTest | ||
* | ||
* @package JBZoo\PHPUnit | ||
*/ | ||
class ConverterPmdCpdTest extends PHPUnit | ||
{ | ||
public function testToJUnit() | ||
{ | ||
$source = (new PmdCpdConverter()) | ||
->setRootPath('/Users/smetdenis/Work/projects/jbzoo-ci-report-converter') | ||
->toInternal(file_get_contents(Fixtures::PHPCPD_XML)); | ||
|
||
$actual = (new JUnitConverter())->fromInternal($source); | ||
|
||
Aliases::isValidXml($actual); | ||
isSame(Fixtures::getExpectedFileContent(), $actual); | ||
} | ||
|
||
public function testToTeamCity() | ||
{ | ||
$source = (new PmdCpdConverter()) | ||
->setRootPath('/Users/smetdenis/Work/projects/jbzoo-ci-report-converter') | ||
->toInternal(file_get_contents(Fixtures::PHPCPD_XML)); | ||
|
||
$actual = (new TeamCityTestsConverter(['show-datetime' => false], 42)) | ||
->fromInternal($source); | ||
|
||
isSame(Fixtures::getExpectedFileContent('txt'), $actual); | ||
} | ||
} |
Oops, something went wrong.