Completed
Push — master ( 1cdbe4...17a825 )
by T
02:03
created

ClassAnalyzer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 94
loc 94
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C analyze() 81 81 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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