Skip to content

Commit 79195a5

Browse files
author
Reno Reckling
committed
share more code with checkstyle enricher
1 parent 578a141 commit 79195a5

File tree

2 files changed

+22
-52
lines changed

2 files changed

+22
-52
lines changed

src/generator/enricher/checkstyle/CheckStyle.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ private function processFindings(fDOMDocument $dom, \DOMNodeList $findings) {
8080
// but we have individual objects - so we just ignore the finding for this context
8181
continue;
8282
}
83+
$this->processFinding($dom, $ref, $finding);
84+
}
8385

84-
$enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle');
85-
$enrichFinding = $dom->createElementNS(self::XMLNS, $finding->getAttribute('severity', 'error'));
86-
$enrichment->appendChild($enrichFinding);
87-
foreach($finding->attributes as $attr) {
88-
if ($attr->localName == 'severity') {
89-
continue;
90-
}
91-
$enrichFinding->setAttributeNode($dom->importNode($attr, true));
92-
}
86+
}
9387

88+
protected function processFinding(fDOMDocument $dom, $ref, \DOMElement $finding) {
89+
$enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle');
90+
$enrichFinding = $dom->createElementNS(self::XMLNS, $finding->getAttribute('severity', 'error'));
91+
$enrichment->appendChild($enrichFinding);
92+
foreach($finding->attributes as $attr) {
93+
if ($attr->localName == 'severity') {
94+
continue;
95+
}
96+
$enrichFinding->setAttributeNode($dom->importNode($attr, true));
9497
}
95-
9698
}
9799
}
98100

src/generator/enricher/phpcs/PHPCs.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use TheSeer\phpDox\Generator\InterfaceStartEvent;
1111
use TheSeer\phpDox\Generator\TraitStartEvent;
1212

13-
class PHPCs extends AbstractEnricher implements ClassEnricherInterface, TraitEnricherInterface, InterfaceEnricherInterface {
13+
class PHPCs extends CheckStyle {
1414

1515
private $config;
1616
private $findings = NULL;
@@ -28,25 +28,6 @@ public function getName() {
2828
return 'PHPCS XML';
2929
}
3030

31-
public function enrichClass(ClassStartEvent $event) {
32-
$this->enrichUnit($event->getClass());
33-
}
34-
35-
public function enrichInterface(InterfaceStartEvent $event) {
36-
$this->enrichUnit($event->getInterface());
37-
}
38-
39-
public function enrichTrait(TraitStartEvent $event) {
40-
$this->enrichUnit($event->getTrait());
41-
}
42-
43-
private function enrichUnit(AbstractUnitObject $ctx) {
44-
$file = $ctx->getSourceFile();
45-
if (isset($this->findings[$file])) {
46-
$this->processFindings($ctx->asDom(), $this->findings[$file]);
47-
}
48-
}
49-
5031
private function loadFindings($xmlFile) {
5132
$this->findings = array();
5233
try {
@@ -69,31 +50,18 @@ private function loadFindings($xmlFile) {
6950
}
7051
}
7152

72-
private function processFindings(fDOMDocument $dom, \DOMNodeList $findings) {
73-
74-
foreach($findings as $finding) {
75-
/** @var fDOMElement $finding */
76-
$line = $finding->getAttribute('line');
77-
$ref = $dom->queryOne(sprintf('//phpdox:*/*[@line = %d or (@start <= %d and @end >= %d)]', $line, $line, $line));
78-
if (!$ref) {
79-
// One src file may contain multiple classes/traits/interfaces, so the
80-
// finding might not apply to the current object since findings are based on filenames
81-
// but we have individual objects - so we just ignore the finding for this context
53+
protected function processFinding(fDOMDocument $dom, $ref, \DOMElement $finding)
54+
{
55+
$enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle');
56+
$enrichFinding = $dom->createElementNS(self::XMLNS, $finding->tagName);
57+
$enrichment->appendChild($enrichFinding);
58+
foreach($finding->attributes as $attr) {
59+
if ($attr->localName == 'severity') {
8260
continue;
8361
}
84-
85-
$enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle');
86-
$enrichFinding = $dom->createElementNS(self::XMLNS, $finding->tagName);
87-
$enrichment->appendChild($enrichFinding);
88-
foreach($finding->attributes as $attr) {
89-
if ($attr->localName == 'severity') {
90-
continue;
91-
}
92-
$enrichFinding->setAttributeNode($dom->importNode($attr, true));
93-
}
94-
$enrichFinding->setAttribute('message', $finding->nodeValue);
62+
$enrichFinding->setAttributeNode($dom->importNode($attr, true));
9563
}
96-
64+
$enrichFinding->setAttribute('message', $finding->nodeValue);
9765
}
9866
}
9967

0 commit comments

Comments
 (0)