Completed
Push — master ( 0e1c32...e580ff )
by T
05:02
created

TraitAnalyzer::analyze()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 50
Code Lines 30

Duplication

Lines 50
Ratio 100 %

Code Coverage

Tests 25
CRAP Score 6.0944

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 50
loc 50
ccs 25
cts 29
cp 0.8621
rs 8.6315
cc 6
eloc 30
nc 16
nop 2
crap 6.0944
1
<?php
2
3
namespace PHPSemVerChecker\Analyzer;
4
5
use PHPSemVerChecker\Operation\TraitAdded;
6
use PHPSemVerChecker\Operation\TraitRemoved;
7
use PHPSemVerChecker\Registry\Registry;
8
use PHPSemVerChecker\Report\Report;
9
10 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...
11
	protected $context = 'trait';
12
13 4
	public function analyze(Registry $registryBefore, Registry $registryAfter)
14
	{
15 4
		$report = new Report();
16
17 4
		$keysBefore = array_keys($registryBefore->data['trait']);
18 4
		$keysAfter = array_keys($registryAfter->data['trait']);
19 4
		$added = array_diff($keysAfter, $keysBefore);
20 4
		$removed = array_diff($keysBefore, $keysAfter);
21 4
		$toVerify = array_intersect($keysBefore, $keysAfter);
22
23 4
		foreach ($removed as $key) {
24 1
			$fileBefore = $registryBefore->mapping['trait'][$key];
25 1
			$traitBefore = $registryBefore->data['trait'][$key];
26
27 1
			$data = new TraitRemoved($fileBefore, $traitBefore);
28 1
			$report->addTrait($data);
29
		}
30
31 4
		foreach ($toVerify as $key) {
32 1
			$fileBefore = $registryBefore->mapping['trait'][$key];
33
			/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
34 1
			$traitBefore = $registryBefore->data['trait'][$key];
35 1
			$fileAfter = $registryAfter->mapping['trait'][$key];
36
			/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
37 1
			$traitAfter = $registryAfter->data['trait'][$key];
38
39
			// Leave non-strict comparison here
40 1
			if ($traitBefore != $traitAfter) {
41
				$analyzers = [
42
					new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
43
					new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
44
				];
45
46
				foreach ($analyzers as $analyzer) {
47
					$internalReport = $analyzer->analyze($traitBefore, $traitAfter);
48 1
					$report->merge($internalReport);
49
				}
50
			}
51
		}
52
53 4
		foreach ($added as $key) {
54 1
			$fileAfter = $registryAfter->mapping['trait'][$key];
55 1
			$traitAfter = $registryAfter->data['trait'][$key];
56
57 1
			$data = new TraitAdded($fileAfter, $traitAfter);
58 1
			$report->addTrait($data);
59
		}
60
61 4
		return $report;
62
	}
63
}
64