FunctionAnalyzer::analyze()   D
last analyzed

Complexity

Conditions 12
Paths 288

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 12

Importance

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