Passed
Push — master ( 1b968b...2baf19 )
by Philippe
01:56
created

MisspellingFinder::doSpellCheckTexts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck;
6
7
use PhpSpellcheck\Exception\InvalidArgumentException;
8
use PhpSpellcheck\MisspellingHandler\MisspellingHandlerInterface;
9
use PhpSpellcheck\Source\SourceInterface;
10
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
11
use PhpSpellcheck\TextProcessor\TextProcessorInterface;
12
13
class MisspellingFinder
14
{
15
    /**
16
     * @var SpellcheckerInterface
17
     */
18
    private $spellChecker;
19
20
    /**
21
     * @var MisspellingHandlerInterface|null
22
     */
23
    private $misspellingHandler;
24
25
    /**
26
     * @var TextProcessorInterface|null
27
     */
28
    private $textProcessor;
29
30
    public function __construct(
31
        SpellcheckerInterface $spellChecker,
32 5
        ?MisspellingHandlerInterface $misspellingHandler = null,
33
        ?TextProcessorInterface $textProcessor = null
34
    ) {
35
        $this->spellChecker = $spellChecker;
36
        $this->misspellingHandler = $misspellingHandler;
37 5
        $this->textProcessor = $textProcessor;
38 5
    }
39 5
40 5
    /**
41
     * @param SourceInterface|string|TextInterface|TextInterface[] $source
42
     *
43
     * @return MisspellingInterface[]
44
     */
45
    public function find(
46
        $source,
47 5
        array $languages = [],
48
        array $context = []
49
    ): iterable {
50
        if (is_string($source)) {
51
            $texts = [t($source, $context)];
52
        } elseif ($source instanceof TextInterface) {
53 5
            $texts = [$source];
54
        } elseif (is_array($source)) {
55 4
            $texts = $source;
56 1
        } elseif ($source instanceof SourceInterface) {
0 ignored issues
show
introduced by
$source is always a sub-type of PhpSpellcheck\Source\SourceInterface.
Loading history...
57
            $texts = $source->toTexts($context);
58
        } else {
59 4
            $sourceVarType = is_object($source) ? get_class($source) : gettype($source);
60
            $allowedTypes = implode(' or ', ['"string"', '"' . SourceInterface::class . '"', '"' . TextInterface::class . '[]"', '"' . TextInterface::class . '"']);
61
62
            throw new InvalidArgumentException('Source should be of type ' . $allowedTypes . ', "' . $sourceVarType . '" given');
63
        }
64
65
        $misspellings = $this->doSpellCheckTexts($texts, $languages);
66
67 5
        if ($this->misspellingHandler !== null) {
68
            $this->misspellingHandler->handle($misspellings);
69 5
        }
70 4
71
        return $misspellings;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $misspellings returns the type Generator which is incompatible with the documented return type PhpSpellcheck\MisspellingInterface[].
Loading history...
72
    }
73 5
74 4
    public function setSpellchecker(SpellcheckerInterface $spellChecker): void
75
    {
76
        $this->spellChecker = $spellChecker;
77 1
    }
78
79 1
    public function setMisspellingHandler(MisspellingHandlerInterface $misspellingHandler): void
80
    {
81
        $this->misspellingHandler = $misspellingHandler;
82 4
    }
83
84
    /**
85
     * @param TextInterface[] $texts
86
     * @param string[] $languages
87
     *
88 4
     * @return iterable<MisspellingInterface>
89 4
     */
90 2
    private function doSpellCheckTexts(
91
        iterable $texts,
92
        array $languages
93 4
    ): iterable {
94 4
        foreach ($texts as $text) {
95 4
            if ($this->textProcessor !== null) {
96 4
                $text = $this->textProcessor->process($text);
97 4
            }
98
99
            yield from $this->spellChecker->check(
100 4
                $text->getContent(),
101
                $languages,
102 4
                $text->getContext()
103
            );
104
        }
105
    }
106
}
107