LanguageTool   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSupportedLanguages() 0 3 1
A check() 0 36 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Spellchecker;
6
7
use PhpSpellcheck\Exception\RuntimeException;
8
use PhpSpellcheck\Misspelling;
9
use PhpSpellcheck\Spellchecker\LanguageTool\LanguageToolApiClient;
10
use PhpSpellcheck\Utils\LineAndOffset;
11
use Webmozart\Assert\Assert;
12
13
class LanguageTool implements SpellcheckerInterface
14
{
15
    /**
16
     * @var LanguageToolApiClient
17
     */
18
    private $apiClient;
19
20 4
    public function __construct(LanguageToolApiClient $apiClient)
21
    {
22 4
        $this->apiClient = $apiClient;
23 4
    }
24
25
    /**
26
     * @param array<mixed> $context
27
     * @param array<string> $languages
28 2
     *
29
     * @return \Generator<Misspelling>
30
     */
31
    public function check(
32
        string $text,
33
        array $languages,
34 2
        array $context
35
    ): iterable {
36 2
        Assert::notEmpty($languages, 'LanguageTool requires at least one language to be set to run it\'s spellchecking process');
37 2
38
        if (isset($context[self::class])) {
39 2
            Assert::isArray($context[self::class]);
40 2
            /** @var array<mixed> $options */
41
            $options = $context[self::class];
42 2
        }
43 2
        $check = $this->apiClient->spellCheck($text, $languages, $options ?? []);
44 2
45 2
        if (!\is_array($check['matches'])) {
46 2
            throw new RuntimeException('LanguageTool spellcheck response must contain a "matches" array');
47 2
        }
48
49 2
        foreach ($check['matches'] as $match) {
50 2
            [$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset(
51 2
                $text,
52
                $match['offset']
53 2
            );
54
55
            yield new Misspelling(
56
                mb_substr($match['context']['text'], $match['context']['offset'], $match['context']['length']),
57 2
                $offsetFromLine,
58
                $line, // line break index transformed in line number
59
                array_column($match['replacements'], 'value'),
60
                array_merge(
61
                    [
62 2
                        'sentence' => $match['sentence'],
63
                        'spellingErrorMessage' => $match['message'],
64 2
                        'ruleUsed' => $match['rule'],
65
                    ],
66
                    $context
67 2
                )
68
            );
69 2
        }
70 2
    }
71 2
72 2
    /**
73 2
     * @return array<string>
74
     */
75
    public function getSupportedLanguages(): array
76 2
    {
77
        return $this->apiClient->getSupportedLanguages();
78 2
    }
79
}
80