Conditions | 5 |
Paths | 4 |
Total Lines | 58 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function assertSameArray(array $expected, $actual, $prefix = null) |
||
20 | { |
||
21 | //assert that the actual value is an array |
||
22 | $this->assertInternalType('array', $actual, '$actual was not an array'); |
||
23 | |||
24 | $expectedKeys = array_keys($expected); |
||
25 | $actualKeys = array_keys($actual); |
||
26 | |||
27 | //find any keys in the expected array that are not present in the actual array |
||
28 | $missingExpectedKeys = array_diff($expectedKeys, $actualKeys); |
||
29 | $this->assertCount( |
||
30 | 0, |
||
31 | $missingExpectedKeys, |
||
32 | sprintf( |
||
33 | '$actual array is missing %d keys: %s', |
||
34 | count($missingExpectedKeys), |
||
35 | implode(', ', $missingExpectedKeys) |
||
36 | ) |
||
37 | ); |
||
38 | |||
39 | //find any keys in the actual array that are not expected in the expected array |
||
40 | $unexpectedKeys = array_diff($actualKeys, $expectedKeys); |
||
41 | $this->assertCount( |
||
42 | 0, |
||
43 | $unexpectedKeys, |
||
44 | sprintf( |
||
45 | '$actual array contains %d unexpected keys: %s', |
||
46 | count($unexpectedKeys), |
||
47 | implode(', ', $unexpectedKeys) |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | //Assert all values are the same value and type. |
||
52 | //Recursively call assertSameArray on array values |
||
53 | foreach ($expected as $key => $value) { |
||
54 | //If a sub array is indexed numerically we just want to ensure all the values are present and not their keys. |
||
55 | if (is_array($value) && self::isNumeric($value)) { |
||
56 | $difference = array_diff($value, $actual[$key]); |
||
57 | $this->assertSame(count($difference), 0); |
||
58 | continue; |
||
59 | } |
||
60 | |||
61 | if (is_array($value)) { |
||
62 | $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}."); |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $this->assertSame( |
||
67 | $value, |
||
68 | $actual[$key], |
||
69 | sprintf( |
||
70 | "{$prefix}{$key} value is not correct expected %s\nfound %s", |
||
71 | var_export($value, 1), |
||
72 | var_export($actual[$key], 1) |
||
73 | ) |
||
74 | ); |
||
75 | } |
||
76 | } |
||
77 | |||
136 |