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

ClassAnalyzer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 82
loc 82
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C analyze() 78 78 10

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