Completed
Pull Request — master (#82)
by Graham
15:21 queued 09:41
created

FunctionAnalyzer::analyze()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 63
Code Lines 38

Duplication

Lines 10
Ratio 15.87 %

Code Coverage

Tests 38
CRAP Score 8

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 10
loc 63
ccs 38
cts 38
cp 1
rs 6.8825
cc 8
eloc 38
nc 24
nop 2
crap 8

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\Signature;
6
use PHPSemVerChecker\Operation\FunctionAdded;
7
use PHPSemVerChecker\Operation\FunctionImplementationChanged;
8
use PHPSemVerChecker\Operation\FunctionParameterChanged;
9
use PHPSemVerChecker\Operation\FunctionParameterNameChanged;
10
use PHPSemVerChecker\Operation\FunctionRemoved;
11
use PHPSemVerChecker\Operation\Unknown;
12
use PHPSemVerChecker\Registry\Registry;
13
use PHPSemVerChecker\Report\Report;
14
15
class FunctionAnalyzer {
16
	protected $context = 'function';
17
18
	/**
19
	 * @param \PHPSemVerChecker\Registry\Registry $registryBefore
20
	 * @param \PHPSemVerChecker\Registry\Registry $registryAfter
21
	 * @return \PHPSemVerChecker\Report\Report
22
	 */
23 10
	public function analyze(Registry $registryBefore, Registry $registryAfter)
24
	{
25 10
		$report = new Report();
26
27 10
		$keysBefore = array_keys($registryBefore->data['function']);
28 10
		$keysAfter = array_keys($registryAfter->data['function']);
29 10
		$added = array_diff($keysAfter, $keysBefore);
30 10
		$removed = array_diff($keysBefore, $keysAfter);
31 10
		$toVerify = array_intersect($keysBefore, $keysAfter);
32
33 10
		foreach ($removed as $key) {
34 1
			$fileBefore = $registryBefore->mapping['function'][$key];
35 1
			$functionBefore = $registryBefore->data['function'][$key];
36
37 1
			$data = new FunctionRemoved($fileBefore, $functionBefore);
38 1
			$report->addFunction($data);
39
		}
40
41 10
		foreach ($toVerify as $key) {
42 7
			$fileBefore = $registryBefore->mapping['function'][$key];
43 7
			$functionBefore = $registryBefore->data['function'][$key];
44 7
			$fileAfter = $registryAfter->mapping['function'][$key];
45 7
			$functionAfter = $registryAfter->data['function'][$key];
46
47
			// Leave non-strict comparison here
48 7
			if ($functionBefore != $functionAfter) {
49 4
				$paramsBefore = $functionBefore->params;
50 4
				$paramsAfter = $functionAfter->params;
51
				// Signature
52
53 4 View Code Duplication
				if ( ! Signature::isSameTypehints($paramsBefore, $paramsAfter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54 2
					$data = new FunctionParameterChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
55 2
					$report->addFunction($data);
56 2
					continue;
57
				}
58
59 2 View Code Duplication
				if ( ! Signature::isSameVariables($paramsBefore, $paramsAfter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60 1
					$data = new FunctionParameterNameChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
61 1
					$report->addFunction($data);
62 1
					continue;
63
				}
64
65
				// Different length (considering params with defaults)
66
67
				// Difference in source code
68 1
				if ($functionBefore->stmts != $functionAfter->stmts) {
69 1
					$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
70 1
					$report->addFunction($data);
71 4
					continue;
72
				}
73
			}
74
		}
75
76 10
		foreach ($added as $key) {
77 1
			$fileAfter = $registryAfter->mapping['function'][$key];
78 1
			$functionAfter = $registryAfter->data['function'][$key];
79
80 1
			$data = new FunctionAdded($fileAfter, $functionAfter);
81 1
			$report->addFunction($data);
82
		}
83
84 10
		return $report;
85
	}
86
}
87