Skip to content

Commit cf88b99

Browse files
committed
🚨 Fix scrutinizer
1 parent 86eb119 commit cf88b99

6 files changed

Lines changed: 44 additions & 29 deletions

File tree

‎benchmarks/TreeMapBench.php‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class TreeMapBench
3939
*/
4040
protected $data;
4141

42+
/**
43+
* @var integer Sink value used to consume benchmark results
44+
*
45+
* @since 1.0.5
46+
*/
47+
protected $sink = 0;
48+
4249
/**
4350
* Provider for counts
4451
*
@@ -72,13 +79,11 @@ public function provideTypes()
7279
/**
7380
* Create the tree map.
7481
*
75-
* @param array $params Array of parameters
76-
*
7782
* @return void
7883
*
7984
* @since 1.0.5
8085
*/
81-
public function init($params)
86+
public function init()
8287
{
8388
$this->tree = TreeMap::create();
8489
}
@@ -126,13 +131,11 @@ public function data($params)
126131
/**
127132
* Clear the tree map.
128133
*
129-
* @param array $params Array of parameters
130-
*
131134
* @return void
132135
*
133136
* @since 1.0.5
134137
*/
135-
public function finish($params)
138+
public function finish()
136139
{
137140
$this->tree->clear();
138141
}
@@ -170,6 +173,8 @@ public function benchFill($params)
170173
*/
171174
public function benchSearch($params)
172175
{
176+
$sink = 0;
177+
173178
if (isset($params['from'])) {
174179
$min = (int) ($params['from'] * $params['count']);
175180
} else {
@@ -183,8 +188,10 @@ public function benchSearch($params)
183188
}
184189

185190
for ($i = $min; $i < $max; $i++) {
186-
$value = $this->data[$i];
191+
$sink += $this->data[$i];
187192
}
193+
194+
$this->sink = $sink;
188195
}
189196

190197
/**

‎src/SortedCollection/ReversedMap.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ class ReversedMap extends AbstractMap
6767
protected function __construct(SortedMap $map)
6868
{
6969
$this->mapInternal = $map;
70-
$this->comparatorInternal = function ($key1, $key2) {
71-
return - call_user_func($this->mapInternal->comparator, $key1, $key2);
70+
$comparator = $map->comparator();
71+
72+
$this->comparatorInternal = function ($key1, $key2) use ($comparator) {
73+
return - call_user_func($comparator, $key1, $key2);
7274
};
7375
}
7476

‎src/SortedCollection/TreeMap.php‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TreeMap extends AbstractMap
5454
*
5555
* @since 1.0.0
5656
*/
57-
private $comparator;
57+
private $comparatorInternal;
5858

5959
/**
6060
* Constructor
@@ -66,11 +66,11 @@ class TreeMap extends AbstractMap
6666
protected function __construct($comparator = null)
6767
{
6868
if ($comparator === null) {
69-
$this->comparator = function ($key1, $key2) {
69+
$this->comparatorInternal = function ($key1, $key2) {
7070
return $key1 <=> $key2;
7171
};
7272
} else {
73-
$this->comparator = $comparator;
73+
$this->comparatorInternal = $comparator;
7474
}
7575
}
7676

@@ -97,7 +97,7 @@ public static function create($comparator = null)
9797
*/
9898
public function comparator()
9999
{
100-
return $this->comparator;
100+
return $this->comparatorInternal;
101101
}
102102

