QueryCounterBehavior::afterFind()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 2
nop 3
1
<?php
2
3
class QueryCounterBehavior extends ModelBehavior {
4
5
	private $runtime = array(); // @codingStandardsIgnoreLine
6
7
/**
8
 * beforeFind
9
 *
10
 * @param Model $model Model
11
 * @param array $query Query
12
 * @return mixed
13
 */
14
	public function beforeFind(Model $model, $query) {
15
		$db = $model->getDataSource();
16
		$log = $db->getLog();
17
		$this->runtime[$model->alias]['count'] = -$log['count'];
18
		return true;
19
	}
20
21
/**
22
 * afterFind 
23
 *
24
 * @param Model $model Model
25
 * @param array $results Results
26
 * @param bool $primary Primary
27
 * @return mixed
28
 */
29
	public function afterFind(Model $model, $results, $primary = false) {
30
		$db = $model->getDataSource();
31
32
		$log = $db->getLog();
33
		$count = $log['count'];
34
35
		if ($db instanceof Sqlite) {
0 ignored issues
show
Bug introduced by
The class Sqlite does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
36
			foreach ($log['log'] as $log) {
37
				if (strpos($log['query'], 'sqlite_master') !== false) {
38
					--$count;
39
				}
40
			}
41
		}
42
43
		$this->runtime[$model->alias]['count'] += $count;
44
	}
45
46
/**
47
 * queryCount
48
 *
49
 * @param Model $model Model
50
 * @return int
51
 */
52
	public function queryCount(Model $model) {
53
		return $this->runtime[$model->alias]['count'];
54
	}
55
56
}
57