Completed
Push — master ( 6ee2a7...5f60a5 )
by Rafael
04:29
created

SuggestQuery::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-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 3 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\Domain\Search\Query\Helper\EscapeService;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\Util;
30
31
/**
32
 * A query specialized to get search suggestions
33
 *
34
 * @author Ingo Renner <[email protected]>
35
 */
36
class SuggestQuery extends Query
37
{
38
    /**
39
     * @var array
40
     */
41
    protected $configuration;
42
43
    /**
44
     * @var string
45
     */
46
    protected $prefix;
47
48
    /**
49
     * SuggestQuery constructor.
50
     *
51
     * @param string $keywords
52
     * @param TypoScriptConfiguration $solrConfiguration
53
     */
54
    public function __construct($keywords, $solrConfiguration = null)
55
    {
56
        $keywords = (string)$keywords;
57
        if ($solrConfiguration == null) {
58
            $solrConfiguration = Util::getSolrConfiguration();
59
        }
60
61
        parent::__construct('');
62
63
        $this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []);
64
65
        if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) {
66
            $this->prefix = EscapeService::escape($keywords);
0 ignored issues
show
Documentation Bug introduced by
It seems like \ApacheSolrForTypo3\Solr...vice::escape($keywords) can also be of type integer or double. However, the property $prefix is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
67
        } else {
68
            $matches = [];
69
            preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches);
70
            $fullKeywords = trim($matches[2]);
71
            $partialKeyword = trim($matches[3]);
72
73
            $this->getQueryStringContainer()->setKeywords($fullKeywords);
74
            $this->prefix = EscapeService::escape($partialKeyword);
0 ignored issues
show
Documentation Bug introduced by
It seems like \ApacheSolrForTypo3\Solr...escape($partialKeyword) can also be of type integer or double. However, the property $prefix is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        }
76
77
        $this->setAlternativeQuery('*:*');
78
    }
79
80
    /**
81
     * Returns the query parameters that should be used.
82
     *
83
     * @return array
84
     */
85
    public function getQueryParameters()
86
    {
87
        $suggestParameters = [
88
            'facet' => 'on',
89
            'facet.prefix' => $this->prefix,
90
            'facet.field' => $this->configuration['suggestField'],
91
            'facet.limit' => $this->configuration['numberOfSuggestions'],
92
            'facet.mincount' => '1',
93
            'fl' => $this->configuration['suggestField']
94
        ];
95
96
        $this->filters->build($this);
97
        $this->queryParametersContainer->merge($suggestParameters);
98
        return $this->queryParametersContainer->toArray();
99
    }
100
}
101