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

ClassAnalyzer::analyze()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 84
Code Lines 50

Duplication

Lines 84
Ratio 100 %

Code Coverage

Tests 59
CRAP Score 9

Importance

Changes 0
Metric Value
dl 84
loc 84
ccs 59
cts 59
cp 1
rs 5.4349
c 0
b 0
f 0
cc 9
eloc 50
nc 96
nop 2
crap 9

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\ClassAdded;
6
use PHPSemVerChecker\Operation\ClassRemoved;
7
use PHPSemVerChecker\Operation\ClassCaseChanged;
8
use PHPSemVerChecker\Registry\Registry;
9
use PHPSemVerChecker\Report\Report;
10
11 View Code Duplication
class ClassAnalyzer {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
12
	/**
13
	 * @var string
14
	 */
15
	protected $context = 'class';
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
		$classesBefore = $registryBefore->data['class'];
27 5
		$classesAfter = $registryAfter->data['class'];
28
29 5
		$classesBeforeKeyed = [];
30 5
		$filesBeforeKeyed = [];
31 5
		foreach($classesBefore as $key => $classBefore)
32
		{
33 3
			$classesBeforeKeyed[strtolower($classBefore->name)] = $classBefore;
34 3
			$filesBeforeKeyed[strtolower($classBefore->name)] = $registryBefore->mapping['class'][$key];
35 5
		}
36
37 5
		$classesAfterKeyed = [];
38 5
		$filesAfterKeyed = [];
39 5
		foreach($classesAfter as $key => $classAfter)
40
		{
41 3
			$classesAfterKeyed[strtolower($classAfter->name)] = $classAfter;
42 3
			$filesAfterKeyed[strtolower($classAfter->name)] = $registryAfter->mapping['class'][$key];
43 5
		}
44
45 5
		$classNamesBefore = array_keys($classesBeforeKeyed);
46 5
		$classNamesAfter = array_keys($classesAfterKeyed);
47 5
		$added = array_diff($classNamesAfter, $classNamesBefore);
48 5
		$removed = array_diff($classNamesBefore, $classNamesAfter);
49 5
		$toVerify = array_intersect($classNamesBefore, $classNamesAfter);
50
51 5
		foreach ($removed as $key) {
52 1
			$fileBefore = $filesBeforeKeyed[$key];
53 1
			$classBefore = $classesBeforeKeyed[$key];
54
55 1
			$data = new ClassRemoved($fileBefore, $classBefore);
56 1
			$report->addClass($data);
57 5
		}
58
59 5
		foreach ($toVerify as $key) {
60 2
			$fileBefore = $filesBeforeKeyed[$key];
61
			/** @var \PhpParser\Node\Stmt\Class_ $classBefore */
62 2
			$classBefore = $classesBeforeKeyed[$key];
63 2
			$fileAfter = $filesAfterKeyed[$key];
64
			/** @var \PhpParser\Node\Stmt\Class_ $classBefore */
65 2
			$classAfter = $classesAfterKeyed[$key];
66
67
			// Leave non-strict comparison here
68 2
			if ($classBefore != $classAfter) {
69
70
				// Check for case change of class name.
71
				// If we entered this section then the normalized names (lowercase) were equal.
72 1
				if ($classBefore->name !== $classAfter->name) {
73 1
					$report->add(
74 1
						$this->context,
75 1
						new ClassCaseChanged(
76 1
							$fileBefore,
77 1
							$classBefore,
78 1
							$fileAfter,
79
							$classAfter
80 1
						)
81 1
					);
82 1
				}
83
84
				$analyzers = [
85 1
					new ClassMethodAnalyzer('class', $fileBefore, $fileAfter),
86 1
					new PropertyAnalyzer('class', $fileBefore, $fileAfter),
87 1
				];
88
89 1
				foreach ($analyzers as $analyzer) {
90 1
					$internalReport = $analyzer->analyze($classBefore, $classAfter);
91 1
					$report->merge($internalReport);
92 1
				}
93 1
			}
94 5
		}
95
96 5
		foreach ($added as $key) {
97 1
			$fileAfter = $filesAfterKeyed[$key];
98 1
			$classAfter = $classesAfterKeyed[$key];
99
100 1
			$data = new ClassAdded($fileAfter, $classAfter);
101 1
			$report->addClass($data);
102 5
		}
103
104 5
		return $report;
105
	}
106
}
107