Failed Conditions
Push — master ( 9ab98a...04484b )
by Alexander
03:08
created

ConstantDataCollector::collectData()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 25
cts 25
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 * This file is part of the Code-Insight library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\KnowledgeBase\DataCollector;
12
13
14
use Go\ParserReflection\ReflectionFileNamespace;
15
16
class ConstantDataCollector extends AbstractDataCollector
17
{
18
19
	/**
20
	 * Collect data from a namespace.
21
	 *
22
	 * @param integer                 $file_id   File id.
23
	 * @param ReflectionFileNamespace $namespace Namespace.
24
	 *
25
	 * @return void
26
	 */
27 3
	public function collectData($file_id, ReflectionFileNamespace $namespace)
28
	{
29 3
		$constants = $namespace->getConstants(true);
30
31
		$sql = 'SELECT Name
32
				FROM Constants
33 3
				WHERE FileId = :file_id';
34 3
		$old_constants = $this->db->fetchCol($sql, array(
35 3
			'file_id' => $file_id,
36 3
		));
37
38 3
		$insert_sql = 'INSERT INTO Constants (FileId, Name, Value) VALUES (:file_id, :name, :value)';
39 3
		$update_sql = 'UPDATE Constants SET Value = :value WHERE FileId = :file_id AND Name = :name';
40
41 3
		foreach ( $constants as $constant_name => $constant_value ) {
42 3
			$this->db->perform(
43 3
				in_array($constant_name, $old_constants) ? $update_sql : $insert_sql,
44
				array(
45 3
					'file_id' => $file_id,
46 3
					'name' => $constant_name,
47 3
					'value' => json_encode($constant_value),
48
				)
49 3
			);
50 3
		}
51
52 3
		$delete_constants = array_diff($old_constants, array_keys($constants));
53
54 3
		if ( $delete_constants ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $delete_constants of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
			$sql = 'DELETE FROM Constants
56 1
					WHERE FileId = :file_id AND Name IN (:names)';
57 1
			$this->db->perform($sql, array(
58 1
				'file_id' => $file_id,
59 1
				'names' => $delete_constants,
60 1
			));
61 1
		}
62 3
	}
63
64
	/**
65
	 * Delete previously collected data for a files.
66
	 *
67
	 * @param array $file_ids File IDs.
68
	 *
69
	 * @return void
70
	 */
71 1
	public function deleteData(array $file_ids)
72
	{
73
		$sql = 'DELETE FROM Constants
74 1
				WHERE FileId IN (:file_ids)';
75 1
		$this->db->perform($sql, array(
76 1
			'file_ids' => $file_ids,
77 1
		));
78 1
	}
79
80
	/**
81
	 * Returns statistics about the code.
82
	 *
83
	 * @return array
84
	 */
85 1
	public function getStatistics()
86
	{
87
		$sql = 'SELECT COUNT(*)
88 1
				FROM Constants';
89 1
		$constant_count = $this->db->fetchValue($sql);
90
91
		return array(
92 1
			'Constants' => $constant_count,
93 1
		);
94
	}
95
96
}
97