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

TraitAnalyzer::analyze()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 82
Code Lines 50

Duplication

Lines 82
Ratio 100 %

Code Coverage

Tests 49
CRAP Score 9

Importance

Changes 0
Metric Value
dl 82
loc 82
ccs 49
cts 49
cp 1
rs 5.5259
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\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
		}
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
		}
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
		}
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 1
							$traitAfter
78
						)
79
					);
80
				}
81
82
				$analyzers = [
83 1
					new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
84 1
					new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
85
				];
86
87 1
				foreach ($analyzers as $analyzer) {
88 1
					$internalReport = $analyzer->analyze($traitBefore, $traitAfter);
89 2
					$report->merge($internalReport);
90
				}
91
			}
92
		}
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
		}
101
102 5
		return $report;
103
	}
104
}
105