Skip to content

Commit 865fb5d

Browse files
authored
Final test coverage sweep (#42)
* test: drop redundancy to avoid coverage gymnastics * test: suppressed native warning * test: added coverage for UpstreamResolver * test: added early return * test: increased coverage * test: increased edge case coverage * test: added cwd fallback * test: added fixer mapping coverage * test: removed obsolete todos ref #33 (comment) * test: simplified simpara source matching * test: covered declaration masking * test: covered valid indentation styles * test: covered exception name mismatch * test: covered report edge cases * test: covered input all paths * test: covered diff edge cases * test: covered scope edge cases * test: covered noop fix edge cases * test: covered fixer input edge cases * test: academics * test: academics * test: covered processing failure paths * test: removed @api; now covered by tests * review: removed yoda style
1 parent 7c30881 commit 865fb5d

34 files changed

Lines changed: 894 additions & 93 deletions

src/Diff/UpstreamResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public function resolve(string $repoRoot, string $repoName): ?string
4848
}
4949

5050
try {
51+
// filesystem or OS edge cases
5152
if (!flock($lock, LOCK_EX)) {
52-
return null;
53+
return null; // @codeCoverageIgnore
5354
}
5455

5556
return $this->refreshAndResolve($repoRoot, $repoName);

src/Path/EntityResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ private function scanDirectory(string $directory): array
123123
*/
124124
private function resolveFile(string $filePath, array &$visited, array &$paths, ?string $originEntity = null): array
125125
{
126-
if (isset($visited[$filePath]) || !is_readable($filePath)) {
126+
if (isset($visited[$filePath])) {
127127
return [];
128128
}
129129

130130
$visited[$filePath] = true;
131131

132-
$content = file_get_contents($filePath);
132+
$content = @file_get_contents($filePath);
133133

134134
if ($content === false) {
135-
return []; // @codeCoverageIgnore
135+
return [];
136136
}
137137

138138
$entities = $this->extractEntities($content, $filePath, $visited, $paths);

src/Process/NativeProcessRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class NativeProcessRunner implements ProcessRunnerInterface
88
{
99
public function run(array $command, string $workingDirectory, array $environment = []): ProcessResult
1010
{
11-
$process = proc_open(
11+
$process = @proc_open(
1212
$command,
1313
[
1414
['pipe', 'r'],

src/Progress/ConsoleProgress.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ public function finish(): void
6767

6868
private function drawBar(int $current, string $filePath, int $violations = 0): void
6969
{
70-
$percent = $this->totalFiles > 0
71-
? (int)floor(($current / $this->totalFiles) * 100)
72-
: 0; // @codeCoverageIgnore
70+
if ($this->totalFiles === 0) {
71+
// prevented by public methods
72+
return; // @codeCoverageIgnore
73+
}
7374

74-
$filled = $this->totalFiles > 0
75-
? (int)floor(($current / $this->totalFiles) * self::BAR_WIDTH)
76-
: 0; // @codeCoverageIgnore
75+
$ratio = $current / $this->totalFiles;
76+
$percent = (int)floor($ratio * 100);
77+
$filled = (int)floor($ratio * self::BAR_WIDTH);
7778

7879
$empty = self::BAR_WIDTH - $filled;
7980

src/RelativePath.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ final class RelativePath
88
{
99
public static function fromWorkingDirectory(string $filePath): string
1010
{
11-
$workingDirectory = getcwd();
12-
if ($workingDirectory === false) {
13-
return $filePath; // @codeCoverageIgnore
14-
}
15-
11+
$workingDirectory = getcwd() ?: '.';
1612
$prefix = rtrim(str_replace('\\', '/', $workingDirectory), '/') . '/';
1713
$normalisedPath = str_replace('\\', '/', $filePath);
1814

src/Report/Report.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function getTotalWarningLevelViolationCount(): int
187187
));
188188
}
189189

190-
/** @api not implemented */
190+
// not implemented
191191
public function getTotalInfoLevelViolationCount(): int
192192
{
193193
return array_sum(array_map(

src/Report/Reporter/ConsoleReporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ private function collectPerformanceRows(Report $report): array
152152

153153
private function formatSeverity(Severity $severity): string
154154
{
155-
return match ($severity) { // @codeCoverageIgnore
155+
return match ($severity) {
156156
Severity::ERROR => $this->red(str_pad(Severity::ERROR->name, 7)),
157157
Severity::WARNING => $this->yellow(str_pad(Severity::WARNING->name, 7)),
158158
default => $this->dim(str_pad(strtoupper($severity->name), 7)),
159-
}; // @codeCoverageIgnore
159+
};
160160
}
161161

162162
private function formatPerformanceCell(?float $time, float $totalTime): string

src/Runner/RunScopeResolver.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,10 @@ private function absolutePaths(array $paths): array
105105
private function expandReferencedTargets(array &$targets): void
106106
{
107107
$pending = array_keys($targets);
108-
$visitedFiles = [];
109108
$visitedEntityPaths = [];
110109

111110
for ($i = 0; isset($pending[$i]); $i++) {
112111
$file = $pending[$i];
113-
114-
if (isset($visitedFiles[$file])) {
115-
continue;
116-
}
117-
118-
$visitedFiles[$file] = true;
119112
$content = @file_get_contents($file);
120113

121114
if ($content === false) {

src/Sniff/SimparaSniff.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public function process(\DOMDocument $document, File $file): array
144144
throw new \LogicException('Could not map simpara violation to source content.');
145145
}
146146

147-
if ($match['selfClosing']) {
147+
$closingOffset = $match['closingOffset'];
148+
if (null === $closingOffset) {
148149
continue;
149150
}
150151

@@ -160,11 +161,6 @@ public function process(\DOMDocument $document, File $file): array
160161
continue;
161162
}
162163

163-
$closingOffset = $match['closingOffset'];
164-
if ($closingOffset === null) {
165-
throw new \LogicException('Could not map simpara violation to source content.');
166-
}
167-
168164
$affectedRanges = $this->elementNameRanges(
169165
$file,
170166
$match['beginOffset'],
@@ -219,13 +215,7 @@ private function getAllowedElements(): array
219215
|> array_values(...);
220216
}
221217

222-
/**
223-
* @return list<array{
224-
* beginOffset: int,
225-
* selfClosing: bool,
226-
* closingOffset: int|null
227-
* }>
228-
*/
218+
/** @return list<array{beginOffset: int, closingOffset: int|null}> */
229219
private function sourceMatches(File $file): array
230220
{
231221
preg_match_all(
@@ -245,7 +235,6 @@ private function sourceMatches(File $file): array
245235
if (str_ends_with(rtrim($tag), '/>')) {
246236
$sourceMatches[] = [
247237
'beginOffset' => $offset,
248-
'selfClosing' => true,
249238
'closingOffset' => null,
250239
];
251240
continue;
@@ -262,7 +251,6 @@ private function sourceMatches(File $file): array
262251

263252
$sourceMatches[] = [
264253
'beginOffset' => $opening,
265-
'selfClosing' => false,
266254
'closingOffset' => $offset,
267255
];
268256
}

tests/Support/Sniff/ExposedAbstractSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public function exposeSeverity(): Severity
2929
{
3030
return $this->severity;
3131
}
32+
33+
public function exposeMaskNonElementMarkup(string $source): string
34+
{
35+
return $this->maskNonElementMarkup($source);
36+
}
3237
}

0 commit comments

Comments
 (0)