Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: misspelling should still be recognised as a misspelling when no suggestions are returned #121

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benjam-es
Copy link
Contributor

@benjam-es benjam-es commented Jan 18, 2025

What:

  • Bug Fix

Description:

Fixes #100

When a misspelling occurs, but no suggestions are found, the spellchecker returns as a pass. This is incorrect as the misspelling should still be reported.

Screenshot 2025-01-18 at 14 53 38

results in:

Screenshot 2025-01-18 at 14 54 54

With this PR:

Screenshot 2025-01-18 at 14 54 39

Related:

Issue #100
Also just found #101

@benjam-es benjam-es force-pushed the fix/misspelling-no-suggestions branch from 40291f7 to 7629314 Compare January 18, 2025 15:00
@c0nst4ntin c0nst4ntin added duplicate This issue or pull request already exists bug Something isn't working labels Jan 19, 2025
Copy link
Collaborator

@c0nst4ntin c0nst4ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjam-es I reviewed the changes. Generally, I think this is a good contribution. I just added some feedback where I see some problems with your approach.

Please take a look the the feedback and suggestions. I really like that you added good tests for your changes!

@@ -198,6 +198,8 @@ private function renderLineIssue(Issue $issue): void
$issue,
);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer checking directly for "not equals" or am I missing something?

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

@@ -272,6 +274,8 @@ private function renderLineLessIssue(Issue $issue): void
$issue,
);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment ^^

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

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we really drop this conversion at this position?
Then it would be serialized as an iterator and returned as an iterator from the cache.

This would cause the is_array() call to always be false:

/** @var array<int, Misspelling>|null $misspellings */
$misspellings = $this->cache->has($text) ? $this->cache->get($text) : $this->getMisspellings($text);
if (! is_array($misspellings)) {
$misspellings = $this->getMisspellings($text);
}

Comment on lines +104 to +106
$lines = $this->getAspellLinesFromOutput($output);

return $this->getMisspellingsFromOutputLines($lines);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the variable and can pass this directly:

Suggested change
$lines = $this->getAspellLinesFromOutput($output);
return $this->getMisspellingsFromOutputLines($lines);
return $this->getMisspellingsFromOutputLines(
$this->getAspellLinesFromOutput($output)
);

Comment on lines +132 to +139
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));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to, we could turn this if-else block into two match() statements.

This way we do not rely on the variables from inside the if block in the return statement and clearly separate the aggregation of the $word and the $suggestions.

Suggested change
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));
}
$word = match (true) {
preg_match('/#\\s(\\w*)\\sd*/', $line, $matches) => $matches[1],
default => explode(' ', explode(':', trim($line))[0])[1],
};
$suggestions = match (true) {
preg_match('/#\\s(\\w*)\\sd*/', $line, $matches) => [],
default => explode(', ', trim(explode(':', trim($line))[1])),
};

@c0nst4ntin c0nst4ntin marked this pull request as draft January 21, 2025 20:19
@c0nst4ntin
Copy link
Collaborator

I marked this as a draft for now until we discussed and implemented some of the feedback I gave you.
If you belive this request is ready for another review or to be merged, please open it back up for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Aspell class not returning when there are no suggestions
2 participants