Passed
Push — develop ( f3fa70...194671 )
by Christophe
01:51
created

TreeMapBench::provideCounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TreeMap benchmark class
5
 *
6
 * @author     Christophe Demko <[email protected]>
7
 * @copyright  Copyright (C) 2012-2023 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
// Declare chdemko\SortedCollectionBenchmark namespace
15
namespace chdemko\SortedCollectionBenchmark;
16
17
use chdemko\SortedCollection\TreeMap;
18
use chdemko\SortedCollection\ReversedMap;
19
use chdemko\SortedCollection\SubMap;
20
21
/**
22
 * TreeMap benchmark class
23
 *
24
 * @since 1.0.5
25
 */
26
class TreeMapBench
27
{
28
    /**
29
     * @var TreeMap The tree map
30
     *
31
     * @since 1.0.5
32
     */
33
    protected $tree;
34
35
    /**
36
     * @var SortedMap The sorted map
0 ignored issues
show
Bug introduced by
The type chdemko\SortedCollectionBenchmark\SortedMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     *
38
     * @since 1.0.5
39
     */
40
    protected $data;
41
42
    /**
43
     * Provider for counts
44
     *
45
     * @return iterator Iterator on count
0 ignored issues
show
Bug introduced by
The type chdemko\SortedCollectionBenchmark\iterator was not found. Did you mean iterator? If so, make sure to prefix the type with \.
Loading history...
46
     *
47
     * @since 1.0.5
48
     */
49
    public function provideCounts()
50
    {
51
        yield array('count' => 100);
0 ignored issues
show
Bug Best Practice introduced by
The expression yield array('count' => 100) returns the type Generator which is incompatible with the documented return type chdemko\SortedCollectionBenchmark\iterator.
Loading history...
52
        yield array('count' => 1000);
53
        yield array('count' => 10000);
54
        yield array('count' => 100000);
55
    }
56
57
    /**
58
     * Provider for counts
59
     *
60
     * @return iterator Iterator on type
61
     *
62
     * @since 1.0.5
63
     */
64
    public function provideTypes()
65
    {
66
        yield array('type' => 'tree');
0 ignored issues
show
Bug Best Practice introduced by
The expression yield array('type' => 'tree') returns the type Generator which is incompatible with the documented return type chdemko\SortedCollectionBenchmark\iterator.
Loading history...
67
        yield array('type' => 'reversed');
68
        yield array('type' => 'sub', 'from' => 0.30, 'to' => 0.70);
69
        yield array('type' => 'sub', 'from' => 0.40, 'to' => 0.80);
70
    }
71
72
    /**
73
     * Create the tree map.
74
     *
75
     * @param array $params Array of parameters
76
     *
77
     * @return void
78
     *
79
     * @since 1.0.5
80
     */
81
    public function init($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
    public function init(/** @scrutinizer ignore-unused */ $params)

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

Loading history...
82
    {
83
        $this->tree = TreeMap::create();
84
    }
85
86
    /**
87
     * Create the sorted map.
88
     *
89
     * @param array $params Array of parameters
90
     *
91
     * @return void
92
     *
93
     * @since 1.0.5
94
     */
95
    public function data($params)
96
    {
97
        if (isset($params['type'])) {
98
            switch ($params['type']) {
99
                case 'tree':
100
                    $this->data = $this->tree;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->tree of type chdemko\SortedCollection\TreeMap is incompatible with the declared type chdemko\SortedCollectionBenchmark\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...
101
                    break;
102
                case 'reversed':
103
                    $this->data = ReversedMap::create($this->tree);
104
                    break;
105
                case 'sub':
106
                    if (isset($params['from']) && isset($params['to'])) {
107
                        $this->data = SubMap::create(
108
                            $this->tree,
109
                            (int) ($params['from'] * $params['count']),
110
                            (int) ($params['to'] * $params['count'])
111
                        );
112
                    } else {
113
                        $this->data = SubMap::create(
114
                            $this->tree,
115
                            null,
116
                            null
117
                        );
118
                    }
119
                    break;
120
            }
121
        } else {
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

135
    public function finish(/** @scrutinizer ignore-unused */ $params)

This check looks for 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();
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
    public function benchFill($params)
153
    {
154
        for ($i = 0; $i < $params['count']; $i++) {
155
            $this->tree[$i] = $i;
156
        }
157
    }
158
159
    /**
160
     * @BeforeMethods({"init", "benchFill", "data"})
161
     * @AfterMethods({"finish"})
162
     * @Revs(5)
163
     * @ParamProviders({"provideCounts", "provideTypes"})
164
     *
165
     * @param array $params Array of parameters
166
     *
167
     * @return void
168
     *
169
     * @since 1.0.5
170
     */
171
    public function benchSearch($params)
172
    {
173
        if (isset($params['from'])) {
174
            $min = (int) ($params['from'] * $params['count']);
175
        } else {
176
            $min = 0;
177
        }
178
179
        if (isset($params['to'])) {
180
            $max = (int) ($params['to'] * $params['count']);
181
        } else {
182
            $max = $params['count'];
183
        }
184
185
        for ($i = $min; $i < $max; $i++) {
186
            $value = $this->data[$i];
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
187
        }
188
    }
189
190
    /**
191
     * @BeforeMethods({"init", "benchFill", "data"})
192
     * @AfterMethods({"finish"})
193
     * @Revs(5)
194
     * @ParamProviders({"provideCounts"})
195
     *
196
     * @param array $params Array of parameters
197
     *
198
     * @return void
199
     *
200
     * @since 1.0.5
201
     */
202
    public function benchClean($params)
203
    {
204
        for ($i = 0; $i < $params['count']; $i++) {
205
            unset($this->tree[$i]);
206
        }
207
    }
208
}
209