Skip to content

Commit c4586bb

Browse files
Refactor exception handling and output formatting in BcCheck. Changed BcCheckException to extend from Exception instead of RuntimeException for broader compatibility. Simplified CheckstyleOutputFormatter and TextOutputFormatter by extracting file element creation and grouped break writing into dedicated methods, enhancing code readability and maintainability.
1 parent 1b017d1 commit c4586bb

3 files changed

Lines changed: 45 additions & 27 deletions

File tree

src/Exception/BcCheckException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
namespace Phauthentic\BcCheck\Exception;
1818

19-
use RuntimeException;
19+
use Exception;
2020
use Throwable;
2121

2222
/**
2323
* Base exception class for all BC Check exceptions.
2424
*
2525
* All application-specific exceptions should extend this class.
2626
*/
27-
abstract class BcCheckException extends RuntimeException
27+
abstract class BcCheckException extends Exception
2828
{
2929
protected function __construct(string $message, ?Throwable $previous = null)
3030
{

src/Output/CheckstyleOutputFormatter.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Phauthentic\BcCheck\Output;
1818

1919
use DOMDocument;
20+
use DOMElement;
2021
use Phauthentic\BcCheck\ValueObject\BcBreak;
2122
use Symfony\Component\Console\Output\OutputInterface;
2223

@@ -40,23 +41,12 @@ public function format(array $breaks, OutputInterface $output): void
4041
$checkstyle->setAttribute('version', self::VERSION);
4142
$dom->appendChild($checkstyle);
4243

43-
// Group breaks by class name
4444
$groupedBreaks = $this->groupByClassName($breaks);
4545

4646
foreach ($groupedBreaks as $className => $classBreaks) {
47-
$file = $dom->createElement('file');
48-
$file->setAttribute('name', $className);
49-
50-
foreach ($classBreaks as $break) {
51-
$error = $dom->createElement('error');
52-
$error->setAttribute('severity', 'error');
53-
$error->setAttribute('message', $break->message);
54-
$error->setAttribute('source', 'bc-check.' . $break->type->value);
55-
56-
$file->appendChild($error);
57-
}
58-
59-
$checkstyle->appendChild($file);
47+
$checkstyle->appendChild(
48+
$this->createFileElement($dom, $className, $classBreaks),
49+
);
6050
}
6151

6252
$xml = $dom->saveXML();
@@ -65,6 +55,26 @@ public function format(array $breaks, OutputInterface $output): void
6555
}
6656
}
6757

58+
/**
59+
* @param list<BcBreak> $breaks
60+
*/
61+
private function createFileElement(DOMDocument $dom, string $className, array $breaks): DOMElement
62+
{
63+
$file = $dom->createElement('file');
64+
$file->setAttribute('name', $className);
65+
66+
foreach ($breaks as $break) {
67+
$error = $dom->createElement('error');
68+
$error->setAttribute('severity', 'error');
69+
$error->setAttribute('message', $break->message);
70+
$error->setAttribute('source', 'bc-check.' . $break->type->value);
71+
72+
$file->appendChild($error);
73+
}
74+
75+
return $file;
76+
}
77+
6878
/**
6979
* Group BC breaks by class name.
7080
*

src/Output/TextOutputFormatter.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ public function format(array $breaks, OutputInterface $output): void
4141

4242
// Group breaks by class for better readability
4343
$groupedByClass = $this->groupBreaksByClass($breaks);
44-
45-
foreach ($groupedByClass as $className => $classBreaks) {
46-
$output->writeln(sprintf('<comment> Class: %s</comment>', $className));
47-
$output->writeln('');
48-
49-
foreach ($classBreaks as $break) {
50-
$this->writeBreak($break, $output);
51-
}
52-
53-
$output->writeln('');
54-
}
44+
$this->writeGroupedBreaks($groupedByClass, $output);
5545

5646
// Summary
5747
$output->writeln($this->createSeparator(60));
@@ -68,6 +58,23 @@ public function format(array $breaks, OutputInterface $output): void
6858
$output->writeln('');
6959
}
7060

61+
/**
62+
* @param array<string, list<BcBreak>> $groupedBreaks
63+
*/
64+
private function writeGroupedBreaks(array $groupedBreaks, OutputInterface $output): void
65+
{
66+
foreach ($groupedBreaks as $className => $classBreaks) {
67+
$output->writeln(sprintf('<comment> Class: %s</comment>', $className));
68+
$output->writeln('');
69+
70+
foreach ($classBreaks as $break) {
71+
$this->writeBreak($break, $output);
72+
}
73+
74+
$output->writeln('');
75+
}
76+
}
77+
7178
private function writeBreak(BcBreak $break, OutputInterface $output): void
7279
{
7380
$typeLabel = $this->formatTypeName($break->type->value);
@@ -80,6 +87,7 @@ private function writeBreak(BcBreak $break, OutputInterface $output): void
8087
$typeLabel,
8188
$memberInfo,
8289
));
90+
8391
$output->writeln(sprintf(' %s', $break->message));
8492
}
8593

0 commit comments

Comments
 (0)