Completed
Pull Request — master (#99)
by
unknown
02:14
created

InterfaceAnalyzer::analyze()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 79
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 8

Importance

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