Skip to content

Commit be29dc5

Browse files
authored
Merge pull request #19 from wundii/dev
added php unit test, made small adjustments to console output and refactoring the lint config file
2 parents 1269f26 + bbe01a1 commit be29dc5

20 files changed

+514
-76
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
]);
1717

1818
$ecsConfig->skip([
19-
__DIR__ . '/tests/Failures',
19+
__DIR__ . '/tests/FaultyFiles',
2020
]);
2121

2222
$ecsConfig->parallel();

phplint.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
use PHPLint\Config\LintConfig;
66

77
return static function (LintConfig $lintConfig): void {
8-
$lintConfig->setPaths(['src', 'tests']);
8+
$lintConfig->setPaths([
9+
'src',
10+
'tests',
11+
]);
912
};

phpunit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
<testsuite name="main">
2020
<directory>tests/Main/Config</directory>
2121
<directory>tests/Main/Bootstrap</directory>
22-
<directory>tests/Main/Console</directory>
2322
<directory>tests/Main/DependencyInjection</directory>
2423
<directory>tests/Main/Finder</directory>
24+
<directory>tests/Main/Process</directory>
25+
<directory>tests/Main/Console</directory>
2526
</testsuite>
2627
</testsuites>
2728
<source>

rector.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,4 @@
4343

4444
$services = $rectorConfig->services();
4545
$services->set(ExplicitBoolCompareRector::class);
46-
47-
$rectorConfig->skip([
48-
// RemoveUselessReturnTagRector::class,
49-
// RemoveUselessParamTagRector::class,
50-
]);
5146
};

src/Config/LintConfig.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace PHPLint\Config;
66

7-
use Exception;
8-
use Symfony\Component\DependencyInjection\ContainerBuilder;
9-
107
final class LintConfig
118
{
129
private string $phpCgiExecutable = 'php';
@@ -30,10 +27,9 @@ final class LintConfig
3027

3128
private int $asyncProcess = 10;
3229

33-
public function __construct(
34-
private readonly ContainerBuilder $containerBuilder
35-
) {
36-
}
30+
private bool $allowWarning = true;
31+
32+
private bool $allowNotice = true;
3733

3834
public function getPhpCgiExecutable(): string
3935
{
@@ -151,11 +147,23 @@ public function setAsyncProcess(int $asyncProcess): void
151147
$this->asyncProcess = $asyncProcess;
152148
}
153149

154-
/**
155-
* @throws Exception
156-
*/
157-
public function getService(string $id): ?object
150+
public function isAllowWarning(): bool
151+
{
152+
return $this->allowWarning;
153+
}
154+
155+
public function setAllowWarning(bool $allowWarning): void
156+
{
157+
$this->allowWarning = $allowWarning;
158+
}
159+
160+
public function isAllowNotice(): bool
161+
{
162+
return $this->allowNotice;
163+
}
164+
165+
public function setAllowNotice(bool $allowNotice): void
158166
{
159-
return $this->containerBuilder->get($id);
167+
$this->allowNotice = $allowNotice;
160168
}
161169
}

src/Console/ConsoleColorEnum.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPLint\Console;
6+
7+
enum ConsoleColorEnum: string
8+
{
9+
case BLACK = 'black';
10+
case RED = 'red';
11+
case GREEN = 'green';
12+
case YELLOW = 'yellow';
13+
case BLUE = 'blue';
14+
case MAGENTA = 'magenta';
15+
case CYAN = 'cyan';
16+
case WHITE = 'white';
17+
case GRAY = 'gray';
18+
case BRIGHT_RED = 'bright-red';
19+
case BRIGHT_GREEN = 'bright-green';
20+
case BRIGHT_YELLOW = 'bright-yellow';
21+
case BRIGHT_BLUE = 'bright-blue';
22+
case BRIGHT_MAGENTA = 'bright-magenta';
23+
case BRIGHT_CYAN = 'bright-cyan';
24+
case BRIGHT_WHITE = 'bright-white';
25+
26+
public function getBrightEnum(): self
27+
{
28+
return match ($this) {
29+
self::RED => self::BRIGHT_RED,
30+
self::GREEN => self::BRIGHT_GREEN,
31+
self::YELLOW => self::BRIGHT_YELLOW,
32+
self::BLUE => self::BRIGHT_BLUE,
33+
self::MAGENTA => self::BRIGHT_MAGENTA,
34+
self::CYAN => self::BRIGHT_CYAN,
35+
self::WHITE => self::BRIGHT_WHITE,
36+
default => $this,
37+
};
38+
}
39+
40+
public function getBrightValue(): string
41+
{
42+
return $this->getBrightEnum()->value;
43+
}
44+
}

