Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

MisspellingFinder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 100
rs 10
c 0
b 0
f 0
ccs 28
cts 34
cp 0.8235
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doSpellcheckFromSource() 0 19 3
A __construct() 0 8 1
A find() 0 13 2
A doSpellcheck() 0 13 4
A setSpellchecker() 0 3 1
A setMisspellingHandler() 0 3 1
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\PHPString;
10
use PhpSpellcheck\Source\SourceInterface;
11
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
12
use PhpSpellcheck\TextProcessor\TextProcessorInterface;
13
use PhpSpellcheck\Utils\TextEncoding;
14
15
class MisspellingFinder
16
{
17
    /**
18
     * @var SpellcheckerInterface
19
     */
20
    private $spellChecker;
21
22
    /**
23
     * @var null|MisspellingHandlerInterface
24
     */
25
    private $misspellingHandler;
26
27
    /**
28
     * @var null|TextProcessorInterface
29
     */
30
    private $textProcessor;
31
32 5
    public function __construct(
33
        SpellcheckerInterface $spellChecker,
34
        ?MisspellingHandlerInterface $misspellingHandler = null,
35
        ?TextProcessorInterface $textProcessor = null
36
    ) {
37 5
        $this->spellChecker = $spellChecker;
38 5
        $this->misspellingHandler = $misspellingHandler;
39 5
        $this->textProcessor = $textProcessor;
40 5
    }
41
42
    /**
43
     * @param string|SourceInterface $source
44
     *
45
     * @return MisspellingInterface[]
46
     */
47 5
    public function find(
48
        $source,
49
        array $languages = [],
50
        array $context = [],
51
        string $spellCheckEncoding = TextEncoding::UTF8
52
    ) {
53 5
        $misspellings = $this->doSpellcheck($source, $languages, $context, $spellCheckEncoding);
54
55 4
        if ($this->misspellingHandler !== null) {
56 1
            $this->misspellingHandler->handle($misspellings);
57
        }
58
59 4
        return $misspellings;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $misspellings returns the type iterable which is incompatible with the documented return type PhpSpellcheck\MisspellingInterface[].
Loading history...
60
    }
61
62
    /**
63
     * @param string|SourceInterface $source
64
     *
65
     * @return MisspellingInterface[]
66
     */
67 5
    private function doSpellcheck($source, array $languages, array $context, string $spellCheckEncoding): iterable
68
    {
69 5
        if (is_string($source)) {
70 4
            $source = new PHPString($source);
71
        }
72
73 5
        if ($source instanceof SourceInterface) {
0 ignored issues
show
introduced by
$source is always a sub-type of PhpSpellcheck\Source\SourceInterface.
Loading history...
74 4
            return $this->doSpellcheckFromSource($source, $languages, $context, $spellCheckEncoding);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->doSpellche...t, $spellCheckEncoding) returns the type Generator which is incompatible with the documented return type PhpSpellcheck\MisspellingInterface[].
Loading history...
75
        }
76
77 1
        $sourceVarType = is_object($source) ? get_class($source) : gettype($source);
78
79 1
        throw new InvalidArgumentException('Source should be of type string or ' . SourceInterface::class . '. "' . $sourceVarType . '" given');
80
    }
81
82 4
    /**
83
     * @return MisspellingInterface[]
84
     */
85
    private function doSpellcheckFromSource(
86
        SourceInterface $source,
87
        array $languages,
88 4
        array $context,
89 4
        string $spellCheckEncoding
90 2
    ): iterable {
91
        foreach ($source->toTexts($context) as $text) {
92
            if ($this->textProcessor !== null) {
93 4
                $text = $this->textProcessor->process($text);
94 4
            }
95 4
96 4
            $misspellingsCheck = $this->spellChecker->check(
97 4
                $text->getContent(),
98
                $languages,
99
                $text->getContext(),
100 4
                $spellCheckEncoding
101
            );
102 4
103
            yield from $misspellingsCheck;
0 ignored issues
show
Bug Best Practice introduced by
The expression YieldFromNode returns the type Generator which is incompatible with the documented return type PhpSpellcheck\MisspellingInterface[].
Loading history...
104
        }
105
    }
106
107
    public function setSpellchecker(SpellcheckerInterface $spellChecker): void
108
    {
109
        $this->spellChecker = $spellChecker;
110
    }
111
112
    public function setMisspellingHandler(MisspellingHandlerInterface $misspellingHandler): void
113
    {
114
        $this->misspellingHandler = $misspellingHandler;
115
    }
116
}
117