Completed
Push — master ( 5294dc...c896df )
by T
04:42
created

Signature::requiredParametersChanges()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 0
cts 0
cp 0
rs 6.9811
cc 7
eloc 11
nc 14
nop 2
crap 56
1
<?php
2
3
namespace PHPSemVerChecker\Comparator;
4
5
class Signature {
6
	/**
7
	 * @param array $parametersA
8
	 * @param array $parametersB
9
	 * @return array
10
	 */
11 15
	public static function analyze(array $parametersA, array $parametersB)
12
	{
13 15
		$changes = [
14 15
			'parameter_added' => false,
15
			'parameter_removed' => false,
16 12
			'parameter_renamed' => false,
17 8
			'parameter_typing_added' => false,
18
			'parameter_typing_removed' => false,
19 4
			'parameter_default_added' => false,
20
			'parameter_default_removed' => false,
21 7
			'parameter_default_value_changed' => false,
22
		];
23 7
		$lengthA = count($parametersA);
24
		$lengthB = count($parametersB);
25
26
		// TODO([email protected]): This is only true if newer params do not have defaults
27 7
		if ($lengthA < $lengthB) {
28 7
			$changes['parameter_added'] = true;
29
		} elseif ($lengthA > $lengthB) {
30
			$changes['parameter_removed'] = true;
31
		}
32
33
		$iterations = min($lengthA, $lengthB);
34
		for ($i = 0; $i < $iterations; ++$i) {
35
			// Name checking
36 7
			if ($parametersA[$i]->name !== $parametersB[$i]->name) {
37
				$changes['parameter_renamed'] = true;
38 7
			}
39
40
			// Type checking
41
			if (Type::get($parametersA[$i]->type) !== Type::get($parametersB[$i]->type)) {
42 7
//				if ($paramsA[$i]->default !== null && $paramsB[$i]->default !== null) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43 7
//					$changes['parameter_default_value_changed'] = true;
44 4
				if ($parametersA[$i]->type !== null) {
45 4
					$changes['parameter_typing_removed'] = true;
46
				}
47
				if ($parametersB[$i]->type !== null) {
48 3
					$changes['parameter_typing_added'] = true;
49
				}
50
			}
51
52
			// Default checking
53
			if ($parametersA[$i]->default === null && $parametersB[$i]->default === null) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
54
				// Do nothing
55
			} elseif ($parametersA[$i]->default !== null && $parametersB[$i]->default === null) {
56
				$changes['parameter_default_removed'] = true;
57
			} elseif ($parametersA[$i]->default === null && $parametersB[$i]->default !== null) {
58
				$changes['parameter_default_added'] = true;
59
			// TODO([email protected]): Not all nodes have a value property
60
			} elseif ( ! Node::isEqual($parametersA[$i]->default, $parametersB[$i]->default)) {
61
				$changes['parameter_default_value_changed'] = true;
62
			}
63
		}
64
65
		return $changes;
66
	}
67
68
	/**
69
	 * @param array $paramsA
70
	 * @param array $paramsB
71
	 * @return string|bool
72
	 */
73
	public static function requiredParametersChanges(array $paramsA, array $paramsB)
74
	{
75
		$iterations = min(count($paramsA), count($paramsB));
76
		// Verify that all params default value match (either they're both null => parameter is required, or both
77
		// set to the same required value)
78
		for ($i = 0; $i < $iterations; ++$i) {
79
			if ($paramsA[$i]->default !== $paramsB[$i]->default) {
80
				return $paramsB[$i]->default ? 'removed' : 'added';
81
			}
82
		}
83
84
		// Only one of these will return its remaining values, the other returning an empty array
85
		$toCheck = array_slice($paramsA, $iterations) + array_slice($paramsB, $iterations);
86
		$operation = count($paramsA) < count($paramsB) ? 'added' : 'removed';
87
		// If any additional argument does not have a default value, the signature has changed
88
		foreach ($toCheck as $param) {
89
			if ($param->default === null) {
90
				return $operation;
91
			}
92
		}
93
		return false;
94
	}
95
96
	/**
97
	 * @param array $paramsA
98
	 * @param array $paramsB
99
	 * @return bool
100
	 */
101
	public static function isSameTypehints(array $paramsA, array $paramsB)
102
	{
103
		$iterations = min(count($paramsA), count($paramsB));
104 View Code Duplication
		for ($i = 0; $i < $iterations; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
			// TODO: Allow for contravariance <[email protected]>
106
			if ( ! Type::isSame($paramsA[$i]->type, $paramsB[$i]->type)) {
107
				return false;
108
			}
109
		}
110
		// Only one of these will return its remaining values, the other returning an empty array
111
		$toCheck = array_slice($paramsA, $iterations) + array_slice($paramsB, $iterations);
112
		// If any additional argument does not have a default value, the signature has changed
113
		foreach ($toCheck as $param) {
114
			if ($param->default === null) {
115
				return false;
116
			}
117
		}
118
		return true;
119
	}
120
121
	/**
122
	 * @param array $paramsA
123
	 * @param array $paramsB
124
	 * @return bool
125
	 */
126
	public static function isSameVariables(array $paramsA, array $paramsB)
127
	{
128
		if (count($paramsA) !== count($paramsB)) {
129
			return false;
130
		}
131
132
		$iterations = min(count($paramsA), count($paramsB));
133 View Code Duplication
		for ($i = 0; $i < $iterations; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
			if ($paramsA[$i]->name != $paramsB[$i]->name) {
135
				return false;
136
			}
137
		}
138
		return true;
139
	}
140
}
141