src/Console/Output/LintConsoleOutput.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PHPLint\Console\Output;
66

7+
use PHPLint\Console\ConsoleColorEnum;
78
use PHPLint\Process\LintProcessResult;
9+
use PHPLint\Process\StatusEnum;
810
use Symfony\Component\Console\Helper\Helper;
911
use Symfony\Component\Console\Style\SymfonyStyle;
1012

@@ -22,6 +24,8 @@ final class LintConsoleOutput
2224

2325
private bool $isSuccess = true;
2426

27+
private int $countFiles = 0;
28+
2529
public function __construct(
2630
private readonly SymfonyStyle $symfonyStyle
2731
) {
@@ -31,8 +35,13 @@ public function startApplication(string $version): void
3135
{
3236
$argv = $_SERVER['argv'] ?? [];
3337

38+
$message = sprintf(
39+
'<fg=blue;options=bold>PHP</><fg=yellow;options=bold>Lint</> %s - current PHP version: %s',
40+
$version,
41+
PHP_VERSION,
42+
);
3443
$this->symfonyStyle->writeln('> ' . implode('', $argv));
35-
$this->symfonyStyle->writeln('<fg=blue;options=bold>PHP</><fg=yellow;options=bold>Lint</> ' . $version);
44+
$this->symfonyStyle->writeln($message);
3645
$this->symfonyStyle->writeln('');
3746
}
3847

@@ -43,11 +52,11 @@ public function finishApplication(string $executionTime): bool
4352
$this->symfonyStyle->writeln(sprintf('Memory usage: %s', $usageMemory));
4453

4554
if (! $this->isSuccess) {
46-
$this->symfonyStyle->error(sprintf('Finished in %s seconds', $executionTime));
55+
$this->symfonyStyle->error(sprintf('Finished in %s', $executionTime));
4756
return true;
4857
}
4958

50-
$this->symfonyStyle->success(sprintf('Finished in %s seconds', $executionTime));
59+
$this->symfonyStyle->success(sprintf('Finished in %s', $executionTime));
5160
return false; // false means success
5261
}
5362

@@ -71,18 +80,38 @@ public function progressBarFinish(): void
7180

