Conditions | 11 |
Paths | 427 |
Total Lines | 86 |
Code Lines | 55 |
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 |
||
36 | public function testCache($class, $params=array()) |
||
37 | { |
||
38 | // set us up as provider for Api\Cache class |
||
39 | $GLOBALS['egw_info']['server']['install_id'] = md5(microtime(true).__FILE__); |
||
40 | unset($GLOBALS['egw_info']['server']['cache_provider_instance']); |
||
41 | unset($GLOBALS['egw_info']['server']['cache_provider_tree']); |
||
42 | Api\Cache::$default_provider = $class; |
||
|
|||
43 | |||
44 | try { |
||
45 | $provider = new $class($params); |
||
46 | $refclass = new ReflectionClass($class); |
||
47 | $methods = array(); |
||
48 | foreach(array('get','set','add','mget','delete') as $name) |
||
49 | { |
||
50 | if ($name != 'mget' || is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
51 | { |
||
52 | $methods[$name] = $refclass->getMethod($name); |
||
53 | $methods[$name]->setAccessible(true); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | foreach(array( |
||
58 | Api\Cache::TREE => 'tree', |
||
59 | Api\Cache::INSTANCE => 'instance', |
||
60 | ) as $level => $label) |
||
61 | { |
||
62 | $locations = array(); |
||
63 | foreach(array('string',123,true,false,null,array(),array(1,2,3)) as $data) |
||
64 | { |
||
65 | $location = md5(microtime(true).$label.serialize($data)); |
||
66 | $this->assertNull($methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
67 | "$class: $label: get_before_set"); |
||
68 | |||
69 | $this->assertTrue($methods['set']->invokeArgs($provider, array(array($level,__CLASS__,$location), $data, 10)), |
||
70 | "$class: $label: set"); |
||
71 | |||
72 | $this->assertEquals($data, $methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
73 | "$class: $label: get_after_set"); |
||
74 | |||
75 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
76 | { |
||
77 | $this->assertEquals(array($location => $data), |
||
78 | $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,array($location)))), |
||
79 | "$class: $label: mget_after_set"); |
||
80 | } |
||
81 | $this->assertNotTrue($methods['add']->invokeArgs($provider, array(array($level,__CLASS__,$location), 'other-data')), |
||
82 | "$class: $label: add_after_set"); |
||
83 | |||
84 | $this->assertTrue($methods['delete']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
85 | "$class: $label: delete"); |
||
86 | |||
87 | $this->assertNull($methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
88 | "$class: $label: get_after_delete"); |
||
89 | |||
90 | // prepare for mget of everything |
||
91 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
92 | { |
||
93 | $locations[$location] = $data; |
||
94 | $mget_after_delete = $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,array($location)))); |
||
95 | $this->assertNotTrue(isset($mget_after_delete[$location]), |
||
96 | "$class: $label: mget_after_delete['$location']"); |
||
97 | } |
||
98 | elseif (!is_null($data)) // emulation can NOT distinquish between null and not set |
||
99 | { |
||
100 | $locations[$location] = $data; |
||
101 | } |
||
102 | $this->assertTrue($methods['add']->invokeArgs($provider, array(array($level,__CLASS__,$location), $data, 10)), |
||
103 | "$class: $label: add_after_delete"); |
||
104 | |||
105 | $this->assertEquals($data, $methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
106 | "$class: $label: get_after_add"); |
||
107 | } |
||
108 | // get all above in one request |
||
109 | $keys = array_keys($locations); |
||
110 | $keys_bogus = array_merge(array('not-set'),array_keys($locations),array('not-set-too')); |
||
111 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
112 | { |
||
113 | $this->assertEquals($locations, $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,$keys))), |
||
114 | "$class: $label: mget_all"); |
||
115 | $this->assertEquals($locations, $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,$keys_bogus))), |
||
116 | "$class: $label: mget_with_bogus_key"); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | catch (\Exception $e) { |
||
121 | $this->markTestSkipped($e->getMessage()); |
||
122 | } |
||
145 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..