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