Completed
Pull Request — master (#99)
by
unknown
01:44
created

TraitAnalyzer::analyze()   C

Complexity

Conditions 10
Paths 96

Size

Total Lines 78
Code Lines 46

Duplication

Lines 78
Ratio 100 %

Code Coverage

Tests 44
CRAP Score 10

Importance

Changes 0
Metric Value
dl 78
loc 78
ccs 44
cts 44
cp 1
rs 5.6321
c 0
b 0
f 0
cc 10
eloc 46
nc 96
nop 2
crap 10

How to fix   Long Method    Complexity   

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