Skip to content

Commit

Permalink
fix: misspelling should still be seen as a misspelling when no sugges…
Browse files Browse the repository at this point in the history
…tions are returned

Signed-off-by: Ben James <[email protected]>
  • Loading branch information
benjam-es committed Jan 18, 2025
1 parent 6854cbf commit 7629314
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/Console/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ private function renderLineIssue(Issue $issue): void
$issue,
);

$suggestionsHelperText = ($suggestions > '') ? 'Did you mean:' : 'No suggestions found.';

render(<<<HTML
<div class="mx-2 mb-1">
<div class="space-x-1">
Expand All @@ -208,7 +210,7 @@ private function renderLineIssue(Issue $issue): void
</div>
<div class="space-x-1 text-gray-700">
<span>Did you mean:</span>
<span>{$suggestionsHelperText}</span>
<span class="font-bold">{$suggestions}</span>
</div>
</div>
Expand Down Expand Up @@ -272,6 +274,8 @@ private function renderLineLessIssue(Issue $issue): void
$issue,
);

$suggestionsHelperText = ($suggestions > '') ? 'Did you mean:' : 'No suggestions found.';

render(<<<HTML
<div class="mx-2 mb-1">
<div class="space-x-1">
Expand All @@ -282,7 +286,7 @@ private function renderLineLessIssue(Issue $issue): void
</div>
<div class="space-x-1 text-gray-700">
<span>Did you mean:</span>
<span>{$suggestionsHelperText}</span>
<span class="font-bold">{$suggestions}</span>
</div>
</div>
Expand Down
43 changes: 37 additions & 6 deletions src/Services/Spellcheckers/Aspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function check(string $text): array
*/
private function getMisspellings(string $text): array
{
$misspellings = iterator_to_array($this->run($text));
$misspellings = $this->run($text);

$this->cache->set($text, $misspellings);

Expand Down Expand Up @@ -101,14 +101,45 @@ private function run(string $text): array

$output = $process->getOutput();

return array_values(array_map(function (string $line): Misspelling {
[$wordMetadataAsString, $suggestionsAsString] = explode(':', trim($line));
$lines = $this->getAspellLinesFromOutput($output);

return $this->getMisspellingsFromOutputLines($lines);
}

/**
* Gets Aspell output lines as an array
*
* @return array<int, string>
*/
private function getAspellLinesFromOutput(string $output): array
{
return array_values(
array_filter(
explode(PHP_EOL, trim($output)), fn (string $line): bool => ! str_starts_with($line, '@(#)') && $line !== '*'
)
);
}

$word = explode(' ', $wordMetadataAsString)[1];
$suggestions = explode(', ', trim($suggestionsAsString));
/**
* Maps output lines to an array of Misspellings if suggestions exist, null if no suggestions or an empty array if spelling is correct
*
* @param array<int, string> $lines
* @return array <int, Misspelling>
*/
private function getMisspellingsFromOutputLines(array $lines): array
{
return array_values(array_map(function (string $line): Misspelling {
if (preg_match('/#\\s(\\w*)\\sd*/', $line, $matches)) {
$word = $matches[1];
$suggestions = [];
} else {
[$wordMetadataAsString, $suggestionsAsString] = explode(':', trim($line));
$word = explode(' ', $wordMetadataAsString)[1];
$suggestions = explode(', ', trim($suggestionsAsString));
}

return new Misspelling($word, $this->takeSuggestions($suggestions));
}, array_filter(explode(PHP_EOL, $output), fn (string $line): bool => str_starts_with($line, '&'))));
}, $lines));
}

/**
Expand Down
9 changes: 7 additions & 2 deletions tests/.pest/snapshots/Console/OutputTest/it_may_fail.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.............⨯⨯⨯.⨯....⨯⨯.⨯⨯⨯⨯⨯⨯....⨯⨯
.............⨯⨯⨯.⨯......⨯⨯.⨯⨯⨯⨯⨯⨯....⨯⨯

Misspelling ./tests/Fixtures/FolderWithTypoos: 'typoos'
./tests/Fixtures/FolderWithTypoos
Expand Down Expand Up @@ -185,7 +185,12 @@
---------------------^
Did you mean: constant, constants, consonant, consent

FAIL 37 misspelling(s) found in your project.
Misspelling ./tests/Fixtures/TypoNoSuggestions/ClassWithTypoErrors.php:16:48: 'xxxxxxxxxxxxxxxxxxxx'
16▕  public int $propertyWithTypoAndNoSuggestionsXxxxxxxxxxxxxxxxxxxx = 1;
------------------------------------------------^
No suggestions found.

FAIL 38 misspelling(s) found in your project.

Duration: 0.00s
Hint: You may correct the misspellings individually, ignore them one by one, or ignore all of them using the peck --ignore-all option.
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/TypoNoSuggestions/ClassWithTypoErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\TypoNoSuggestions;

/**
* Class ClassWithTypoErrors
*
* This class is used to test misspellings without suggestions.
*
* @internal
*/
final class ClassWithTypoErrors
{
public int $propertyWithTypoAndNoSuggestionsXxxxxxxxxxxxxxxxxxxx = 1;
}
20 changes: 20 additions & 0 deletions tests/Unit/Checkers/SourceCodeCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@
expect($issues)->toBeEmpty();
});

it('detects issue and returns as fail when there are no suggestions', function (): void {
$checker = new SourceCodeChecker(
Config::instance(),
Aspell::default(),
);

$issues = $checker->check([
'directory' => __DIR__.'/../../Fixtures/TypoNoSuggestions',
'onSuccess' => fn (): null => null,
'onFailure' => fn (): null => null,
]);

expect($issues)->toHaveCount(1)
->and($issues[0]->file)->toEndWith('tests/Fixtures/TypoNoSuggestions/ClassWithTypoErrors.php')
->and($issues[0]->line)->toBe(16)
->and($issues[0]->misspelling->word)->toBe('xxxxxxxxxxxxxxxxxxxx')
->and($issues[0]->misspelling->suggestions)->toBe([]);

});

it('detects issues in the given directory of classes', function (): void {
$checker = new SourceCodeChecker(
Config::instance(),
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
'onFailure' => fn (): null => null,
]);

expect($issues)->toHaveCount(37);
expect($issues)->toHaveCount(38);
});
14 changes: 14 additions & 0 deletions tests/Unit/Services/AspellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Peck\Config;
use Peck\Plugins\Cache;
use Peck\Services\Spellcheckers\Aspell;
use Peck\ValueObjects\Misspelling;

it('does not detect issues', function (): void {
$spellchecker = Aspell::default();
Expand Down Expand Up @@ -91,3 +92,16 @@

expect($issues)->not->toBeEmpty();
});

it('reports a bad spelling even when there are no suggestions', function (): void {
Cache::default()->flush();
$spellchecker = Aspell::default();

$issues = $spellchecker->check('ppppppppppppppooihihihihih');

expect($issues)->toBeArray()
->and($issues[0]->word)->toBe('ppppppppppppppooihihihihih')
->and($issues[0]->suggestions)->toBe([])
->and($issues[0])->toBeInstanceOf(Misspelling::class);

});

0 comments on commit 7629314

Please sign in to comment.