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

Xhgui_Profiles::getForUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
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
108
        if (empty($results['result'])) {
109
            return array();
110
        }
111
        $keys = array(
112
            'wall_times'    => 'wt',
113
            'cpu_times'     => 'cpu',
114
            'mu_times'      => 'mu',
115
            'pmu_times'     => 'pmu'
116
        );
117
        foreach ($results['result'] as &$result) {
118
            if ($result['_id'] instanceof MongoDate) {
119
                $result['date'] = date('Y-m-d H:i:s', $result['_id']->sec);
120
            } else {
121
                $result['date'] = $result['_id'];
122
            }
123
            
124
            unset($result['_id']);
125
            $index = max(round($result['raw_index']) - 1, 0);
126
            foreach ($keys as $key => $out) {
127
                sort($result[$key]);
128
                $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null;
129
                unset($result[$key]);
130
            }
131
        }
132
        return array_values($results['result']);
133
    }
134
135
    /**
136
     * Get a paginated set of results.
137
     *
138
     * @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...
139
     * @return array An array of result data.
140
     */
141
    public function getAll($filter)
142
    {
143
        return $this->paginate($filter);
144
    }
145
146
    /**
147
     * Delete a profile run.
148
     *
149
     * @param string $id The profile id to delete.
150
     * @return array|bool
151
     */
152
    public function delete($id)
153
    {
154
        return $this->storage->remove($id);
155
    }
156
157
    /**
158
     * Used to truncate a collection.
159
     *
160
     * Primarly used in test cases to reset the test db.
161
     *
162
     * @return boolean
163
     */
164
    public function truncate()
165
    {
166
        return $this->storage->drop();
167
    }
168
169
    /**
170
     * Converts arrays + MongoCursors into Xhgui_Profile instances.
171
     *
172
     * @param array|MongoCursor $data The data to transform.
173
     * @return Xhgui_Profile|array The transformed/wrapped results.
174
     * @throws Exception
175
     */
176
    protected function wrap($data)
177
    {
178
        if ($data === null) {
179
            throw new Exception('No profile data found.');
180
        }
181
182
        if (!($data instanceof \Xhgui_Storage_ResultSet)) {
183
            return new Xhgui_Profile($data, true);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 176 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...
184
        }
185
186
        $results = array();
187
        foreach ($data as $row) {
188
            $results[] = new Xhgui_Profile($row, true);
189
        }
190
        return $results;
191
    }
192
193
    /**
194
     * @return Xhgui_StorageInterface
195
     */
196
    public function getStorage()
197
    {
198
        return $this->storage;
199
    }
200
201
    /**
202
     * @param Xhgui_StorageInterface $storage
203
     */
204
    public function setStorage($storage)
205
    {
206
        $this->storage = $storage;
207
    }
208
}
209