Issues (150)

src/Extension/SpellCheck.php (2 issues)

Labels
Severity
1
<?php
2
namespace SoliDry\Extension;
3
4
use SoliDry\Helpers\ConfigHelper;
5
use SoliDry\Helpers\MigrationsHelper;
6
use SoliDry\Types\ConfigInterface;
7
use SoliDry\Types\PhpInterface;
8
9
class SpellCheck
10
{
11
    /**
12
     * @var mixed|null
13
     */
14
    private $entity    = [];
15
16
    /**
17
     * @var int|string|null
18
     */
19
    private $field     = null;
20
21
    /**
22
     * @var bool
23
     */
24
    private bool $isEnabled = false;
25
26
    /**
27
     * @var mixed|string
28
     */
29
    private $language  = ConfigInterface::DEFAULT_LANGUAGE;
30
31
    /**
32
     * @var false|int|null
33
     */
34
    private $speLink = null;
35
    /**
36
     * SpellCheck constructor.
37
     * @param string $entity
38
     */
39
    public function __construct(string $entity)
40
    {
41
        $this->entity    = ConfigHelper::getNestedParam(ConfigInterface::SPELL_CHECK, MigrationsHelper::getTableName($entity));
42
        $this->field     = key($this->entity);
0 ignored issues
show
It seems like $this->entity can also be of type null; however, parameter $array of key() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $this->field     = key(/** @scrutinizer ignore-type */ $this->entity);
Loading history...
43
        $this->isEnabled = empty($this->entity[$this->field][ConfigInterface::ENABLED]) ? false : true;
44
        $this->language  = empty($this->entity[$this->field][ConfigInterface::LANGUAGE]) ? ConfigInterface::DEFAULT_LANGUAGE
45
            : $this->entity[$this->field][ConfigInterface::LANGUAGE];
46
        if($this->isEnabled === true) {
47
            $this->speLink = pspell_new($this->language, '', '', '', PSPELL_FAST);
48
        }
49
    }
50
51
    /**
52
     * Gets the "dirty" text and returns array of failed spell checked words
53
     * if there are any
54
     * @param string $text
55
     * @return array
56
     */
57
    public function check(string $text)
58
    {
59
        $failed = [];
60
        $cleanText = $this->cleanText($text);
61
        $words     = explode(PhpInterface::SPACE, $cleanText);
62
        foreach($words as $k => $word) {
63
            $pass = pspell_check($this->speLink, $word);
0 ignored issues
show
It seems like $this->speLink can also be of type boolean; however, parameter $dictionary_link of pspell_check() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $pass = pspell_check(/** @scrutinizer ignore-type */ $this->speLink, $word);
Loading history...
64
            if($pass === false) {
65
                $failed[] = $word;
66
            }
67
        }
68
        return $failed;
69
    }
70
71
    /**
72
     * @param string $text
73
     * @return string
74
     */
75
    private function cleanText(string $text)
76
    {
77
        return str_replace([',', '.', '-', '- ', ':', '"'], '', $text);
78
    }
79
80
    /**
81
     * @return boolean
82
     */
83
    public function isEnabled()
84
    {
85
        return $this->isEnabled;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getLanguage()
92
    {
93
        return $this->language;
94
    }
95
96
    /**
97
     * @return mixed|null
98
     */
99
    public function getField()
100
    {
101
        return $this->field;
102
    }
103
}