103103
/**
@@ -194,7 +194,7 @@ public function successor($element)
194194
public function lower($key)
195195
{
196196
if ($this->root) {
197-
$lower = $this->root->find($key, $this->comparator, -2);
197+
$lower = $this->root->find($key, $this->comparatorInternal, -2);
198198
} else {
199199
$lower = null;
200200
}
@@ -220,7 +220,7 @@ public function lower($key)
220220
public function floor($key)
221221
{
222222
if ($this->root) {
223-
$floor = $this->root->find($key, $this->comparator, -1);
223+
$floor = $this->root->find($key, $this->comparatorInternal, -1);
224224
} else {
225225
$floor = null;
226226
}
@@ -246,7 +246,7 @@ public function floor($key)
246246
public function find($key)
247247
{
248248
if ($this->root) {
249-
$find = $this->root->find($key, $this->comparator, 0);
249+
$find = $this->root->find($key, $this->comparatorInternal, 0);
250250
} else {
251251
$find = null;
252252
}
@@ -272,7 +272,7 @@ public function find($key)
272272
public function ceiling($key)
273273
{
274274
if ($this->root) {
275-
$ceiling = $this->root->find($key, $this->comparator, 1);
275+
$ceiling = $this->root->find($key, $this->comparatorInternal, 1);
276276
} else {
277277
$ceiling = null;
278278
}
@@ -298,7 +298,7 @@ public function ceiling($key)
298298
public function higher($key)
299299
{
300300
if ($this->root) {
301-
$higher = $this->root->find($key, $this->comparator, 2);
301+
$higher = $this->root->find($key, $this->comparatorInternal, 2);
302302
} else {
303303
$higher = null;
304304
}
@@ -408,7 +408,7 @@ public function jsonSerialize(): array
408408
public function offsetSet($key, $value): void
409409
{
410410
if ($this->root) {
411-
$this->root = $this->root->insert($key, $value, $this->comparator);
411+
$this->root = $this->root->insert($key, $value, $this->comparatorInternal);
412412
} else {
413413
$this->root = TreeNode::create($key, $value);
414414
}
@@ -426,7 +426,7 @@ public function offsetSet($key, $value): void
426426
public function offsetUnset($key): void
427427
{
428428
if ($this->root) {
429-
$this->root = $this->root->remove($key, $this->comparator);
429+
$this->root = $this->root->remove($key, $this->comparatorInternal);
430430
}
431431
}
432432

‎src/SortedCollection/TreeNode.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TreeNode implements \Countable
5858
*
5959
* @since 1.0.0
6060
*/
61-
private $key;
61+
private $keyInternal;
6262

6363
/**
6464
* @var mixed Node value
@@ -94,7 +94,7 @@ public static function create($key, $value)
9494
*/
9595
protected function __construct($key, $value, $predecessor = null, $successor = null)
9696
{
97-
$this->key = $key;
97+
$this->keyInternal = $key;
9898
$this->value = $value;
9999
$this->left = $predecessor;
100100
$this->right = $successor;
@@ -123,7 +123,7 @@ public function __get($property)
123123
case 'successor':
124124
return $this->successor();
125125
case 'key':
126-
return $this->key;
126+
return $this->keyInternal;
127127
case 'count':
128128
return $this->count();
129129
default:
@@ -265,7 +265,7 @@ public function find($key, $comparator, $type = 0)
265265
$cmp = 0;
266266

267267
while (true) {
268-
$cmp = call_user_func($comparator, $key, $node->key);
268+
$cmp = call_user_func($comparator, $key, $node->keyInternal);
269269

270270
if ($cmp < 0 && $node->information & 2) {
271271
$node = $node->left;
@@ -429,7 +429,7 @@ private function decBalance()
429429
public function insert($key, $value, $comparator)
430430
{
431431
$node = $this;
432-
$cmp = call_user_func($comparator, $key, $this->key);
432+
$cmp = call_user_func($comparator, $key, $this->keyInternal);
433433

434434
if ($cmp < 0) {
435435
if ($this->information & 2) {
@@ -483,7 +483,7 @@ private function pullUpLeftMost()
483483
return $this;
484484
}
485485
} else {
486-
$this->left->key = $this->key;
486+
$this->left->keyInternal = $this->keyInternal;
487487
$this->left->value = $this->value;
488488

489489
if ($this->information & 1) {
@@ -516,7 +516,7 @@ private function pullUpLeftMost()
516516
*/
517517
public function remove($key, $comparator)
518518
{
519-
$cmp = call_user_func($comparator, $key, $this->key);
519+
$cmp = call_user_func($comparator, $key, $this->keyInternal);
520520

521521
if ($cmp < 0) {
522522
if ($this->information & 2) {

‎src/SortedCollection/TreeSet.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ public function put($traversable = array())
8282
*/
8383
public function clear()
8484
{
85-
$this->getMap()->clear();
85+
$map = $this->getMap();
86+
87+
if ($map instanceof TreeMap) {
88+
$map->clear();
89+
} else {
90+
$this->setMap(TreeMap::create($map->comparator()));
91+
}
8692

8793
return $this;
8894
}

‎tests/SortedCollection/TreeNodeTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function nodeToString($node)
4141
$string = '(';
4242

4343
// Set the key property accessible
44-
$key = (new \ReflectionClass($node))->getProperty('key');
44+
$key = (new \ReflectionClass($node))->getProperty('keyInternal');
4545
$key->setAccessible(true);
4646
$string .= $key->getValue($node);
4747

0 commit comments

Comments
 (0)