PropertyAnalyzer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 94
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A analyze() 0 37 5
A getProperties() 0 10 3
1
<?php
2
3
namespace PHPSemVerChecker\Analyzer;
4
5
use PhpParser\Node\Stmt;
6
use PhpParser\Node\Stmt\Property;
7
use PHPSemVerChecker\Operation\PropertyAdded;
8
use PHPSemVerChecker\Operation\PropertyRemoved;
9
use PHPSemVerChecker\Report\Report;
10
11
class PropertyAnalyzer
12
{
13
	/**
14
	 * @var string
15
	 */
16
	protected $context;
17
	/**
18
	 * @var null|string
19
	 */
20
	protected $fileBefore;
21
	/**
22
	 * @var null|string
23
	 */
24
	protected $fileAfter;
25
26
	/**
27
	 * @param string $context
28
	 * @param string $fileBefore
29
	 * @param string $fileAfter
30
	 */
31 8
	public function __construct($context, $fileBefore = null, $fileAfter = null)
32
	{
33 8
		$this->context = $context;
34 8
		$this->fileBefore = $fileBefore;
35 8
		$this->fileAfter = $fileAfter;
36 8
	}
37
38
	/**
39
	 * @param \PhpParser\Node\Stmt $contextBefore
40
	 * @param \PhpParser\Node\Stmt $contextAfter
41
	 * @return \PHPSemVerChecker\Report\Report
42
	 */
43 8
	public function analyze(Stmt $contextBefore, Stmt $contextAfter)
44
	{
45 8
		$report = new Report();
46
47 8
		$propertiesBefore = $this->getProperties($contextBefore);
48 8
		$propertiesAfter = $this->getProperties($contextAfter);
49
50 8
		$propertiesBeforeKeyed = [];
51 8
		foreach ($propertiesBefore as $property) {
52 4
			$propertiesBeforeKeyed[$this->getName($property)] = $property;
53
		}
54
55 8
		$propertiesAfterKeyed = [];
56 8
		foreach ($propertiesAfter as $property) {
57 4
			$propertiesAfterKeyed[$this->getName($property)] = $property;
58
		}
59
60 8
		$propertyNamesBefore = array_keys($propertiesBeforeKeyed);
61 8
		$propertyNamesAfter = array_keys($propertiesAfterKeyed);
62 8
		$propertiesAdded = array_diff($propertyNamesAfter, $propertyNamesBefore);
63 8
		$propertiesRemoved = array_diff($propertyNamesBefore, $propertyNamesAfter);
64 8
		$propertiesToVerify = array_intersect($propertyNamesBefore, $propertyNamesAfter);
0 ignored issues
show
Unused Code introduced by
$propertiesToVerify is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
66 8
		foreach ($propertiesRemoved as $property) {
67 2
			$propertyBefore = $propertiesBeforeKeyed[$property];
68 2
			$data = new PropertyRemoved($this->context, $this->fileBefore, $contextBefore, $propertyBefore);
69 2
			$report->add($this->context, $data);
70
		}
71
72 8
		foreach ($propertiesAdded as $property) {
73 2
			$propertyAfter = $propertiesAfterKeyed[$property];
74 2
			$data = new PropertyAdded($this->context, $this->fileAfter, $contextAfter, $propertyAfter);
75 2
			$report->add($this->context, $data);
76
		}
77
78 8
		return $report;
79
	}
80
81
	/**
82
	 * @param \PhpParser\Node\Stmt $context
83
	 * @return array
84
	 */
85 8
	protected function getProperties(Stmt $context)
86
	{
87 8
		$properties = [];
88 8
		foreach ($context->stmts as $stmt) {
89 6
			if ($stmt instanceof Property) {
90 6
				$properties[] = $stmt;
91
			}
92
		}
93 8
		return $properties;
94
	}
95
96
	/**
97
	 * @param \PhpParser\Node\Stmt\Property $property
98
	 * @return string
99
	 */
100 6
	protected function getName(Property $property)
101
	{
102 6
		return $property->props[0]->name->toString();
103
	}
104
}
105