Completed
Push — master ( b85bec...862a1a )
by Alexander
05:15
created

FunctionChecker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 109
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
B doCheck() 0 28 3
A getFunctionParameterSignature() 0 16 2
A processFunction() 0 13 2
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\BackwardsCompatibility\Checker;
12
13
14
use Aura\Sql\ExtendedPdoInterface;
15
16
class FunctionChecker extends AbstractChecker
17
{
18
19
	const TYPE_FUNCTION_DELETED = 'function.deleted';
20
	const TYPE_FUNCTION_SIGNATURE_CHANGED = 'function.signature_changed';
21
22
	/**
23
	 * Source function data.
24
	 *
25
	 * @var array
26
	 */
27
	protected $sourceFunctionData = array();
28
29
	/**
30
	 * Target function data.
31
	 *
32
	 * @var array
33
	 */
34
	protected $targetFunctionData = array();
35
36
	/**
37
	 * Returns backwards compatibility checker name.
38
	 *
39
	 * @return string
40
	 */
41 1
	public function getName()
42
	{
43 1
		return 'function';
44
	}
45
46
	/**
47
	 * Collects backwards compatibility violations.
48
	 *
49
	 * @return void
50
	 */
51 2
	protected function doCheck()
52
	{
53
		$sql = 'SELECT Name, Id
54 2
				FROM Functions';
55 2
		$source_functions = $this->sourceDatabase->fetchAssoc($sql);
56 2
		$target_functions = $this->targetDatabase->fetchAssoc($sql);
57
58 2
		foreach ( $source_functions as $source_function_name => $source_function_data ) {
59 2
			if ( !isset($target_functions[$source_function_name]) ) {
60 1
				$this->addIncident(self::TYPE_FUNCTION_DELETED, $source_function_name);
61 1
				continue;
62
			}
63
64 2
			$this->sourceFunctionData = $source_function_data;
65 2
			$this->sourceFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
66 2
				$this->sourceDatabase,
67 2
				$this->sourceFunctionData['Id']
68
			);
69
70 2
			$this->targetFunctionData = $target_functions[$source_function_name];
71 2
			$this->targetFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
72 2
				$this->targetDatabase,
73 2
				$this->targetFunctionData['Id']
74
			);
75
76 2
			$this->processFunction();
77
		}
78 2
	}
79
80
	/**
81
	 * Calculates function parameter signature.
82
	 *
83
	 * @param ExtendedPdoInterface $db          Database.
84
	 * @param integer              $function_id Function ID.
85
	 *
86
	 * @return integer
87
	 */
88 2
	protected function getFunctionParameterSignature(ExtendedPdoInterface $db, $function_id)
89
	{
90
		$sql = 'SELECT *
91
				FROM FunctionParameters
92
				WHERE FunctionId = :function_id
93 2
				ORDER BY Position ASC';
94 2
		$function_parameters = $db->fetchAll($sql, array('function_id' => $function_id));
95
96 2
		$hash_parts = array();
97
98 2
		foreach ( $function_parameters as $function_parameter_data ) {
99 2
			$hash_parts[] = $this->paramToString($function_parameter_data);
100
		}
101
102 2
		return implode(', ', $hash_parts);
103
	}
104
105
	/**
106
	 * Processes function.
107
	 *
108
	 * @return void
109
	 */
110 2
	protected function processFunction()
111
	{
112 2
		$function_name = $this->sourceFunctionData['Name'];
113
114 2
		if ( $this->sourceFunctionData['ParameterSignature'] !== $this->targetFunctionData['ParameterSignature'] ) {
115 1
			$this->addIncident(
116 1
				self::TYPE_FUNCTION_SIGNATURE_CHANGED,
117
				$function_name,
118 1
				$this->sourceFunctionData['ParameterSignature'],
119 1
				$this->targetFunctionData['ParameterSignature']
120
			);
121
		}
122 2
	}
123
124
}
125