Completed
Push — master ( 3d902c...0a581b )
by Christophe
13:31
created

TreeMap::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TreeMap benchmark class
5
 *
6
 * @author     Christophe Demko <[email protected]>
7
 * @copyright  Copyright (C) 2012-2018 Christophe Demko. All rights reserved.
8
 *
9
 * @license    BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13
14
/**
15
 * TreeMap benchmark class
16
 *
17
 * @since 1.0.5
18
 */
19
class TreeMap
20
{
21
	/**
22
	 * @var TreeMap The tree map
23
	 *
24
	 * @since 1.0.5
25
	 */
26
	protected $tree;
27
28
	/**
29
	 * @var SortedMap The sorted map
30
	 *
31
	 * @since 1.0.5
32
	 */
33
	protected $data;
34
35
	/**
36
	 * Provider for counts
37
	 *
38
	 * @return iterator Iterator on count
39
	 *
40
	 * @since 1.0.5
41
	 */
42
	public function provideCounts()
43
	{
44
		yield array('count' => 100);
45
		yield array('count' => 1000);
46
		yield array('count' => 10000);
47
		yield array('count' => 100000);
48
	}
49
50
	/**
51
	 * Provider for counts
52
	 *
53
	 * @return iterator Iterator on type
54
	 *
55
	 * @since 1.0.5
56
	 */
57
	public function provideTypes()
58
	{
59
		yield array('type' => 'tree');
60
		yield array('type' => 'reversed');
61
		yield array('type' => 'sub', 'from' => 0.30, 'to' => 0.70);
62
		yield array('type' => 'sub', 'from' => 0.40, 'to' => 0.80);
63
	}
64
65
	/**
66
	 * Create the tree map.
67
	 *
68
	 * @param array $params Array of parameters
69
	 *
70
	 * @return void
71
	 *
72
	 * @since 1.0.5
73
	 */
74
	public function init($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
	{
76
		$this->tree = chdemko\SortedCollection\TreeMap::create();
0 ignored issues
show
Documentation Bug introduced by
It seems like \chdemko\SortedCollection\TreeMap::create() of type object<chdemko\SortedCollection\TreeMap> is incompatible with the declared type object<TreeMap> of property $tree.

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...
77
	}
78
79
	/**
80
	 * Create the sorted map.
81
	 *
82
	 * @param array $params Array of parameters
83
	 *
84
	 * @return void
85
	 *
86
	 * @since 1.0.5
87
	 */
88
	public function data($params)
89
	{
90
		if (isset($params['type']))
91
		{
92
			switch ($params['type'])
93
			{
94
				case 'tree':
95
					$this->data = $this->tree;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->tree of type object<TreeMap> is incompatible with the declared type object<SortedMap> of property $data.

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...
96
				break;
97
				case 'reversed':
98
					$this->data = chdemko\SortedCollection\ReversedMap::create($this->tree);
0 ignored issues
show
Documentation introduced by
$this->tree is of type object<TreeMap>, but the function expects a object<chdemko\SortedCollection\SortedMap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
It seems like \chdemko\SortedCollectio...ap::create($this->tree) of type object<chdemko\SortedCollection\ReversedMap> is incompatible with the declared type object<SortedMap> of property $data.

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...
99
				break;
100
				case 'sub':
101
					if (isset($params['from']) && isset($params['to']))
102
					{
103
						$this->data = chdemko\SortedCollection\SubMap::create(
0 ignored issues
show
Documentation Bug introduced by
It seems like \chdemko\SortedCollectio...'] * $params['count'])) of type object<chdemko\SortedCollection\SubMap> is incompatible with the declared type object<SortedMap> of property $data.

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...
104
							$this->tree,
0 ignored issues
show
Documentation introduced by
$this->tree is of type object<TreeMap>, but the function expects a object<chdemko\SortedCollection\SortedMap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
							(int) ($params['from'] * $params['count']),
106
							(int) ($params['to'] * $params['count'])
107
						);
108
					}
109
					else
110
					{
111
						$this->data = chdemko\SortedCollection\SubMap::create(
0 ignored issues
show
Documentation Bug introduced by
It seems like \chdemko\SortedCollectio...this->tree, null, null) of type object<chdemko\SortedCollection\SubMap> is incompatible with the declared type object<SortedMap> of property $data.

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...
112
							$this->tree,
0 ignored issues
show
Documentation introduced by
$this->tree is of type object<TreeMap>, but the function expects a object<chdemko\SortedCollection\SortedMap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
							null,
114
							null
115
						);
116
					}
117
				break;
118
			}
119
		}
120
		else
121
		{
122
			$this->data = $this->tree;
123
		}
124
	}
125
126
	/**
127
	 * Clear the tree map.
128
	 *
129
	 * @param array $params Array of parameters
130
	 *
131
	 * @return void
132
	 *
133
	 * @since 1.0.5
134
	 */
135
	public function finish($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
	{
137
		$this->tree->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not seem to exist on object<TreeMap>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
	}
139
140
	/**
141
	 * @BeforeMethods({"init", "data"})
142
	 * @AfterMethods({"finish"})
143
	 * @Revs(5)
144
	 * @ParamProviders({"provideCounts"})
145
	 *
146
	 * @param array $params Array of parameters
147
	 *
148
	 * @return void
149
	 *
150
	 * @since 1.0.5
151
	 */
152 View Code Duplication
	public function benchFill($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
	{
154
		for ($i = 0; $i < $params['count']; $i++)
155
		{
156
			$this->tree[$i] = $i;
157
		}
158
	}
159
160
	/**
161
	 * @BeforeMethods({"init", "benchFill", "data"})
162
	 * @AfterMethods({"finish"})
163
	 * @Revs(5)
164
	 * @ParamProviders({"provideCounts", "provideTypes"})
165
	 *
166
	 * @param array $params Array of parameters
167
	 *
168
	 * @return void
169
	 *
170
	 * @since 1.0.5
171
	 */
172
	public function benchSearch($params)
173
	{
174 View Code Duplication
		if (isset($params['from']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
		{
176
			$min = (int) ($params['from'] * $params['count']);
177
		}
178
		else
179
		{
180
			$min = 0;
181
		}
182
183 View Code Duplication
		if (isset($params['to']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
		{
185
			$max = (int) ($params['to'] * $params['count']);
186
		}
187
		else
188
		{
189
			$max = $params['count'];
190
		}
191
192
		for ($i = $min; $i < $max; $i++)
193
		{
194
			$value = $this->data[$i];
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
		}
196
	}
197
198
	/**
199
	 * @BeforeMethods({"init", "benchFill", "data"})
200
	 * @AfterMethods({"finish"})
201
	 * @Revs(5)
202
	 * @ParamProviders({"provideCounts"})
203
	 *
204
	 * @param array $params Array of parameters
205
	 *
206
	 * @return void
207
	 *
208
	 * @since 1.0.5
209
	 */
210 View Code Duplication
	public function benchClean($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
	{
212
		for ($i = 0; $i < $params['count']; $i++)
213
		{
214
			unset($this->tree[$i]);
215
		}
216
	}
217
}
218
219