GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — integration ( a3ab80...7a98f7 )
by Brendan
06:22
created

Profiler::seed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
    /**
4
     * @package toolkit
5
     */
6
7
    /**
8
     * The Profiler class tracks various performance metrics while a Symphony
9
     * page is being generated. It provides a basic stopwatch functionality and
10
     * memory usage statistics. Profiling occurs in both the Frontend and
11
     * Administration execution. The Profiler implements the Singleton interface.
12
     */
13
    class Profiler implements Singleton
14
    {
15
        /**
16
         * Holds the timestamp from when the profiler was first initialised
17
         *
18
         * @var integer
19
         */
20
        protected static $_starttime = 0;
21
22
        /**
23
         * An array of arrays containing profiling samples. A record contains a
24
         * profile message, the time since `$_starttime` timestamp, the end timestamp,
25
         * the group for this record, the number of SQL queries and the result of
26
         * memory_get_usage()
27
         *
28
         * @var array
29
         */
30
        protected static $_samples = array();
31
32
        /**
33
         * A seed holds a start time to be used in profiling. If this is not null
34
         * the profiler will use this as the start time instead of `$_starttime`. This
35
         * is set with the seed function.
36
         *
37
         * @var integer
38
         * @see seed()
39
         */
40
        protected static $_seed = null;
41
42
        /**
43
         * An instance of the Profiler class
44
         *
45
         * @var Profiler
46
         */
47
        protected static $_instance = null;
48
49
        /**
50
         * The constructor for the profile function sets the start time
51
         */
52
        protected function __construct()
53
        {
54
            Profiler::$_starttime = precision_timer();
0 ignored issues
show
Documentation Bug introduced by
The property $_starttime was declared of type integer, but precision_timer() is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
55
        }
56
57
        /**
58
         * Returns the Profiler instance, creating one if it does not exist
59
         *
60
         * @return Profiler
61
         */
62
        public static function instance()
63
        {
64
            if (!(Profiler::$_instance instanceof Profiler)) {
65
                Profiler::$_instance = new self;
66
            }
67
68
            return Profiler::$_instance;
69
        }
70
71
        /**
72
         * Sets the seed to be a timestamp so that time profiling will use this
73
         * as a starting point
74
         *
75
         * @param integer $time
76
         *  The time in seconds
77
         */
78
        public static function seed($time = null)
79
        {
80
            Profiler::$_seed = (is_null($time)) ? precision_timer() : $time;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($time) ? precision_timer() : $time can also be of type double. However, the property $_seed is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
        }
82
83
        /**
84
         * This function creates a new report in the `$_samples` array where the message
85
         * is the name of this report. By default, all samples are compared to the `$_starttime`
86
         * but if the `PROFILE_LAP` constant is passed, it will be compared to specific `$_seed`
87
         * timestamp. Samples can grouped by type (ie. Datasources, Events), but by default
88
         * are grouped by 'General'. Optionally, the number of SQL queries that have occurred
89
         * since either `$_starttime` or `$_seed` can be passed. Memory usage is taken with each
90
         * sample which measures the amount of memory used by this script by PHP at the
91
         * time of sampling.
92
         *
93
         * @param string $msg
94
         *  A description for this sample
95
         * @param integer $type
96
         *  Either `PROFILE_RUNNING_TOTAL` or `PROFILE_LAP`
97
         * @param string $group
98
         *  Allows samples to be grouped together, defaults to General.
99
         * @param integer $queries
100
         *  The number of MySQL queries that occurred since the `$_starttime` or `$_seed`
101
         */
102
        public function sample($msg, $type = PROFILE_RUNNING_TOTAL, $group = 'General', $queries = null)
103
        {
104
            if ($type === PROFILE_RUNNING_TOTAL) {
105
                Profiler::$_samples[] = array(
106
                    $msg,
107
                    precision_timer('stop', Profiler::$_starttime),
108
                    precision_timer(),
109
                    $group,
110
                    $queries,
111
                    memory_get_usage()
112
                );
113
            } else {
114
                if (!is_null(Profiler::$_seed)) {
115
                    $start = Profiler::$_seed;
116
                    Profiler::$_seed = null;
117
                } else {
118
                    $start = null;
119
                }
120
121
                $prev = Profiler::retrieveLast();
122
                Profiler::$_samples[] = array(
123
                    $msg,
124
                    precision_timer('stop', ($start ? $start : $prev[2])),
125
                    precision_timer(),
126
                    $group,
127
                    $queries,
128
                    memory_get_usage()
129
                );
130
            }
131
        }
132
133
        /**
134
         * Returns the last record from the `$_records` array
135
         *
136
         * @return array
137
         */
138
        public static function retrieveLast()
139
        {
140
            return end(Profiler::$_samples);
141
        }
142
143
        /**
144
         * Returns a sample by message, if no sample is found, an empty
145
         * array is returned
146
         *
147
         * @param string $msg
148
         *  The name of the sample to return
149
         * @return array
150
         */
151
        public function retrieveByMessage($msg)
152
        {
153
            foreach (Profiler::$_samples as $record) {
154
                if ($record[0] === $msg) {
155
                    return $record;
156
                }
157
            }
158
159
            return array();
160
        }
161
162
        /**
163
         * Returns all the samples that belong to a particular group.
164
         *
165
         * @param string $group
166
         * @return array
167
         */
168
        public function retrieveGroup($group)
169
        {
170
            $result = array();
171
172
            foreach (Profiler::$_samples as $record) {
173
                if ($record[3] === $group) {
174
                    $result[] = $record;
175
                }
176
            }
177
178
            return $result;
179
        }
180
181
        /**
182
         * Returns the difference between when the Profiler was initialised
183
         * (aka `$_starttime`) and the last record the Profiler has.
184
         *
185
         * @return integer
186
         */
187
        public function retrieveTotalRunningTime()
188
        {
189
            $last = Profiler::retrieveLast();
190
191
            return $last[1];
192
        }
193
194
        /**
195
         * Returns the total memory usage from all samples taken by comparing
196
         * each sample to the base memory sample.
197
         *
198
         * @return integer
199
         *  Memory usage in bytes.
200
         */
201
        public function retrieveTotalMemoryUsage()
202
        {
203
            $base = $this->retrieve(0);
204
            $total = $last = 0;
205
206
            foreach ($this->retrieve() as $item) {
207
                $total += max(0, (($item[5] - $base[5]) - $last));
208
                $last = $item[5] - $base[5];
209
            }
210
211
            return $total;
212
        }
213
214
        /**
215
         * Given an index, return the sample at that position otherwise just
216
         * return all samples.
217
         *
218
         * @param integer $index
219
         *  The array index to return the sample for
220
         * @return array
221
         *  If no `$index` is passed an array of all the sample arrays are returned
222
         *  otherwise just the sample at the given `$index` will be returned.
223
         */
224
        public function retrieve($index = null)
225
        {
226
            return !is_null($index) ? Profiler::$_samples[$index] : Profiler::$_samples;
227
        }
228
    }
229