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

Misspelling   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 96.36%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 134
rs 10
c 0
b 0
f 0
ccs 53
cts 55
cp 0.9636
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A setContext() 0 8 1
A getSuggestions() 0 3 1
A getLineNumber() 0 3 1
A hasSuggestions() 0 3 1
A getContext() 0 3 1
A mergeSuggestions() 0 16 3
A mergeContext() 0 12 3
A getOffset() 0 3 1
A getUniqueIdentity() 0 3 1
A hasContext() 0 3 1
A canDeterminateUniqueIdentity() 0 4 2
A getWord() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck;
6
7
use PhpSpellcheck\Exception\InvalidArgumentException;
8
use Webmozart\Assert\Assert;
9
10
class Misspelling implements MisspellingInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $word;
16
17
    /**
18
     * @var null|int
19
     */
20
    private $offset;
21
22
    /**
23
     * @var null|int
24
     */
25
    private $lineNumber;
26
27
    /**
28
     * @var string[]
29
     */
30
    private $suggestions;
31
32
    /**
33
     * @var array
34
     */
35
    private $context;
36
37 18
    public function __construct(
38
        string $word,
39
        ?int $offset = null,
40
        ?int $lineNumber = null,
41
        array $suggestions = [],
42
        array $context = []
43
    ) {
44 18
        Assert::stringNotEmpty($word);
45
46 18
        $this->word = $word;
47 18
        $this->offset = $offset;
48 18
        $this->lineNumber = $lineNumber;
49 18
        $this->suggestions = $suggestions;
50 18
        $this->context = $context;
51 18
    }
52
53 2
    public function mergeSuggestions(array $suggestionsToAdd): MisspellingInterface
54
    {
55 2
        $mergedSuggestions = [];
56 2
        $existingSuggestionsAsKeys = array_flip($this->suggestions);
57 2
        foreach ($suggestionsToAdd as $suggestionToAdd) {
58 2
            if (!isset($existingSuggestionsAsKeys[$suggestionToAdd])) {
59 2
                $this->suggestions[] = $suggestionToAdd;
60
            }
61
        }
62
63 2
        return new self(
64 2
            $this->word,
65 2
            $this->offset,
66 2
            $this->lineNumber,
67 2
            $mergedSuggestions,
68 2
            $this->context
69
        );
70
    }
71
72 1
    public function getUniqueIdentity(): string
73
    {
74 1
        return $this->getWord() . $this->getLineNumber() . $this->getOffset();
75
    }
76
77 4
    public function canDeterminateUniqueIdentity(): bool
78
    {
79 4
        return $this->getLineNumber() !== null
80 4
            && $this->getOffset() !== null;
81
    }
82
83 11
    public function getWord(): string
84
    {
85 11
        return $this->word;
86
    }
87
88 12
    public function getOffset(): ?int
89
    {
90 12
        return $this->offset;
91
    }
92
93 14
    public function getLineNumber(): ?int
94
    {
95 14
        return $this->lineNumber;
96
    }
97
98 1
    public function hasSuggestions(): bool
99
    {
100 1
        return !empty($this->suggestions);
101
    }
102
103
    public function hasContext(): bool
104
    {
105
        return !empty($this->context);
106
    }
107
108
    /**
109
     * @return string[]
110
     */
111 12
    public function getSuggestions(): array
112
    {
113 12
        return $this->suggestions;
114
    }
115
116 10
    public function getContext(): array
117
    {
118 10
        return $this->context;
119
    }
120
121 3
    public function setContext(array $context): MisspellingInterface
122
    {
123 3
        return new self(
124 3
            $this->word,
125 3
            $this->offset,
126 3
            $this->lineNumber,
127 3
            $this->suggestions,
128 3
            $context
129
        );
130
    }
131
132 5
    public function mergeContext(array $context, bool $override = true): MisspellingInterface
133
    {
134 5
        if (empty($context)) {
135 1
            throw new InvalidArgumentException('Context trying to be merged is empty');
136
        }
137
138 4
        return new self(
139 4
            $this->word,
140 4
            $this->offset,
141 4
            $this->lineNumber,
142 4
            $this->suggestions,
143 4
            $override ? array_merge($this->context, $context) : array_merge($context, $this->context)
144
        );
145
    }
146
}
147