7281
public function messageByProcessResult(LintProcessResult $lintProcessResult): void
7382
{
74-
$line01 = sprintf('<options=bold>line %s </><fg=gray;options=bold>[%s]</>', $lintProcessResult->getLine(), $lintProcessResult->getFilename());
75-
$line02 = sprintf('<fg=bright-red>%s</>', $lintProcessResult->getResult());
83+
$consoleColorEnum = match ($lintProcessResult->getStatus()) {
84+
StatusEnum::OK => ConsoleColorEnum::GREEN,
85+
StatusEnum::NOTICE => ConsoleColorEnum::BLUE,
86+
StatusEnum::WARNING => ConsoleColorEnum::YELLOW,
87+
default => ConsoleColorEnum::RED,
88+
};
89+
90+
++$this->countFiles;
91+
92+
$line01 = sprintf(
93+
'<fg=white;options=bold>#%d - line %s </><fg=gray;options=bold>[%s]</>',
94+
$this->countFiles,
95+
$lintProcessResult->getLine(),
96+
$lintProcessResult->getFilename(),
97+
);
98+
$line02 = sprintf(
99+
'<fg=%s;options=bold>%s</>: <fg=%s>%s</>',
100+
$consoleColorEnum->getBrightValue(),
101+
ucfirst($lintProcessResult->getStatus()->value),
102+
$consoleColorEnum->value,
103+
$lintProcessResult->getResult(),
104+
);
76105

77106
$this->symfonyStyle->writeln($line01);
78107
$this->symfonyStyle->writeln($line02);
79-
$this->loadCodeSnippet($lintProcessResult->getFilename(), (int) $lintProcessResult->getLine());
108+
$this->loadCodeSnippet($lintProcessResult->getFilename(), (int) $lintProcessResult->getLine(), $consoleColorEnum);
80109
$this->symfonyStyle->newLine();
81110

82111
$this->isSuccess = false;
83112
}
84113

85-
private function loadCodeSnippet(string $filename, int $line): void
114+
private function loadCodeSnippet(string $filename, int $line, ConsoleColorEnum $consoleColorEnum): void
86115
{
87116
$lineStart = $line - self::SNIPPED_LINE;
88117
$lineEnd = $line + (self::SNIPPED_LINE - 1);
@@ -102,9 +131,22 @@ private function loadCodeSnippet(string $filename, int $line): void
102131
$lineNumberPre = substr($tmp, 0, self::LINE_LENGTH - strlen((string) $lineNumberPost));
103132

104133
if ($lineCnt + 1 === $line) {
105-
$result = sprintf('<fg=bright-red;options=bold>%s</><fg=red>%s</><fg=blue;options=bold>:</> <fg=red>%s</>', $lineNumberPre, $lineNumberPost, $contentLine);
134+
$result = sprintf(
135+
'<fg=%s;options=bold>%s</><fg=%s>%s</><fg=blue;options=bold>:</> <fg=%s>%s</>',
136+
$consoleColorEnum->getBrightValue(),
137+
$lineNumberPre,
138+
$consoleColorEnum->value,
139+
$lineNumberPost,
140+
$consoleColorEnum->value,
141+
$contentLine,
142+
);
106143
} else {
107-
$result = sprintf('<fg=gray;options=bold>%s</><fg=white>%s</><fg=blue;options=bold>:</> <fg=white>%s</>', $lineNumberPre, $lineNumberPost, $contentLine);
144+
$result = sprintf(
145+
'<fg=gray;options=bold>%s</><fg=white>%s</><fg=blue;options=bold>:</> <fg=white>%s</>',
146+
$lineNumberPre,
147+
$lineNumberPost,
148+
$contentLine,
149+
);
108150
}
109151

110152
$this->symfonyStyle->writeln($result);

src/DependencyInjection/LintContainerFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function createFromArgvInput(ArgvInput $argvInput): ContainerInterface
3636
$containerBuilder->autowire(BootstrapConfigResolver::class, BootstrapConfigResolver::class)
3737
->setPublic(true);
3838
$containerBuilder->autowire(LintConfig::class, LintConfig::class)
39-
->setPublic(true)
40-
->setArgument('$containerBuilder', $containerBuilder);
39+
->setPublic(true);
4140

4241
$phpFileLoader = new PhpFileLoader($containerBuilder, new FileLocator(__DIR__));
4342
$phpFileLoader->load(__DIR__ . '/../../config/config.php');

src/Lint/Lint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function run(): void
4141

4242
$this->lintConsoleOutput->progressBarAdvance();
4343

44-
$processes[] = new LintProcessEntity($lintProcess, $currentFile);
44+
$processes[] = new LintProcessEntity($this->lintConfig, $lintProcess, $currentFile);
4545

4646
$iterator->next();
4747
}

0 commit comments

Comments
 (0)