Completed
Push — master ( 01705b...10ec3f )
by Morris
15:02 queued 02:44
created

QueryLogger::getStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 * @author Piotr Mrowczynski <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Diagnostics;
27
28
use OC\Cache\CappedMemoryCache;
29
use OCP\Diagnostics\IQueryLogger;
30
31
class QueryLogger implements IQueryLogger {
32
	/**
33
	 * @var \OC\Diagnostics\Query
34
	 */
35
	protected $activeQuery;
36
37
	/**
38
	 * @var \OC\Diagnostics\Query[]
39
	 */
40
	protected $queries;
41
42
	/**
43
	 * QueryLogger constructor.
44
	 */
45
	public function __construct() {
46
		$this->queries = new CappedMemoryCache(1024);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \OC\Cache\CappedMemoryCache(1024) of type object<OC\Cache\CappedMemoryCache> is incompatible with the declared type array<integer,object<OC\Diagnostics\Query>> of property $queries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
	}
48
49
50
	/**
51
	 * @var bool - Module needs to be activated by some app
52
	 */
53
	private $activated = false;
54
55
	/**
56
	 * @inheritdoc
57
	 */
58
	public function startQuery($sql, array $params = null, array $types = null) {
59
		if ($this->activated) {
60
			$this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 58 can also be of type null; however, OC\Diagnostics\Query::__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...
61
		}
62
	}
63
64
	private function getStack() {
65
		$stack = debug_backtrace();
66
		array_shift($stack);
67
		array_shift($stack);
68
		array_shift($stack);
69
		return $stack;
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75
	public function stopQuery() {
76
		if ($this->activated && $this->activeQuery) {
77
			$this->activeQuery->end(microtime(true));
78
			$this->queries[] = $this->activeQuery;
79
			$this->activeQuery = null;
80
		}
81
	}
82
83
	/**
84
	 * @inheritdoc
85
	 */
86
	public function getQueries() {
87
		return $this->queries->getData();
0 ignored issues
show
Bug introduced by
The method getData cannot be called on $this->queries (of type array<integer,object<OC\Diagnostics\Query>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function activate() {
94
		$this->activated = true;
95
	}
96
}
97