Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
10 | class TestCase extends \WP_UnitTestCase { |
||
11 | |||
12 | /** |
||
13 | * @param $class |
||
14 | * @param $property |
||
15 | * @return mixed |
||
16 | */ |
||
17 | public function getReflectionPropertyValue( $class, $property ) |
||
18 | { |
||
19 | $reflection = new \ReflectionProperty( $class, $property ); |
||
20 | $reflection->setAccessible( true ); |
||
21 | return $reflection->getValue( $class ); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param $class |
||
26 | * @param $property |
||
27 | * @param $value |
||
28 | */ |
||
29 | public function setReflectionPropertyValue( $class, $property, $value ) |
||
30 | { |
||
31 | $reflection = new \ReflectionProperty( $class, $property ); |
||
32 | $reflection->setAccessible( true ); |
||
33 | return $reflection->setValue( $class, $value ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param $class |
||
38 | * @param $method |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function reflectionMethodInvoke( $class, $method ) |
||
42 | { |
||
43 | $reflection = new \ReflectionMethod( $class, $method ); |
||
44 | $reflection->setAccessible( true ); |
||
45 | if (is_string($class)) { |
||
46 | $class = null; |
||
47 | } |
||
48 | return $reflection->invoke( $class ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $class |
||
53 | * @param $method |
||
54 | * @param $args |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function reflectionMethodInvokeArgs( $class, $method, $args ) |
||
58 | { |
||
59 | $reflection = new \ReflectionMethod( $class, $method ); |
||
60 | $reflection->setAccessible( true ); |
||
61 | if (is_string($class)) { |
||
62 | $class = null; |
||
63 | } |
||
64 | return $reflection->invoke( $class, $args ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | protected function get_sample_response() { |
||
71 | |||
72 | return file_get_contents( __DIR__ . '/geocoder-response.json' ); |
||
73 | |||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return Mock |
||
78 | */ |
||
79 | protected function getMockGeocoder() { |
||
80 | |||
81 | /** |
||
82 | * @var Mock $geocoder |
||
83 | */ |
||
84 | $geocoder = \Mockery::mock('\Clubdeuce\WPLib\Components\GoogleMaps\Geocoder'); |
||
85 | $geocoder->shouldReceive('geocode')->andReturn($this->getMockLocation()); |
||
86 | |||
87 | return $geocoder; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Mock |
||
92 | */ |
||
93 | protected function getMockLocation() { |
||
94 | /** |
||
95 | * @var Mock $location |
||
96 | */ |
||
97 | $location = \Mockery::mock('\Clubdeuce\WPLib\Components\GoogleMape\Location'); |
||
98 | $location->shouldReceive('address')->andReturn('1600 Amphitheatre Parkway, Mountain View, CA 94043, USA'); |
||
99 | $location->shouldReceive('formatted_address')->andReturn('1600 Amphitheatre Parkway, Mountain View, CA 94043, USA'); |
||
100 | $location->shouldReceive('state')->andReturn('CA'); |
||
101 | $location->shouldReceive('zip_code')->andReturn('94043'); |
||
102 | $location->shouldReceive('latitude')->andReturn(37.4224764); |
||
103 | $location->shouldReceive('longitude')->andReturn(-122.0842499); |
||
104 | $location->shouldReceive('place_id')->andReturn('ChIJ2eUgeAK6j4ARbn5u_wAGqWA'); |
||
105 | $location->shouldReceive('type')->andReturn('street_address'); |
||
106 | $location->shouldReceive('viewport')->andReturn(array( |
||
107 | 'northeast' => array( |
||
108 | 'lat' => 37.4238253802915, |
||
109 | 'lng' => -122.0829009197085 |
||
110 | ), |
||
111 | 'sourhwest' => array( |
||
112 | 'lat' => 37.4211274197085, |
||
113 | 'lng' => -122.0855988802915 |
||
114 | ) |
||
115 | )); |
||
116 | |||
117 | return $location; |
||
118 | } |
||
119 | } |