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

anonymous//example/aspell_console_output.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A aspell_console_output.php$0 ➔ process() 0 5 1
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