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

TraitAnalyzer::analyze()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 79
Code Lines 50

Duplication

Lines 79
Ratio 100 %

Code Coverage

Tests 49
CRAP Score 9

Importance

Changes 0
Metric Value
dl 79
loc 79
ccs 49
cts 49
cp 1
rs 5.6693
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\TraitCaseChanged;
7
use PHPSemVerChecker\Operation\TraitRemoved;
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
	/**
14
	 * @var string
15
	 */
16
	protected $context = 'trait';
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
		$traitsBefore = $registryBefore->data['trait'];
28 5
		$traitsAfter = $registryAfter->data['trait'];
29
30 5
		$traitsBeforeKeyed = [];
31 5
		$filesBeforeKeyed = [];
32 5
		foreach ($traitsBefore as $key => $traitBefore) {
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 3
			$traitsAfterKeyed[strtolower($traitAfter->name)] = $traitAfter;
41 3
			$filesAfterKeyed[strtolower($traitAfter->name)] = $registryAfter->mapping['trait'][$key];
42
		}
43
44 5
		$traitNamesBefore = array_keys($traitsBeforeKeyed);
45 5
		$traitNamesAfter = array_keys($traitsAfterKeyed);
46 5
		$added = array_diff($traitNamesAfter, $traitNamesBefore);
47 5
		$removed = array_diff($traitNamesBefore, $traitNamesAfter);
48 5
		$toVerify = array_intersect($traitNamesBefore, $traitNamesAfter);
49
50 5
		foreach ($removed as $key) {
51 1
			$fileBefore = $filesBeforeKeyed[$key];
52 1
			$traitBefore = $traitsBeforeKeyed[$key];
53
54 1
			$data = new TraitRemoved($fileBefore, $traitBefore);
55 1
			$report->addTrait($data);
56
		}
57
58 5
		foreach ($toVerify as $key) {
59 2
			$fileBefore = $filesBeforeKeyed[$key];
60 2
			$traitBefore = $traitsBeforeKeyed[$key];
61 2
			$fileAfter = $filesAfterKeyed[$key];
62 2
			$traitAfter = $traitsAfterKeyed[$key];
63
64
			// Leave non-strict comparison here
65 2
			if ($traitBefore != $traitAfter) {
66
				// Check for name case change.
67
				// If we entered this section then the normalized names (lowercase) were equal.
68 1
				if ($traitBefore->name !== $traitAfter->name) {
69 1
					$report->add(
70 1
						$this->context,
71 1
						new TraitCaseChanged(
72 1
							$fileBefore,
73 1
							$traitBefore,
74 1
							$fileAfter,
75 1
							$traitAfter
76
						)
77
					);
78
				}
79
80
				$analyzers = [
81 1
					new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
82 1
					new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
83
				];
84
85 1
				foreach ($analyzers as $analyzer) {
86 1
					$internalReport = $analyzer->analyze($traitBefore, $traitAfter);
87 2
					$report->merge($internalReport);
88
				}
89
			}
90
		}
91
92 5
		foreach ($added as $key) {
93 1
			$fileAfter = $filesAfterKeyed[$key];
94 1
			$traitAfter = $traitsAfter[$key];
95
96 1
			$data = new TraitAdded($fileAfter, $traitAfter);
97 1
			$report->addTrait($data);
98
		}
99
100 5
		return $report;
101
	}
102
}
103