Completed
Push — master ( b6a2ec...b3bd8f )
by Alexander
01:55
created

FunctionChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
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
use Doctrine\Common\Cache\CacheProvider;
16
17
class FunctionChecker extends AbstractChecker
18
{
19
20
	const TYPE_FUNCTION_DELETED = 'function.deleted';
21
	const TYPE_FUNCTION_SIGNATURE_CHANGED = 'function.signature_changed';
22
23
	/**
24
	 * Source function data.
25
	 *
26
	 * @var array
27
	 */
28
	protected $sourceFunctionData = array();
29
30
	/**
31
	 * Target function data.
32
	 *
33
	 * @var array
34
	 */
35
	protected $targetFunctionData = array();
36
37
	/**
38
	 * Constructor.
39
	 *
40
	 * @param CacheProvider $cache Cache provider.
41
	 */
42 3
	public function __construct(CacheProvider $cache)
43
	{
44 3
		parent::__construct($cache);
45
46 3
		$this->typeSorting = array(
47 3
			self::TYPE_FUNCTION_DELETED => 1,
48 3
			self::TYPE_FUNCTION_SIGNATURE_CHANGED => 2,
49
		);
50 3
	}
51
52
	/**
53
	 * Returns backwards compatibility checker name.
54
	 *
55
	 * @return string
56
	 */
57 1
	public function getName()
58
	{
59 1
		return 'function';
60
	}
61
62
	/**
63
	 * Collects backwards compatibility violations.
64
	 *
65
	 * @return void
66
	 */
67 2
	protected function doCheck()
68
	{
69
		$sql = 'SELECT Name, Id
70 2
				FROM Functions';
71 2
		$source_functions = $this->sourceDatabase->fetchAssoc($sql);
72 2
		$target_functions = $this->targetDatabase->fetchAssoc($sql);
73
74 2
		foreach ( $source_functions as $source_function_name => $source_function_data ) {
75 2
			if ( !isset($target_functions[$source_function_name]) ) {
76 1
				$this->addIncident(self::TYPE_FUNCTION_DELETED, $source_function_name);
77 1
				continue;
78
			}
79
80 2
			$this->sourceFunctionData = $source_function_data;
81 2
			$this->sourceFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
82 2
				$this->sourceDatabase,
83 2
				$this->sourceFunctionData['Id']
84 2
			);
85
86 2
			$this->targetFunctionData = $target_functions[$source_function_name];
87 2
			$this->targetFunctionData['ParameterSignature'] = $this->getFunctionParameterSignature(
88 2
				$this->targetDatabase,
89 2
				$this->targetFunctionData['Id']
90 2
			);
91
92 2
			$this->processFunction();
93 2
		}
94 2
	}
95
96
	/**
97
	 * Calculates function parameter signature.
98
	 *
99
	 * @param ExtendedPdoInterface $db          Database.
100
	 * @param integer              $function_id Function ID.
101
	 *
102
	 * @return integer
103
	 */
104 2
	protected function getFunctionParameterSignature(ExtendedPdoInterface $db, $function_id)
105
	{
106
		$sql = 'SELECT *
107
				FROM FunctionParameters
108
				WHERE FunctionId = :function_id
109 2
				ORDER BY Position ASC';
110 2
		$function_parameters = $db->fetchAll($sql, array('function_id' => $function_id));
111
112 2
		$hash_parts = array();
113
114 2
		foreach ( $function_parameters as $function_parameter_data ) {
115 2
			$hash_parts[] = $this->paramToString($function_parameter_data);
116 2
		}
117
118 2
		return implode(', ', $hash_parts);
119
	}
120
121
	/**
122
	 * Processes function.
123
	 *
124
	 * @return void
125
	 */
126 2
	protected function processFunction()
127
	{
128 2
		$function_name = $this->sourceFunctionData['Name'];
129
130 2
		$source_signature = $this->sourceFunctionData['ParameterSignature'];
131 2
		$target_signature = $this->targetFunctionData['ParameterSignature'];
132
133 2
		if ( !$this->isParamSignatureCompatible($source_signature, $target_signature) ) {
134 1
			$this->addIncident(
135 1
				self::TYPE_FUNCTION_SIGNATURE_CHANGED,
136 1
				$function_name,
137 1
				$source_signature,
138
				$target_signature
139 1
			);
140 1
		}
141 2
	}
142
143
}
144