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

TraitAnalyzer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C analyze() 82 82 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\TraitAdded;
6
use PHPSemVerChecker\Operation\TraitRemoved;
7
use PHPSemVerChecker\Operation\TraitCaseChanged;
8
use PHPSemVerChecker\Registry\Registry;
9
use PHPSemVerChecker\Report\Report;
10
11 View Code Duplication
class TraitAnalyzer {
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 = 'trait';
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
		$traitsBefore = $registryBefore->data['trait'];
27 5
		$traitsAfter = $registryAfter->data['trait'];
28
29 5
		$traitsBeforeKeyed = [];
30 5
		$filesBeforeKeyed = [];
31 5
		foreach($traitsBefore as $key => $traitBefore)
32
		{
33 3
			$traitsBeforeKeyed[strtolower($traitBefore->name)] = $traitBefore;
34 3
			$filesBeforeKeyed[strtolower($traitBefore->name)] = $registryBefore->mapping['trait'][$key];
35 5
		}
36
37 5
		$traitsAfterKeyed = [];
38 5
		$filesAfterKeyed = [];
39 5
		foreach($traitsAfter as $key => $traitAfter)
40
		{
41 3
			$traitsAfterKeyed[strtolower($traitAfter->name)] = $traitAfter;
42 3
			$filesAfterKeyed[strtolower($traitAfter->name)] = $registryAfter->mapping['trait'][$key];
43 5
		}
44
45 5
		$traitNamesBefore = array_keys($traitsBeforeKeyed);
46 5
		$traitNamesAfter = array_keys($traitsAfterKeyed);
47 5
		$added = array_diff($traitNamesAfter, $traitNamesBefore);
48 5
		$removed = array_diff($traitNamesBefore, $traitNamesAfter);
49 5
		$toVerify = array_intersect($traitNamesBefore, $traitNamesAfter);
50
51 5
		foreach ($removed as $key) {
52 1
			$fileBefore = $filesBeforeKeyed[$key];
53 1
			$traitBefore = $traitsBeforeKeyed[$key];
54
55 1
			$data = new TraitRemoved($fileBefore, $traitBefore);
56 1
			$report->addTrait($data);
57 5
		}
58
59 5
		foreach ($toVerify as $key) {
60 2
			$fileBefore = $filesBeforeKeyed[$key];
61 2
			$traitBefore = $traitsBeforeKeyed[$key];
62 2
			$fileAfter = $filesAfterKeyed[$key];
63 2
			$traitAfter = $traitsAfterKeyed[$key];
64
65
			// Leave non-strict comparison here
66 2
			if ($traitBefore != $traitAfter) {
67
68
				// Check for name case change.
69
				// If we entered this section then the normalized names (lowercase) were equal.
70 1
				if ($traitBefore->name !== $traitAfter->name) {
71 1
					$report->add(
72 1
						$this->context,
73 1
						new TraitCaseChanged(
74 1
							$fileBefore,
75 1
							$traitBefore,
76 1
							$fileAfter,
77
							$traitAfter
78 1
						)
79 1
					);
80 1
				}
81
82
				$analyzers = [
83 1
					new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
84 1
					new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
85 1
				];
86
87 1
				foreach ($analyzers as $analyzer) {
88 1
					$internalReport = $analyzer->analyze($traitBefore, $traitAfter);
89 1
					$report->merge($internalReport);
90 1
				}
91 1
			}
92 5
		}
93
94 5
		foreach ($added as $key) {
95 1
			$fileAfter = $filesAfterKeyed[$key];
96 1
			$traitAfter = $traitsAfter[$key];
97
98 1
			$data = new TraitAdded($fileAfter, $traitAfter);
99 1
			$report->addTrait($data);
100 5
		}
101
102 5
		return $report;
103
	}
104
}
105