Failed Conditions
Push — master ( 58748a...c61078 )
by Philippe
61:45
created

aspell_console_output.php$0 ➔ process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
use PhpSpellCheck\MisspellingFinder;
4
use PhpSpellCheck\MisspellingHandler\EchoHandler;
5
use PhpSpellCheck\SpellChecker\Aspell;
6
use PhpSpellCheck\TextInterface;
7
use PhpSpellCheck\TextProcessor\TextProcessorInterface;
8
9
require_once __DIR__ . '/../vendor/autoload.php';
10
11
// *** Using spellcheckers directly ***
12
$aspell = Aspell::create(); // Creates aspell spellchecker pointing to "aspell" as it's binary path
13
/** @var \PhpSpellCheck\Misspelling[] $misspellings */
14
$misspellings = $aspell->check('mispell', ['en_US']); // $misspellings is a \Generator here
15
foreach ($misspellings as $misspelling) {
16
    $misspelling->getWord();
17
    $misspelling->getLineNumber();
18
    $misspelling->getOffset();
19
    $misspelling->getSuggestions();
20
    $misspelling->getContext();
21
}
22
23
// *** Using MisspellingFinder class to orchestrate spellchecking flow  ***
24
// My custom text processor that replaces "_" by " "
25
$customTextProcessor = new class implements TextProcessorInterface
26
{
27
    public function process(TextInterface $text): TextInterface
28
    {
29
        $contentProcessed = str_replace('_', ' ', $text->getContent());
30
31
        return $text->replaceContent($contentProcessed);
32
    }
33
};
34
35
$misspellingFinder = new MisspellingFinder(
36
    Aspell::create(), // Creates aspell spellchecker pointing to "aspell" as it's binary path
37
    new EchoHandler(), // Handles all the misspellings found by echoing their information
38
    $customTextProcessor
39
);
40
41
$misspellingFinder->find('It\'s_a_mispelling', ['en_US']); // Misspellings are echoed
42