Completed
Push — master ( 6247dd...e88f06 )
by Peter
07:34
created

MoreLike::toArray()   C

Complexity

Conditions 13
Paths 160

Size

Total Lines 72
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13.33

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 72
ccs 28
cts 32
cp 0.875
rs 5.0833
cc 13
eloc 39
nc 160
nop 0
crap 13.33

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 15.06.18
6
 * Time: 14:38
7
 */
8
9
namespace Maslosoft\Manganel\Options;
10
11
use function is_array;
12
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
13
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
14
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionDecorator;
15
use Maslosoft\Manganel\Helpers\Strings;
16
use Maslosoft\Manganel\Helpers\TypeNamer;
17
use Maslosoft\Manganel\Manganel;
18
use redirect_interval;
19
use ReflectionClass;
20
21
/**
22
 * Holder for more like options
23
 * @package Maslosoft\Manganel\Options
24
 */
25
class MoreLike
26
{
27
	/**
28
	 * Models to search from similar
29
	 * @var AnnotatedInterface[]
30
	 */
31
	public $models = [];
32
33
	/**
34
	 * A list of fields to fetch and analyze the text from. Defaults to the _all field for free text and to all
35
	 * possible fields for document inputs.
36
	 * @var array
37
	 */
38
	public $fields = [];
39
40
	/**
41
	 *
42
	 * The unlike parameter is used in conjunction with like in order not to select terms found in a chosen set of
43
	 * documents. In other words, we could ask for documents like: "Apple", but unlike: "cake crumble tree". The syntax
44
	 * is the same as like.
45
	 * @var array
46
	 */
47
	public $unlike = [];
48
49
	/**
50
	 * Extra text to search for
51
	 * @var string[]
52
	 */
53
	public $texts = [];
54
55
	/**
56
	 * The maximum number of query terms that will be selected. Increasing this value gives greater accuracy at the
57
	 * expense of query execution speed.
58
	 * @var int
59
	 */
60
	public $maxQueryTerms = 25;
61
62
	/**
63
	 * The minimum term frequency below which the terms will be ignored from the input document.
64
	 * @var int
65
	 */
66
	public $minTermFreq = 2;
67
68
	/**
69
	 * The minimum document frequency below which the terms will be ignored from the input document.
70
	 * @var int
71
	 */
72
	public $minDocFreq = 5;
73
74
	/**
75
	 * The maximum document frequency above which the terms will be ignored from the input document. This could be
76
	 * useful in order to ignore highly frequent words such as stop words
77
	 * @var int
78
	 */
79
	public $maxDocFreq = null;
80
81
	/**
82
	 * The minimum word length below which the terms will be ignored. The old name min_word_len is deprecated.
83
	 * @var int
84
	 */
85
	public $minWordLength = 0;
86
87
	/**
88
	 * The maximum word length above which the terms will be ignored. The old name max_word_len is deprecated.
89
	 * @var int
90
	 */
91
	public $maxWordLength = null;
92
93
	/**
94
	 * An array of stop words. Any word in this set is considered "uninteresting" and ignored. If the analyzer allows
95
	 * for stop words, you might want to tell MLT to explicitly ignore them, as for the purposes of document similarity
96
	 * it seems reasonable to assume that "a stop word is never interesting".
97
	 * @var array
98
	 */
99
	public $stopWords = [];
100
101
	/**
102
	 * MoreLikeOptions constructor.
103
	 * @param AnnotatedInterface|AnnotatedInterface[]|DataProviderInterface|null $models
104
	 */
105 1
	public function __construct($models = null)
106
	{
107 1
		if ($models instanceof DataProviderInterface)
108
		{
109
			$this->models = $models->getData();
110
		}
111 1
		elseif ($models instanceof AnnotatedInterface)
112
		{
113 1
			$this->models = [$models];
114
		}
115
		elseif (is_array($models))
116
		{
117
			$this->models = $models;
118
		}
119 1
	}
120
121 1
	public function toArray()
122
	{
123 1
		$options = [];
124 1
		$like = [];
125 1
		if (!empty($this->texts))
126
		{
127
			foreach ((array)$this->texts as $text)
128
			{
129
				$like[] = $text;
130
			}
131
		}
132 1
		if (!empty($this->models))
133
		{
134 1
			foreach ($this->models as $model)
135
			{
136 1
				$like[] = [
137 1
					'_index' => Manganel::create($model)->index,
138 1
					'_type' => TypeNamer::nameType($model),
139 1
					'_id' => (string)$model->_id
140
				];
141
			}
142
		}
143
144 1
		if (!empty($like))
145
		{
146 1
			$options['like'] = $like;
147
		}
148
149 1
		if (!empty($this->fields))
150
		{
151
			// TODO Must decorate, ie `title` to be `title.en` etc.
152
			$options['fields'] = $this->fields;
153
		}
154
155 1
		if(!empty($this->unlike))
156
		{
157
			// TODO Must consider also models array etc.
158
			$options['unlike'] = $this->unlike;
159
		}
160
161
		$keys = [
162 1
			'maxQueryTerms',
163
			'minTermFreq',
164
			'minDocFreq',
165
			'maxDocFreq',
166
			'minWordLength',
167
			'maxWordLength',
168
			'stopWords',
169
		];
170 1
		$info = new ReflectionClass($this);
171 1
		$defaults = $info->getDefaultProperties();
172
173 1
		foreach ($keys as $name)
174
		{
175 1
			$value = $this->$name;
176 1
			if (null === $value)
177
			{
178 1
				continue;
179
			}
180 1
			if (is_array($value) && empty($value))
181
			{
182 1
				continue;
183
			}
184 1
			if ($defaults[$name] === $value)
185
			{
186 1
				continue;
187
			}
188 1
			$key = Strings::decamelize($name, '_');
189 1
			$options[$key] = $value;
190
		}
191 1
		return $options;
192
	}
193
}