Completed
Pull Request — master (#99)
by
unknown
06:39
created

InterfaceAnalyzer::analyze()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 41
cts 41
cp 1
rs 6.4391
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 5
		$interfaceNamesBefore = array_keys($interfacesBeforeKeyed);
38 5
		$interfaceNamesAfter = array_keys($interfacesAfterKeyed);
39 5
		$added = array_diff($interfaceNamesAfter, $interfaceNamesBefore);
40 5
		$removed = array_diff($interfaceNamesBefore, $interfaceNamesAfter);
41 5
		$toVerify = array_intersect($interfaceNamesBefore, $interfaceNamesAfter);
42
43 5
		foreach ($removed as $key) {
44 1
			$fileBefore = $mappingsBeforeKeyed[$key];
45 1
			$interfaceBefore = $interfacesBeforeKeyed[$key];
46
47 1
			$data = new InterfaceRemoved($fileBefore, $interfaceBefore);
48 1
			$report->addInterface($data);
49
		}
50
51 5
		foreach ($toVerify as $key) {
52 2
			$fileBefore = $mappingsBeforeKeyed[$key];
53
			/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */
54 2
			$interfaceBefore = $interfacesBeforeKeyed[$key];
55 2
			$fileAfter = $mappingsAfterKeyed[$key];
56
			/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */
57 2
			$interfaceAfter = $interfacesAfterKeyed[$key];
58
59
			// Leave non-strict comparison here
60 2
			if ($interfaceBefore != $interfaceAfter) {
61
62
				// Check if the name of the interface has changed case.
63 1
				if($interfaceBefore->name !== $interfaceAfter->name)
64
				{
65 1
					$report->add('interface', new InterfaceRenamedCaseOnly($fileAfter, $interfaceAfter));
66
				}
67
68 1
				$analyzer = new ClassMethodAnalyzer('interface', $fileBefore, $fileAfter);
69 1
				$interfaceMethodReport = $analyzer->analyze($interfaceBefore, $interfaceAfter);
70 2
				$report->merge($interfaceMethodReport);
71
			}
72
		}
73
74 5
		foreach ($added as $key) {
75
76 1
			$fileAfter = $mappingsAfterKeyed[$key];
77 1
			$interfaceAfter = $interfacesAfterKeyed[$key];
78
79 1
			$data = new InterfaceAdded($fileAfter, $interfaceAfter);
80 1
			$report->addInterface($data);
81
		}
82
83 5
		return $report;
84
	}
85
}
86