Completed
Push — master ( 043dac...30f7bc )
by Timo
07:40
created

SuggestQuery::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 4
nop 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
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 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
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
27
28
/**
29
 * A query specialized to get search suggestions
30
 *
31
 * @author Ingo Renner <[email protected]>
32
 * @package TYPO3
33
 * @subpackage solr
34
 */
35
class SuggestQuery extends Query
36
{
37
    /**
38
     * @var array
39
     */
40
    protected $configuration;
41
42
     /**
43
     * @var string
44
     */
45
    protected $prefix;
46
47
    /**
48
     * SuggestQuery constructor.
49
     *
50
     * @param string $keywords
51
     * @param TypoScriptConfiguration $solrConfiguration
52
     */
53
    public function __construct($keywords, $solrConfiguration = null)
54
    {
55
        $keywords = (string) $keywords;
56
        if ($solrConfiguration == null) {
57
            $solrConfiguration = Util::getSolrConfiguration();
58
        }
59
60
        parent::__construct('', $solrConfiguration);
61
62
        $this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []);
63
64
        if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) {
65
            $this->prefix = $this->escape($keywords);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->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...
66
        } else {
67
            $matches = array();
68
            preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches);
69
            $fullKeywords = trim($matches[2]);
70
            $partialKeyword = trim($matches[3]);
71
72
            $this->setKeywords($fullKeywords);
73
            $this->prefix = $this->escape($partialKeyword);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->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...
74
        }
75
76
        $this->setAlternativeQuery('*:*');
77
    }
78
79
    /**
80
     * @return void
81
     */
82
    protected function initializeQuery()
83
    {
84
    }
85
86
    /**
87
     * Returns the query paramters that should be used.
88
     *
89
     * @return array
90
     */
91
    public function getQueryParameters()
92
    {
93
        $suggestParameters = [
94
            'facet' => 'on',
95
            'facet.prefix' => $this->prefix,
96
            'facet.field' => $this->configuration['suggestField'],
97
            'facet.limit' => $this->configuration['numberOfSuggestions'],
98
            'facet.mincount' => '1',
99
            'fq' => $this->filters,
100
            'fl' => $this->configuration['suggestField']
101
        ];
102
103
        return array_merge($suggestParameters, $this->queryParameters);
104
    }
105
}
106