QueryCounterBehavior   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFind() 0 6 1
A afterFind() 0 16 4
A queryCount() 0 3 1
1
<?php
2
3
class QueryCounterBehavior extends ModelBehavior {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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