Completed
Push — master ( c896df...0e1c32 )
by T
04:38
created

FunctionAnalyzer::analyze()   C

Complexity

Conditions 9
Paths 40

Size

Total Lines 72
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 9

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 72
ccs 53
cts 53
cp 1
rs 6.0413
cc 9
eloc 47
nc 40
nop 2
crap 9

How to fix   Long Method   

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\Unknown;
21
use PHPSemVerChecker\Registry\Registry;
22
use PHPSemVerChecker\Report\Report;
23
24
class FunctionAnalyzer {
25
	protected $context = 'function';
26
27
	/**
28
	 * @param \PHPSemVerChecker\Registry\Registry $registryBefore
29
	 * @param \PHPSemVerChecker\Registry\Registry $registryAfter
30
	 * @return \PHPSemVerChecker\Report\Report
31
	 */
32 16
	public function analyze(Registry $registryBefore, Registry $registryAfter)
33
	{
34 16
		$report = new Report();
35
36 16
		$keysBefore = array_keys($registryBefore->data['function']);
37 16
		$keysAfter = array_keys($registryAfter->data['function']);
38 16
		$added = array_diff($keysAfter, $keysBefore);
39 16
		$removed = array_diff($keysBefore, $keysAfter);
40 16
		$toVerify = array_intersect($keysBefore, $keysAfter);
41
42 16
		foreach ($removed as $key) {
43 1
			$fileBefore = $registryBefore->mapping['function'][$key];
44 1
			$functionBefore = $registryBefore->data['function'][$key];
45
46 1
			$data = new FunctionRemoved($fileBefore, $functionBefore);
47 1
			$report->addFunction($data);
48 16
		}
49
50 16
		foreach ($toVerify as $key) {
51 13
			$fileBefore = $registryBefore->mapping['function'][$key];
52 13
			$functionBefore = $registryBefore->data['function'][$key];
53 13
			$fileAfter = $registryAfter->mapping['function'][$key];
54 13
			$functionAfter = $registryAfter->data['function'][$key];
55
56
			// Leave non-strict comparison here
57 13
			if ($functionBefore != $functionAfter) {
58 10
				$paramsBefore = $functionBefore->params;
59 10
				$paramsAfter = $functionAfter->params;
60
61 10
				$signatureResult = Signature::analyze($paramsBefore, $paramsAfter);
62
63
				$changes = [
64 10
					'parameter_added' => FunctionParameterAdded::class,
65 10
					'parameter_removed' => FunctionParameterRemoved::class,
66 10
					'parameter_renamed' => FunctionParameterNameChanged::class,
67 10
					'parameter_typing_added' => FunctionParameterTypingAdded::class,
68 10
					'parameter_typing_removed' => FunctionParameterTypingRemoved::class,
69 10
					'parameter_default_added' => FunctionParameterDefaultAdded::class,
70 10
					'parameter_default_removed' => FunctionParameterDefaultRemoved::class,
71 10
					'parameter_default_value_changed' => FunctionParameterDefaultValueChanged::class,
72 10
				];
73
74 10
				foreach ($changes as $changeType => $class) {
75 10
					if ( ! $signatureResult[$changeType]) {
76 10
						continue;
77
					}
78 9
					if (is_a($class, FunctionOperationUnary::class, true)) {
79 7
						$data = new $class($fileAfter, $functionAfter);
80 7
					} else {
81 3
						$data = new $class($fileBefore, $functionBefore, $fileAfter, $functionAfter);
82
					}
83 9
					$report->addFunction($data);
84 10
				}
85
86
				// Difference in source code
87 10
				if ( ! Implementation::isSame($functionBefore->stmts, $functionAfter->stmts)) {
88 1
					$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
89 1
					$report->addFunction($data);
90 1
				}
91 10
			}
92 16
		}
93
94 16
		foreach ($added as $key) {
95 1
			$fileAfter = $registryAfter->mapping['function'][$key];
96 1
			$functionAfter = $registryAfter->data['function'][$key];
97
98 1
			$data = new FunctionAdded($fileAfter, $functionAfter);
99 1
			$report->addFunction($data);
100 16
		}
101
102 16
		return $report;
103
	}
104
}
105