anonymous//examples/custom_spellchecker.php$0   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A custom_spellchecker.php$0 ➔ getSupportedLanguages() 0 3 1
A custom_spellchecker.php$0 ➔ check() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
require __DIR__.'/../vendor/autoload.php';
6
7
use PhpSpellcheck\Misspelling;
8
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
9
use PhpSpellcheck\Utils\LineAndOffset;
10
11
$phpSpellcheckLibraryNameSpellchecker = new class () implements SpellcheckerInterface {
12
    public function check(
13
        string $text,
14
        array $languages,
15
        array $context
16
    ): iterable {
17
        foreach (['php-spellchecker', 'php-spellcheckerer', 'php spellchecker'] as $misspelledCandidate) {
18
            $matches = [];
19
20
            if (preg_match('/\b'.$misspelledCandidate.'\b/i', $text, $matches, PREG_OFFSET_CAPTURE) !== false) {
21
                foreach ($matches as $match) {
22
                    [$word, $offset] = $match;
23
                    [$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset($text, $offset);
24
25
                    yield new Misspelling(
26
                        $word,
27
                        $offsetFromLine,
28
                        $line,
29
                        ['PHP Spellcheck']
30
                    );
31
                }
32
            }
33
        }
34
    }
35
36
    public function getSupportedLanguages(): iterable
37
    {
38
        yield 'en_US';
39
    }
40
};
41
42
/** @var \Generator|Misspelling[] $misspellings */
43
$misspellings = $phpSpellcheckLibraryNameSpellchecker->check('The PHP-Spellcheckerer library', ['en_US'], ['context']);
44
foreach ($misspellings as $misspelling) {
45
    print_r([
46
        $misspelling->getWord(), // 'PHP-Spellcheckerer'
47
        $misspelling->getLineNumber(), // '...'
48
        $misspelling->getOffset(), // '...'
49
        $misspelling->getSuggestions(), // ['PHP Spellcheck']
50
        $misspelling->getContext(), // []
51
    ]);
52
}
53