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

Xhgui_Profiles   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 203
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getForUrl() 0 11 1
A __construct() 0 4 1
A latest() 0 8 1
A query() 0 4 1
A get() 0 4 1
A paginate() 0 29 2
B getPercentileForUrl() 0 29 6
A getAll() 0 4 1
A insert() 0 4 1
A delete() 0 4 1
A truncate() 0 4 1
A _wrap() 0 16 4
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
//        if (isset($options['projection'])) {
70
//            if ($options['projection'] === true) {
71
//                $projection = array('meta' => 1, 'profile.main()' => 1);
72
//            } else {
73
//                $projection = $options['projection'];
74
//            }
75
//        }
76
77
        if ($projection === false) {
78
            $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...
79
        } else {
80
            $result = $this->storage->find($filter, $projection);
81
        }
82
83
        $totalRows = $this->storage->count($filter);
84
        $totalPages = max(ceil($totalRows / $filter->getPerPage()), 1);
85
86
        return array(
87
            'results'       => $this->_wrap($result),
88
            'sort'          => $filter->getSort(),
89
            'direction'     => $filter->getDirection(),
90
            'page'          => $filter->getPage(),
91
            'perPage'       => $filter->getPerPage(),
92
            'totalPages'    => $totalPages
93
        );
94
    }
95
96
    /**
97
     * Get the Percentile metrics for a URL
98
     *
99
     * This will group data by date and returns only the
100
     * percentile + date, making the data ideal for time series graphs
101
     *
102
     * @param integer $percentile The percentile you want. e.g. 90.
103
     * @param string $url
104
     * @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...
105
     * @return array Array of metrics grouped by date
106
     */
107
    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...
108
    {
109
        $col = '$meta.request_date';
110
//        if (!empty($search['limit']) && $search['limit'][0] == "P") {
111
//            $col = '$meta.request_ts';
112
//        }
113
114
        $results = $this->storage->aggregate($filter, $col, $percentile);
115
        if (empty($results['result'])) {
116
            return array();
117
        }
118
        $keys = array(
119
            'wall_times'    => 'wt',
120
            'cpu_times'     => 'cpu',
121
            'mu_times'      => 'mu',
122
            'pmu_times'     => 'pmu'
123
        );
124
        foreach ($results['result'] as &$result) {
125
            $result['date'] = ($result['_id'] instanceof MongoDate) ? date('Y-m-d H:i:s', $result['_id']->sec) : $result['_id'];
126
            unset($result['_id']);
127
            $index = max(round($result['raw_index']) - 1, 0);
128
            foreach ($keys as $key => $out) {
129
                sort($result[$key]);
130
                $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null;
131
                unset($result[$key]);
132
            }
133
        }
134
        return array_values($results['result']);
135
    }
136
137
    /**
138
     * Get a paginated set of results.
139
     *
140
     * @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...
141
     * @return array An array of result data.
142
     */
143
    public function getAll($filter)
144
    {
145
        return $this->paginate($filter);
146
    }
147
148
    /**
149
     * Insert a profile run.
150
     *
151
     * Does unchecked inserts.
152
     *
153
     * @param array $profile The profile data to save.
154
     * @return
155
     */
156
    public function insert($profile)
157
    {
158
        return $this->storage->insert($profile);
159
    }
160
161
    /**
162
     * Delete a profile run.
163
     *
164
     * @param string $id The profile id to delete.
165
     * @return array|bool
166
     */
167
    public function delete($id)
168
    {
169
        return $this->storage->remove($id);
170
    }
171
172
    /**
173
     * Used to truncate a collection.
174
     *
175
     * Primarly used in test cases to reset the test db.
176
     *
177
     * @return boolean
178
     */
179
    public function truncate()
180
    {
181
        return $this->storage->drop();
182
    }
183
184
    /**
185
     * Converts arrays + MongoCursors into Xhgui_Profile instances.
186
     *
187
     * @param array|MongoCursor $data The data to transform.
188
     * @return Xhgui_Profile|array The transformed/wrapped results.
189
     * @throws Exception
190
     */
191
    protected function _wrap($data)
192
    {
193
        if ($data === null) {
194
            throw new Exception('No profile data found.');
195
        }
196
197
        if (!($data instanceof \Xhgui_Storage_ResultSet)) {
198
            return new Xhgui_Profile($data, true);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 191 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...
199
        }
200
201
        $results = array();
202
        foreach ($data as $row) {
203
            $results[] = new Xhgui_Profile($row, true);
204
        }
205
        return $results;
206
    }
207
}
208