Completed
Push — master ( 694264...44bc0a )
by Timo
11s
created

SpellChecker::getCollatedSuggestion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1.064
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Query\LinkBuilder;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Spell checker / Did you mean
32
 *
33
 * @author Ingo Renner <[email protected]>
34
 */
35
class SpellChecker
36
{
37
38
    /**
39
     * Search instance
40
     *
41
     * @var Search
42
     */
43
    protected $search;
44
45
    /**
46
     * Configuration
47
     *
48
     * @var array
49
     */
50
    protected $configuration;
51
52
    /**
53
     * Constructor
54
     *
55
     */
56 25
    public function __construct()
57
    {
58 25
        $this->search = GeneralUtility::makeInstance(Search::class);
59 25
        $this->configuration = Util::getSolrConfiguration();
0 ignored issues
show
Documentation Bug introduced by
It seems like \ApacheSolrForTypo3\Solr...:getSolrConfiguration() of type object<ApacheSolrForTypo...ypoScriptConfiguration> is incompatible with the declared type array of property $configuration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60 25
    }
61
62
    /**
63
     * Gets the collated suggestion
64
     *
65
     * @return string collated suggestion
66
     */
67 3
    public function getCollatedSuggestion()
68
    {
69 3
        $suggestions = $this->search->getSpellcheckingSuggestions();
70
71 3
        return $suggestions['collation'];
72
    }
73
74
    /**
75
     * Checks whether the user's query was correctly spelled.
76
     *
77
     * @return bool TRUE if the query terms were correctly spelled, FALSE otherwise
78
     */
79
    public function isIncorrectlySpelled()
80
    {
81
        $suggestions = $this->getSuggestions();
82
83
        return $suggestions['correctlySpelled'];
84
    }
85
86
    /**
87
     * Gets the raw spellchecking suggestions
88
     *
89
     * @return array Array of suggestions
90
     */
91 20
    public function getSuggestions()
92
    {
93 20
        $suggestions = $this->search->getSpellcheckingSuggestions();
94
95 20
        return $suggestions;
96
    }
97
98
    /**
99
     * Query URL with a suggested/corrected query
100
     *
101
     * @return string Suggestion/spell checked query URL
102
     */
103
    public function getSuggestionQueryUrl()
104
    {
105
        $suggestions = $this->getSuggestions();
106
107
        $query = clone $this->search->getQuery();
108
        $query->setKeywords($suggestions['collation']);
109
110
        $queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class, $query);
111
        $queryLinkBuilder->setLinkTargetPageId($GLOBALS['TSFE']->id);
112
113
        return $queryLinkBuilder->getQueryUrl();
114
    }
115
116
    /**
117
     * Generates a link with spell checking suggestions if it is activated and
118
     * spell checking suggestions are returned by Solr.
119
     *
120
     * @return string A link to start over with a new search using the suggested keywords.
121
     */
122 25
    public function getSpellCheckingSuggestions()
123
    {
124 25
        $suggestionsLink = '';
125
126 25
        if ($this->configuration->getSearchSpellchecking() && $this->search->hasSearched()) {
0 ignored issues
show
Bug introduced by
The method getSearchSpellchecking cannot be called on $this->configuration (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
127 20
            $suggestions = $this->getSuggestions();
128 20
            if ($suggestions && !$suggestions['correctlySpelled'] && !empty($suggestions['collation'])) {
129 5
                $suggestionsLink = $GLOBALS['TSFE']->cObj->noTrimWrap(
130 5
                    $this->getSuggestionQueryLink(),
131 5
                    $this->configuration->getSearchSpellcheckingWrap()
0 ignored issues
show
Bug introduced by
The method getSearchSpellcheckingWrap cannot be called on $this->configuration (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
132
                );
133
            }
134
        }
135
136 25
        return $suggestionsLink;
137
    }
138
139
    /**
140
     * Query link with a suggested/corrected query
141
     *
142
     * @return string Suggestion/spell checked query link
143
     */
144 5
    public function getSuggestionQueryLink()
145
    {
146 5
        $suggestions = $this->getSuggestions();
147
148 5
        $query = clone $this->search->getQuery();
149 5
        $query->setKeywords($suggestions['collation']);
150
151 5
        $queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class, $query);
152 5
        $queryLinkBuilder->setLinkTargetPageId($GLOBALS['TSFE']->id);
153
154 5
        return $queryLinkBuilder->getQueryLink(htmlspecialchars($query->getKeywordsRaw()));
155
    }
156
}
157