Passed
Push — master ( a93e2d...567501 )
by Philippe
03:03
created

ages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
require __DIR__.'/../vendor/autoload.php';
4
5
use PhpSpellcheck\Misspelling;
6
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
7
use PhpSpellcheck\Utils\LineAndOffset;
8
9
$phpSpellcheckLibraryNameSpellchecker = new class implements SpellcheckerInterface {
10
    public function check(
11
        string $text,
12
        array $languages = [],
13
        array $context = [],
14
        ?string $encoding = \PhpSpellcheck\Utils\TextEncoding::UTF8
15
    ): iterable {
16
        foreach (['php-spellchecker', 'php-spellcheckerer', 'php spellchecker'] as $misspelledCandidate) {
17
            $matches = [];
18
19
            if (preg_match('/\b'.$misspelledCandidate.'\b/i', $text, $matches, PREG_OFFSET_CAPTURE) !== false) {
20
                foreach ($matches as $match) {
21
                    [$word, $offset] = $match;
22
                    [$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset($text, $offset, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type null; however, parameter $encoding of PhpSpellcheck\Utils\Line...mFirstCharacterOffset() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
                    [$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset($text, $offset, /** @scrutinizer ignore-type */ $encoding);
Loading history...
23
24
                    yield new Misspelling(
25
                        $word,
26
                        $offsetFromLine,
27
                        $line,
28
                        ['PHP Spellcheck']
29
                    );
30
                }
31
            }
32
        }
33
    }
34
35
    public function getSupportedLanguages(): iterable
36
    {
37
        yield 'en_US';
38
    }
39
};
40
41
/** @var Misspelling[]|\Generator $misspellings */
42
$misspellings = $phpSpellcheckLibraryNameSpellchecker->check('The PHP-Spellcheckerer library', ['en_US']);
43
foreach ($misspellings as $misspelling) {
44
    print_r([
45
        $misspelling->getWord(), // 'PHP-Spellcheckerer'
46
        $misspelling->getLineNumber(), // '...'
47
        $misspelling->getOffset(), // '...'
48
        $misspelling->getSuggestions(), // ['PHP Spellcheck']
49
        $misspelling->getContext(), // []
50
    ]);
51
}
52