Skip to content

Commit e512347

Browse files
authored
Output::class (#72)
`Output` is the class responsible of outputs. #### Available Methods - static successMessage(string $message). - static errorMessage(string $message). - static pass(string $path). - static fail(string $path, string $feedback).
1 parent a7d3605 commit e512347

File tree

2 files changed

+67
-59
lines changed

2 files changed

+67
-59
lines changed

src/Output.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\Profanity;
6+
7+
use function Termwind\render;
8+
use function Termwind\terminal;
9+
10+
/**
11+
* @internal
12+
*/
13+
class Output
14+
{
15+
public static function successMessage(string $message): void
16+
{
17+
render(<<<HTML
18+
<div class="my-1">
19+
<span class="ml-2 px-1 bg-green font-bold">PASS</span>
20+
<span class="ml-1">$message</span>
21+
</div>
22+
HTML);
23+
}
24+
25+
public static function errorMessage(string $message): void
26+
{
27+
render(<<<HTML
28+
<div class="my-1">
29+
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
30+
<span class="ml-1">$message</span>
31+
</div>
32+
HTML);
33+
}
34+
35+
public static function pass(string $path): void
36+
{
37+
$truncateAt = max(1, terminal()->width() - 24);
38+
39+
render(<<<HTML
40+
<div class="flex mx-2">
41+
<span class="truncate-{$truncateAt}">$path</span>
42+
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
43+
<span class="text-green">OK</span>
44+
</div>
45+
HTML);
46+
}
47+
48+
public static function fail(string $path, string $feedback): void
49+
{
50+
$truncateAt = max(1, terminal()->width() - 24);
51+
52+
render(<<<HTML
53+
<div class="flex mx-2">
54+
<span class="truncate-{$truncateAt}">{$path}</span>
55+
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
56+
<span class="text-red">$feedback</span>
57+
</div>
58+
HTML);
59+
}
60+
}

src/Plugin.php

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use Symfony\Component\Console\Output\OutputInterface;
1515
use Symfony\Component\Finder\Finder;
1616

17-
use function Termwind\render;
1817
use function Termwind\renderUsing;
19-
use function Termwind\terminal;
2018

2119
/**
2220
* @internal
@@ -118,15 +116,7 @@ public function handleOriginalArguments(array $arguments): void
118116
$outputPath = explode('=', $argument)[1] ?? null;
119117

120118
if (empty($outputPath)) {
121-
render(<<<'HTML'
122-
<div class="my-1">
123-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
124-
<span class="ml-1">
125-
No output path provided for [--profanity-json].
126-
</span>
127-
</div>
128-
HTML);
129-
119+
Output::errorMessage('No output path provided for [--profanity-json].');
130120
$this->exit(1);
131121
}
132122

@@ -137,14 +127,7 @@ public function handleOriginalArguments(array $arguments): void
137127
$invalidLanguages = $this->validateLanguages($this->language);
138128
if (! empty($invalidLanguages)) {
139129
$invalidLangsStr = implode(', ', $invalidLanguages);
140-
render(<<<HTML
141-
<div class="my-1">
142-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
143-
<span class="ml-1">
144-
The specified language does not exist: {$invalidLangsStr}
145-
</span>
146-
</div>
147-
HTML);
130+
Output::errorMessage("The specified language does not exist: $invalidLangsStr");
148131

149132
$this->output->writeln(['']);
150133
$this->output->writeln('<info>Available languages:</info>');
@@ -163,14 +146,7 @@ public function handleOriginalArguments(array $arguments): void
163146
$source = ConfigurationSourceDetector::detect();
164147

165148
if ($source === []) {
166-
render(<<<'HTML'
167-
<div class="my-1">
168-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
169-
<span class="ml-1">
170-
No source section found. Did you forget to add a `source` section to your `phpunit.xml` file?
171-
</span>
172-
</div>
173-
HTML);
149+
Output::errorMessage('No source section found. Did you forget to add a `source` section to your `phpunit.xml` file?');
174150

175151
$this->exit(1);
176152
}
@@ -192,18 +168,10 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
192168
$path = str_replace(TestSuite::getInstance()->rootPath.'/', '', $result->file);
193169
$errors = $result->errors;
194170

195-
$truncateAt = max(1, terminal()->width() - 24);
196-
197171
if (empty($errors)) {
198172
if (! $this->compact) {
199173
renderUsing($this->output);
200-
render(<<<HTML
201-
<div class="flex mx-2">
202-
<span class="truncate-{$truncateAt}">{$path}</span>
203-
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
204-
<span class="text-green">OK</span>
205-
</div>
206-
HTML);
174+
Output::pass($path);
207175

208176
$this->profanityLogger->append($path, []);
209177
}
@@ -223,13 +191,7 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
223191
$profanityLines = implode(', ', $profanityLines);
224192

225193
renderUsing($this->output);
226-
render(<<<HTML
227-
<div class="flex mx-2">
228-
<span class="truncate-{$truncateAt}">{$path}</span>
229-
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
230-
<span class="text-red">{$profanityLines}</span>
231-
</div>
232-
HTML);
194+
Output::fail($path, $profanityLines);
233195
}
234196
},
235197
$this->excludeWords,
@@ -243,23 +205,9 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
243205
$this->profanityLogger->output();
244206

245207
if ($exitCode === 1) {
246-
render(<<<HTML
247-
<div class="my-1">
248-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
249-
<span class="ml-1">
250-
Found {$totalProfanities} instances of profanity in {$filesWithProfanityCount} files
251-
</span>
252-
</div>
253-
HTML);
208+
Output::errorMessage("Found $totalProfanities instances of profanity in $filesWithProfanityCount files");
254209
} else {
255-
render(<<<'HTML'
256-
<div class="my-1">
257-
<span class="ml-2 px-1 bg-green font-bold">PASS</span>
258-
<span class="ml-1">
259-
No profanity found in your application!
260-
</span>
261-
</div>
262-
HTML);
210+
Output::successMessage('No profanity found in your application!');
263211
}
264212

265213
$this->output->writeln(['']);

0 commit comments

Comments
 (0)