InterfaceAnalyzer::analyze()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 8

Importance

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