Aspell   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getBinaryPath() 0 3 1
A create() 0 3 1
A check() 0 23 3
A getSupportedLanguages() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Spellchecker;
6
7
use PhpSpellcheck\Exception\ProcessHasErrorOutputException;
8
use PhpSpellcheck\Utils\CommandLine;
9
use PhpSpellcheck\Utils\IspellParser;
10
use PhpSpellcheck\Utils\ProcessRunner;
11
use Symfony\Component\Process\Process;
12
use Webmozart\Assert\Assert;
13
14
class Aspell implements SpellcheckerInterface
15
{
16
    /**
17
     * @var CommandLine
18
     */
19
    private $binaryPath;
20
21 5
    public function __construct(CommandLine $binaryPath)
22
    {
23 5
        $this->binaryPath = $binaryPath;
24 5
    }
25
26
    public function check(string $text, array $languages = [], array $context = []): iterable
27
    {
28
        Assert::maxCount($languages, 1, 'Aspell spellchecker doesn\'t support multiple languages check');
29 3
30
        $cmd = $this->binaryPath->addArgs(['--encoding', 'utf-8']);
31 3
        $cmd = $cmd->addArg('-a');
32
33 3
        if (!empty($languages)) {
34
            $cmd = $cmd->addArg('--lang=' . implode(',', $languages));
35 3
        }
36 2
37
        $process = new Process($cmd->getArgs());
38
        // Add prefix characters putting Ispell's type of spellcheckers in terse-mode,
39 3
        // ignoring correct words and thus speeding up the execution
40 2
        $process->setInput('!' . PHP_EOL . IspellParser::adaptInputForTerseModeProcessing($text) . PHP_EOL . '%');
41
42
        $output = ProcessRunner::run($process)->getOutput();
43 3
44
        if ($process->getErrorOutput() !== '') {
45
            throw new ProcessHasErrorOutputException($process->getErrorOutput(), $text, $process->getCommandLine());
46 3
        }
47
48 3
        return IspellParser::parseMisspellingsFromOutput($output, $context);
49
    }
50 3
51 1
    public function getBinaryPath(): CommandLine
52
    {
53
        return $this->binaryPath;
54 2
    }
55
56
    public function getSupportedLanguages(): iterable
57
    {
58
        $languages = [];
59
        $cmd = $this->binaryPath->addArgs(['dump', 'dicts']);
60
        $process = new Process($cmd->getArgs());
61
        $output = explode(PHP_EOL, ProcessRunner::run($process)->getOutput());
62
63
        foreach ($output as $line) {
64
            $name = trim($line);
65 2
            if (strpos($name, '-variant') !== false || $name === '') {
66
                // Skip variants
67 2
                continue;
68 2
            }
69 2
            $languages[$name] = true;
70
        }
71 2
        $languages = array_keys($languages);
72 2
        \Safe\sort($languages);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sort() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-deprecated */ \Safe\sort($languages);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
73 2
74
        return $languages;
75 2
    }
76
77 2
    public static function create(?string $binaryPathAsString = null): self
78
    {
79 2
        return new self(new CommandLine($binaryPathAsString ?? 'aspell'));
80 2
    }
81
}
82