Completed
Pull Request — master (#99)
by
unknown
05:09
created

InterfaceAnalyzer::analyze()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 41
cts 41
cp 1
rs 6.3883
c 0
b 0
f 0
cc 8
eloc 41
nc 64
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\Operation\InterfaceAdded;
6
use PHPSemVerChecker\Operation\InterfaceRemoved;
7
use PHPSemVerChecker\Operation\InterfaceRenamedCaseOnly;
8
use PHPSemVerChecker\Registry\Registry;
9
use PHPSemVerChecker\Report\Report;
10
11
class InterfaceAnalyzer {
12
	protected $context = 'interface';
13
14 5
	public function analyze(Registry $registryBefore, Registry $registryAfter)
15
	{
16 5
		$report = new Report();
17
18 5
		$interfacesBefore = $registryBefore->data['interface'];
19 5
		$interfacesAfter = $registryAfter->data['interface'];
20
21 5
		$interfacesBeforeKeyed = [];
22 5
		$mappingsBeforeKeyed = [];
23 5
		foreach($interfacesBefore as $key => $interfaceBefore)
24
		{
25 3
			$interfacesBeforeKeyed[strtolower($interfaceBefore->name)] = $interfaceBefore;
26 3
			$mappingsBeforeKeyed[strtolower($interfaceBefore->name)] = $registryBefore->mapping['interface'][$key];
27
		}
28
29 5
		$interfacesAfterKeyed = [];
30 5
		$mappingsAfterKeyed = [];
31 5
		foreach($interfacesAfter as $key => $interfaceAfter)
32
		{
33 3
			$interfacesAfterKeyed[strtolower($interfaceAfter->name)] = $interfaceAfter;
34 3
			$mappingsAfterKeyed[strtolower($interfaceAfter->name)] = $registryAfter->mapping['interface'][$key];
35
		}
36
37
38 5
		$interfaceNamesBefore = array_keys($interfacesBeforeKeyed);
39 5
		$interfaceNamesAfter = array_keys($interfacesAfterKeyed);
40 5
		$added = array_diff($interfaceNamesAfter, $interfaceNamesBefore);
41 5
		$removed = array_diff($interfaceNamesBefore, $interfaceNamesAfter);
42 5
		$toVerify = array_intersect($interfaceNamesBefore, $interfaceNamesAfter);
43
44 5
		foreach ($removed as $key) {
45 1
			$fileBefore = $mappingsBeforeKeyed[$key];
46 1
			$interfaceBefore = $interfacesBeforeKeyed[$key];
47
48 1
			$data = new InterfaceRemoved($fileBefore, $interfaceBefore);
49 1
			$report->addInterface($data);
50
		}
51
52 5
		foreach ($toVerify as $key) {
53 2
			$fileBefore = $mappingsBeforeKeyed[$key];
54
			/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */
55 2
			$interfaceBefore = $interfacesBeforeKeyed[$key];
56 2
			$fileAfter = $mappingsAfterKeyed[$key];
57
			/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */
58 2
			$interfaceAfter = $interfacesAfterKeyed[$key];
59
60
			// Leave non-strict comparison here
61 2
			if ($interfaceBefore != $interfaceAfter) {
62
63
				// Check if the name of the interface has changed case.
64 1
				if($interfaceBefore->name !== $interfaceAfter->name)
65
				{
66 1
					$report->add('interface', new InterfaceRenamedCaseOnly($fileAfter, $interfaceAfter));
67
				}
68
69 1
				$analyzer = new ClassMethodAnalyzer('interface', $fileBefore, $fileAfter);
70 1
				$interfaceMethodReport = $analyzer->analyze($interfaceBefore, $interfaceAfter);
71 2
				$report->merge($interfaceMethodReport);
72
			}
73
		}
74
75 5
		foreach ($added as $key) {
76
77 1
			$fileAfter = $mappingsAfterKeyed[$key];
78 1
			$interfaceAfter = $interfacesAfterKeyed[$key];
79
80 1
			$data = new InterfaceAdded($fileAfter, $interfaceAfter);
81 1
			$report->addInterface($data);
82
		}
83
84 5
		return $report;
85
	}
86
}
87