Completed
Pull Request — master (#268)
by
unknown
01:20
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
    protected $storage;
8
9
    public function __construct(\Xhgui_StorageInterface $storage)
10
    {
11
        $this->storage = $storage;
12
    }
13
14
    /**
15
     * Get the latest profile data.
16
     *
17
     * @return Xhgui_Profile
18
     * @throws Exception
19
     */
20
    public function latest()
21
    {
22
        $cursor = $this->storage->find()
0 ignored issues
show
Bug introduced by
The call to find() misses a required argument $filter.

This check looks for function calls that miss required arguments.

Loading history...
23
                                ->sort(array('meta.request_date' => -1))
24
                                ->limit(1);
25
        $result = $cursor->getNext();
26
        return $this->wrap($result);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->wrap($result); of type Xhgui_Profile|array adds the type array to the return on line 26 which is incompatible with the return type documented by Xhgui_Profiles::latest of type Xhgui_Profile.
Loading history...
27
    }
28
29
    public function query($conditions, $fields = null)
30
    {
31
        return $this->storage->find($conditions, $fields);
32
    }
33
34
    /**
35
     * Get a single profile run by id.
36
     *
37
     * @param string $id The id of the profile to get.
38
     * @return Xhgui_Profile
39
     * @throws Exception
40
     */
41
    public function get($id)
42
    {
43
        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 43 which is incompatible with the return type documented by Xhgui_Profiles::get of type Xhgui_Profile.
Loading history...
44
    }
45
46
    /**
47
     * Get the list of profiles for a simplified url.
48
     *
49
     * @param string $url The url to load profiles for.
50
     * @param array $options Pagination options to use.
51
     * @param array $conditions The search options.
52
     * @return MongoCursor
53
     */
54
    public function getForUrl($url, $options, $conditions = array())
55
    {
56
        $conditions = array_merge(
57
            (array)$conditions,
58
            array('simple_url' => $url)
59
        );
60
        $options = array_merge($options, array(
61
            'conditions' => $conditions,
62
        ));
63
        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...
64
    }
65
66
    public function paginate(Xhgui_Storage_Filter $filter)
67
    {
68
        $projection = false;
69
70
        if ($projection === false) {
71
            $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...
72
        } else {
73
            $result = $this->storage->find($filter, $projection);
74
        }
75
76
        $totalRows = $this->storage->count($filter);
77
        $totalPages = max(ceil($totalRows / $filter->getPerPage()), 1);
78
79
        return array(
80
            'results'       => $this->wrap($result),
81
            'sort'          => $filter->getSort(),
82
            'direction'     => $filter->getDirection(),
83
            'page'          => $filter->getPage(),
84
            'perPage'       => $filter->getPerPage(),
85
            'totalPages'    => $totalPages
86
        );
87
    }
88
89
    /**
90
     * Get the Percentile metrics for a URL
91
     *
92
     * This will group data by date and returns only the
93
     * percentile + date, making the data ideal for time series graphs
94
     *
95
     * @param integer $percentile The percentile you want. e.g. 90.
96
     * @param string $url
97
     * @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...
98
     * @return array Array of metrics grouped by date
99
     */
100
    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...
101
    {
102
        $col = '$meta.request_date';
103
104
        $results = $this->storage->aggregate($filter, $col, $percentile);
105
        if (empty($results['result'])) {
106
            return array();
107
        }
108
        $keys = array(
109
            'wall_times'    => 'wt',
110
            'cpu_times'     => 'cpu',
111
            'mu_times'      => 'mu',
112
            'pmu_times'     => 'pmu'
113
        );
114
        foreach ($results['result'] as &$result) {
115
            if ($result['_id'] instanceof MongoDate) {
116
                $result['date'] = date('Y-m-d H:i:s', $result['_id']->sec);
117
            } else {
118
                $result['date'] = $result['_id'];
119
            }
120
            
121
            unset($result['_id']);
122
            $index = max(round($result['raw_index']) - 1, 0);
123
            foreach ($keys as $key => $out) {
124
                sort($result[$key]);
125
                $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null;
126
                unset($result[$key]);
127
            }
128
        }
129
        return array_values($results['result']);
130
    }
131
132
    /**
133
     * Get a paginated set of results.
134
     *
135
     * @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...
136
     * @return array An array of result data.
137
     */
138
    public function getAll($filter)
139
    {
140
        return $this->paginate($filter);
141
    }
142
143
    /**
144
     * Insert a profile run.
145
     *
146
     * Does unchecked inserts.
147
     *
148
     * @param array $profile The profile data to save.
149
     * @return
150
     */
151
    public function insert($profile)
152
    {
153
        return $this->storage->insert($profile);
154
    }
155
156
    /**
157
     * Delete a profile run.
158
     *
159
     * @param string $id The profile id to delete.
160
     * @return array|bool
161
     */
162
    public function delete($id)
163
    {
164
        return $this->storage->remove($id);
165
    }
166
167
    /**
168
     * Used to truncate a collection.
169
     *
170
     * Primarly used in test cases to reset the test db.
171
     *
172
     * @return boolean
173
     */
174
    public function truncate()
175
    {
176
        return $this->storage->drop();
177
    }
178
179
    /**
180
     * Converts arrays + MongoCursors into Xhgui_Profile instances.
181
     *
182
     * @param array|MongoCursor $data The data to transform.
183
     * @return Xhgui_Profile|array The transformed/wrapped results.
184
     * @throws Exception
185
     */
186
    protected function wrap($data)
187
    {
188
        if ($data === null) {
189
            throw new Exception('No profile data found.');
190
        }
191
192
        if (!($data instanceof \Xhgui_Storage_ResultSet)) {
193
            return new Xhgui_Profile($data, true);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 186 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...
194
        }
195
196
        $results = array();
197
        foreach ($data as $row) {
198
            $results[] = new Xhgui_Profile($row, true);
199
        }
200
        return $results;
201
    }
202
}
203