Completed
Pull Request — master (#268)
by
unknown
01:15
created

Xhgui_Profiles::getPercentileForUrl()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 8
nop 3
1
<?php
2
/**
3
 * Contains logic for getting/creating/removing profile records.
4
 */
5
class Xhgui_Profiles
6
{
7
    /**
8
     * @var Xhgui_StorageInterface
9
     */
10
    protected $storage;
11
12
    /**
13
     * Xhgui_Profiles constructor.
14
     * @param Xhgui_StorageInterface $storage
15
     */
16
    public function __construct(\Xhgui_StorageInterface $storage)
17
    {
18
        $this->storage = $storage;
19
    }
20
21
    /**
22
     * @param $conditions
23
     * @param null $fields
24
     * @return mixed
25
     */
26
    public function query($conditions, $fields = null)
27
    {
28
        return $this->storage->find($conditions, $fields);
0 ignored issues
show
Documentation introduced by
$fields is of type null, but the function expects a boolean.

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...
29
    }
30
31
    /**
32
     * Get a single profile run by id.
33
     *
34
     * @param string $id The id of the profile to get.
35
     * @return Xhgui_Profile
36
     * @throws Exception
37
     */
38
    public function get($id)
39
    {
40
        return $this->wrap($this->storage->findOne($id));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->wrap($this->storage->findOne($id)); of type Xhgui_Profile|array adds the type array to the return on line 40 which is incompatible with the return type documented by Xhgui_Profiles::get of type Xhgui_Profile.
Loading history...
41
    }
42
43
    /**
44
     * Get the list of profiles for a simplified url.
45
     *
46
     * @param string $url The url to load profiles for.
47
     * @param array $options Pagination options to use.
48
     * @param array $conditions The search options.
49
     * @return MongoCursor
50
     */
51
    public function getForUrl($url, $options, $conditions = array())
52
    {
53
        $conditions = array_merge(
54
            (array)$conditions,
55
            array('simple_url' => $url)
56
        );
57
        $options = array_merge($options, array(
58
            'conditions' => $conditions,
59
        ));
60
        return $this->paginate($options);
0 ignored issues
show
Documentation introduced by
$options is of type array, but the function expects a object<Xhgui_Storage_Filter>.

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...
61
    }
62
63
    /**
64
     * @param Xhgui_Storage_Filter $filter
65
     * @return array
66
     * @throws Exception
67
     */
68
    public function paginate(Xhgui_Storage_Filter $filter)
69
    {
70
        $projection = false;
71
72
        if ($projection === false) {
73
            $result = $this->storage->find($filter, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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...
74
        } else {
75
            $result = $this->storage->find($filter, $projection);
76
        }
77
78
        $totalRows = $this->storage->count($filter);
79
        $totalPages = max(ceil($totalRows / $filter->getPerPage()), 1);
80
81
        return array(
82
            'results'       => $this->wrap($result),
83
            'sort'          => $filter->getSort(),
84
            'direction'     => $filter->getDirection(),
85
            'page'          => $filter->getPage(),
86
            'perPage'       => $filter->getPerPage(),
87
            'totalPages'    => $totalPages
88
        );
89
    }
90
91
    /**
92
     * Get the Percentile metrics for a URL
93
     *
94
     * This will group data by date and returns only the
95
     * percentile + date, making the data ideal for time series graphs
96
     *
97
     * @param integer $percentile The percentile you want. e.g. 90.
98
     * @param string $url
99
     * @param array $search Search options containing startDate and or endDate
0 ignored issues
show
Bug introduced by
There is no parameter named $search. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
     * @return array Array of metrics grouped by date
101
     */
102
    public function getPercentileForUrl($percentile, $url, $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $url 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...
103
    {
104
        $col = '$meta.request_date';
105
106
        $results = $this->storage->aggregate($filter, $col, $percentile);
107
        if (empty($results['result'])) {
108
            return array();
109
        }
110
        $keys = array(
111
            'wall_times'    => 'wt',
112
            'cpu_times'     => 'cpu',
113
            'mu_times'      => 'mu',
114
            'pmu_times'     => 'pmu'
115
        );
116
        foreach ($results['result'] as &$result) {
117
            if ($result['_id'] instanceof MongoDate) {
118
                $result['date'] = date('Y-m-d H:i:s', $result['_id']->sec);
119
            } else {
120
                $result['date'] = $result['_id'];
121
            }
122
            
123
            unset($result['_id']);
124
            $index = max(round($result['raw_index']) - 1, 0);
125
            foreach ($keys as $key => $out) {
126
                sort($result[$key]);
127
                $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null;
128
                unset($result[$key]);
129
            }
130
        }
131
        return array_values($results['result']);
132
    }
133
134
    /**
135
     * Get a paginated set of results.
136
     *
137
     * @param array $options The find options to use.
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
138
     * @return array An array of result data.
139
     */
140
    public function getAll($filter)
141
    {
142
        return $this->paginate($filter);
143
    }
144
145
    /**
146
     * Insert a profile run.
147
     *
148
     * Does unchecked inserts.
149
     *
150
     * @param array $profile The profile data to save.
151
     * @return
152
     */
153
    public function insert($profile)
154
    {
155
        return $this->storage->insert($profile);
156
    }
157
158
    /**
159
     * Delete a profile run.
160
     *
161
     * @param string $id The profile id to delete.
162
     * @return array|bool
163
     */
164
    public function delete($id)
165
    {
166
        return $this->storage->remove($id);
167
    }
168
169
    /**
170
     * Used to truncate a collection.
171
     *
172
     * Primarly used in test cases to reset the test db.
173
     *
174
     * @return boolean
175
     */
176
    public function truncate()
177
    {
178
        return $this->storage->drop();
179
    }
180
181
    /**
182
     * Converts arrays + MongoCursors into Xhgui_Profile instances.
183
     *
184
     * @param array|MongoCursor $data The data to transform.
185
     * @return Xhgui_Profile|array The transformed/wrapped results.
186
     * @throws Exception
187
     */
188
    protected function wrap($data)
189
    {
190
        if ($data === null) {
191
            throw new Exception('No profile data found.');
192
        }
193
194
        if (!($data instanceof \Xhgui_Storage_ResultSet)) {
195
            return new Xhgui_Profile($data, true);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 188 can also be of type object<MongoCursor>; however, Xhgui_Profile::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
196
        }
197
198
        $results = array();
199
        foreach ($data as $row) {
200
            $results[] = new Xhgui_Profile($row, true);
201
        }
202
        return $results;
203
    }
204
205
    /**
206
     * @return Xhgui_StorageInterface
207
     */
208
    public function getStorage()
209
    {
210
        return $this->storage;
211
    }
212
213
    /**
214
     * @param Xhgui_StorageInterface $storage
215
     */
216
    public function setStorage($storage)
217
    {
218
        $this->storage = $storage;
219
    }
220
}
221