Failed Conditions
Push — master ( e02dc1...26605a )
by Alexander
01:39
created

FunctionChecker::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 2
crap 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;
12
13
14
use Aura\Sql\ExtendedPdoInterface;
15
16
class FunctionChecker extends AbstractChecker
17
{
18
19
	/**
20
	 * Source function data.
21
	 *
22
	 * @var array
23
	 */
24
	protected $sourceFunctionData = array();
25
26
	/**
27
	 * Target function data.
28
	 *
29
	 * @var array
30
	 */
31
	protected $targetFunctionData = array();
32
33
	/**
34
	 * FunctionChecker constructor.
35
	 */
36
	public function __construct()
37
	{
38
		$this->defineIncidentGroups(array(
39
			'Function Deleted',
40
			'Function Signature Changed',
41
		));
42
	}
43
44
	/**
45
	 * Returns backwards compatibility checker name.
46
	 *
47
	 * @return string
48
	 */
49
	public function getName()
50
	{
51
		return 'function';
52
	}
53
54
	/**
55
	 * Collects backwards compatibility violations.
56
	 *
57
	 * @return void
58
	 */
59
	protected function doCheck()
60
	{
61
		$sql = 'SELECT Name, Id
62
				FROM Functions';
63
		$source_functions = $this->sourceDatabase->fetchAssoc($sql);
64
		$target_functions = $this->targetDatabase->fetchAssoc($sql);
65
66
		foreach ( $source_functions as $source_function_name => $source_function_data ) {
67
			if ( !isset($target_functions[$source_function_name]) ) {
68
				$this->addIncident('Function Deleted', $source_function_name);
69
				continue;
70
			}
71
72
			$this->sourceFunctionData = $source_function_data;
73
			$this->sourceFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
74
				$this->sourceDatabase,
75
				$this->sourceFunctionData['Id']
76
			);
77
78
			$this->targetFunctionData = $target_functions[$source_function_name];
79
			$this->targetFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
80
				$this->targetDatabase,
81
				$this->targetFunctionData['Id']
82
			);
83
84
			$this->processFunction();
85
		}
86
	}
87
88
	/**
89
	 * Calculates function parameter signature.
90
	 *
91
	 * @param ExtendedPdoInterface $db          Database.
92
	 * @param integer              $function_id Function ID.
93
	 *
94
	 * @return integer
95
	 */
96
	protected function getFunctionParameterSignature(ExtendedPdoInterface $db, $function_id)
1 ignored issue
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
97
	{
98
		$sql = 'SELECT *
99
				FROM FunctionParameters
100
				WHERE FunctionId = :function_id
101
				ORDER BY Position ASC';
102
		$function_parameters = $db->fetchAll($sql, array('function_id' => $function_id));
103
104
		$hash_parts = array();
105
106
		foreach ( $function_parameters as $function_parameter_data ) {
107
			$hash_parts[] = $this->paramToString($function_parameter_data);
108
		}
109
110
		return implode(', ', $hash_parts);
111
	}
112
113
	/**
114
	 * Processes function.
115
	 *
116
	 * @return void
117
	 */
118
	protected function processFunction()
119
	{
120
		$function_name = $this->sourceFunctionData['Name'];
121
122
		if ( $this->sourceFunctionData['ParameterSignature'] !== $this->targetFunctionData['ParameterSignature'] ) {
123
			$this->addIncident(
124
				'Function Signature Changed',
125
				$function_name,
126
				$this->sourceFunctionData['ParameterSignature'],
127
				$this->targetFunctionData['ParameterSignature']
128
			);
129
		}
130
	}
131
132
}
133