Issues (24)

src/Misspelling.php (1 issue)

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 int|null start at 0
19
     */
20
    private $offset;
21
22
    /**
23
     * @var int|null start at 1
24
     */
25
    private $lineNumber;
26
27
    /**
28
     * @var array<string>
29
     */
30
    private $suggestions;
31
32
    /**
33
     * @var array<mixed>
34
     */
35
    private $context;
36
37 18
    /**
38
     * @param array<mixed> $context
39
     * @param array<string> $suggestions
40
     */
41
    public function __construct(
42
        string $word,
43
        ?int $offset = null,
44 18
        ?int $lineNumber = null,
45
        array $suggestions = [],
46 18
        array $context = []
47 18
    ) {
48 18
        Assert::stringNotEmpty($word);
49 18
50 18
        $this->word = $word;
51 18
        $this->offset = $offset;
52
        $this->lineNumber = $lineNumber;
53 2
        $this->suggestions = $suggestions;
54
        $this->context = $context;
55 2
    }
56 2
57 2
    public function mergeSuggestions(array $suggestionsToAdd): MisspellingInterface
58 2
    {
59 2
        $mergedSuggestions = [];
60
        $existingSuggestionsAsKeys = \Safe\array_flip($this->suggestions);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\array_flip() 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

60
        $existingSuggestionsAsKeys = /** @scrutinizer ignore-deprecated */ \Safe\array_flip($this->suggestions);

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...
61
        foreach ($suggestionsToAdd as $suggestionToAdd) {
62
            if (!isset($existingSuggestionsAsKeys[$suggestionToAdd])) {
63 2
                $this->suggestions[] = $suggestionToAdd;
64 2
            }
65 2
        }
66 2
67 2
        return new self(
68 2
            $this->word,
69
            $this->offset,
70
            $this->lineNumber,
71
            $mergedSuggestions,
72 1
            $this->context
73
        );
74 1
    }
75
76
    public function getUniqueIdentity(): string
77 4
    {
78
        return $this->getWord() . $this->getLineNumber() . $this->getOffset();
79 4
    }
80 4
81
    public function canDeterminateUniqueIdentity(): bool
82
    {
83 11
        return $this->getLineNumber() !== null
84
            && $this->getOffset() !== null;
85 11
    }
86
87
    public function getWord(): string
88 12
    {
89
        return $this->word;
90 12
    }
91
92
    public function getOffset(): ?int
93 14
    {
94
        return $this->offset;
95 14
    }
96
97
    public function getLineNumber(): ?int
98 1
    {
99
        return $this->lineNumber;
100 1
    }
101
102
    public function hasSuggestions(): bool
103
    {
104
        return !empty($this->suggestions);
105
    }
106
107
    public function hasContext(): bool
108
    {
109
        return !empty($this->context);
110
    }
111 12
112
    public function getSuggestions(): array
113 12
    {
114
        return $this->suggestions;
115
    }
116 10
117
    public function getContext(): array
118 10
    {
119
        return $this->context;
120
    }
121 3
122
    public function setContext(array $context): MisspellingInterface
123 3
    {
124 3
        return new self(
125 3
            $this->word,
126 3
            $this->offset,
127 3
            $this->lineNumber,
128 3
            $this->suggestions,
129
            $context
130
        );
131
    }
132 5
133
    public function mergeContext(array $context, bool $override = true): MisspellingInterface
134 5
    {
135 1
        if (empty($context)) {
136
            throw new InvalidArgumentException('Context trying to be merged is empty');
137
        }
138 4
139 4
        return new self(
140 4
            $this->word,
141 4
            $this->offset,
142 4
            $this->lineNumber,
143 4
            $this->suggestions,
144
            $override ? array_merge($this->context, $context) : array_merge($context, $this->context)
145
        );
146
    }
147
}
148