Completed
Pull Request — master (#99)
by
unknown
02:14
created

FunctionAnalyzer::analyze()   F

Complexity

Conditions 12
Paths 288

Size

Total Lines 103
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 55
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 55
cts 55
cp 1
rs 3.7956
c 0
b 0
f 0
cc 12
eloc 64
nc 288
nop 2
crap 12

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PHPSemVerChecker\Analyzer;
4
5
use PHPSemVerChecker\Comparator\Implementation;
6
use PHPSemVerChecker\Comparator\Signature;
7
use PHPSemVerChecker\Operation\FunctionAdded;
8
use PHPSemVerChecker\Operation\FunctionImplementationChanged;
9
use PHPSemVerChecker\Operation\FunctionOperationUnary;
10
use PHPSemVerChecker\Operation\FunctionParameterAdded;
11
use PHPSemVerChecker\Operation\FunctionParameterChanged;
12
use PHPSemVerChecker\Operation\FunctionParameterDefaultAdded;
13
use PHPSemVerChecker\Operation\FunctionParameterDefaultRemoved;
14
use PHPSemVerChecker\Operation\FunctionParameterDefaultValueChanged;
15
use PHPSemVerChecker\Operation\FunctionParameterNameChanged;
16
use PHPSemVerChecker\Operation\FunctionParameterRemoved;
17
use PHPSemVerChecker\Operation\FunctionParameterTypingAdded;
18
use PHPSemVerChecker\Operation\FunctionParameterTypingRemoved;
19
use PHPSemVerChecker\Operation\FunctionRemoved;
20
use PHPSemVerChecker\Operation\FunctionCaseChanged;
21
use PHPSemVerChecker\Registry\Registry;
22
use PHPSemVerChecker\Report\Report;
23
24
class FunctionAnalyzer {
25
	/**
26
	 * @var string
27
	 */
28
	protected $context = 'function';
29
30
	/**
31
	 * @param \PHPSemVerChecker\Registry\Registry $registryBefore
32
	 * @param \PHPSemVerChecker\Registry\Registry $registryAfter
33
	 * @return \PHPSemVerChecker\Report\Report
34
	 */
35 17
	public function analyze(Registry $registryBefore, Registry $registryAfter)
36
	{
37 17
		$report = new Report();
38
39
40 17
		$functionsBefore = $registryBefore->data['function'];
41 17
		$functionsAfter = $registryAfter->data['function'];
42
43 17
		$functionsBeforeKeyed = [];
44 17
		$filesBeforeKeyed = [];
45 17
		foreach($functionsBefore as $key => $functionBefore)
46
		{
47 15
			$functionsBeforeKeyed[strtolower($functionBefore->name)] = $functionBefore;
48 15
			$filesBeforeKeyed[strtolower($functionBefore->name)] = $registryBefore->mapping['function'][$key];
49
		}
50
51 17
		$functionsAfterKeyed = [];
52 17
		$filesAfterKeyed = [];
53 17
		foreach($functionsAfter as $key => $functionAfter)
54
		{
55 15
			$functionsAfterKeyed[strtolower($functionAfter->name)] = $functionAfter;
56 15
			$filesAfterKeyed[strtolower($functionAfter->name)] = $registryAfter->mapping['function'][$key];
57
		}
58
59 17
		$functionNamesBefore = array_keys($functionsBeforeKeyed);
60 17
		$functionNamesAfter = array_keys($functionsAfterKeyed);
61 17
		$added = array_diff($functionNamesAfter, $functionNamesBefore);
62 17
		$removed = array_diff($functionNamesBefore, $functionNamesAfter);
63 17
		$toVerify = array_intersect($functionNamesBefore, $functionNamesAfter);
64
65 17
		foreach ($removed as $key) {
66 1
			$fileBefore = $filesBeforeKeyed[$key];
67 1
			$functionBefore = $functionsBeforeKeyed[$key];
68
69 1
			$data = new FunctionRemoved($fileBefore, $functionBefore);
70 1
			$report->addFunction($data);
71
		}
72
73 17
		foreach ($toVerify as $key) {
74 14
			$fileBefore = $filesBeforeKeyed[$key];
75 14
			$functionBefore = $functionsBeforeKeyed[$key];
76 14
			$fileAfter = $filesAfterKeyed[$key];
77 14
			$functionAfter = $functionsAfterKeyed[$key];
78
79
			// Leave non-strict comparison here
80 14
			if ($functionBefore != $functionAfter) {
81
82
				// Check if the name of the interface has changed case.
83
				// If we entered this section then the normalized names (lowercase) were equal.
84 11
				if ($functionBefore->name !== $functionAfter->name) {
85 1
					$report->addFunction(
86 1
						new FunctionCaseChanged(
87 1
							$fileBefore,
88 1
							$functionBefore,
89 1
							$fileAfter,
90 1
							$functionAfter
91
						)
92
					);
93
				}
94
95 11
				$signatureResult = Signature::analyze($functionBefore->getParams(), $functionAfter->getParams());
96
97
				$changes = [
98 11
					'parameter_added' => FunctionParameterAdded::class,
99
					'parameter_removed' => FunctionParameterRemoved::class,
100
					'parameter_renamed' => FunctionParameterNameChanged::class,
101
					'parameter_typing_added' => FunctionParameterTypingAdded::class,
102
					'parameter_typing_removed' => FunctionParameterTypingRemoved::class,
103
					'parameter_default_added' => FunctionParameterDefaultAdded::class,
104
					'parameter_default_removed' => FunctionParameterDefaultRemoved::class,
105
					'parameter_default_value_changed' => FunctionParameterDefaultValueChanged::class,
106
				];
107
108 11
				foreach ($changes as $changeType => $class) {
109 11
					if ( ! $signatureResult[$changeType]) {
110 11
						continue;
111
					}
112 9
					if (is_a($class, FunctionOperationUnary::class, true)) {
113 7
						$data = new $class($fileAfter, $functionAfter);
114
					} else {
115 3
						$data = new $class($fileBefore, $functionBefore, $fileAfter, $functionAfter);
116
					}
117 9
					$report->addFunction($data);
118
				}
119
120
				// Difference in source code
121 11
				if ( ! Implementation::isSame($functionBefore->stmts, $functionAfter->stmts)) {
122 1
					$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
123 14
					$report->addFunction($data);
124
				}
125
			}
126
		}
127
128 17
		foreach ($added as $key) {
129 1
			$fileAfter = $registryAfter->mapping['function'][$key];
130 1
			$functionAfter = $registryAfter->data['function'][$key];
131
132 1
			$data = new FunctionAdded($fileAfter, $functionAfter);
133 1
			$report->addFunction($data);
134
		}
135
136 17
		return $report;
137
	}
138
}
139