Conditions | 1 |
Paths | 1 |
Total Lines | 125 |
Code Lines | 97 |
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 |
||
105 | public function simplifiedTripleProvider() { |
||
106 | $q42 = new Item(new ItemId('Q42')); |
||
107 | $q42->getStatements()->addNewStatement(new PropertyValueSnak(new PropertyId('P214'), new StringValue('113230702'))); |
||
108 | |||
109 | return array( |
||
110 | array( |
||
111 | new IntersectionNode(array(new ResourceListNode())), |
||
112 | new IntersectionNode(array(new ResourceListNode())), |
||
113 | null, |
||
114 | null, |
||
115 | new ResourceListNode(), |
||
116 | new ResourceListNode(), |
||
117 | new ResourceListNode(), |
||
118 | array() |
||
119 | ), |
||
120 | array( |
||
121 | new IntersectionNode(array(new TripleNode( |
||
122 | new MissingNode(), |
||
123 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
124 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
||
125 | ))), |
||
126 | new IntersectionNode(array(new TripleNode( |
||
127 | new MissingNode(), |
||
128 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
129 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
||
130 | ))), |
||
131 | null, |
||
132 | null, |
||
133 | new ResourceListNode(), |
||
134 | new ResourceListNode(), |
||
135 | new ResourceListNode(), |
||
136 | array() |
||
137 | ), |
||
138 | array( |
||
139 | new IntersectionNode(array( |
||
140 | new MissingNode(), |
||
141 | new TripleNode( |
||
142 | new MissingNode(), |
||
143 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
144 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
||
145 | ) |
||
146 | )), |
||
147 | new IntersectionNode(array( |
||
148 | new TripleNode( |
||
149 | new MissingNode(), |
||
150 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
151 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
||
152 | ), |
||
153 | new MissingNode() |
||
154 | )), |
||
155 | new IntersectionNode(array(new MissingNode())), |
||
156 | new MissingNode(), |
||
157 | new ResourceListNode(), |
||
158 | new ResourceListNode(), |
||
159 | new ResourceListNode(), |
||
160 | array() |
||
161 | ), |
||
162 | array( |
||
163 | new IntersectionNode(array( |
||
164 | new ResourceListNode(array( |
||
165 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
166 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q43'))) |
||
167 | )), |
||
168 | new TripleNode( |
||
169 | new MissingNode(), |
||
170 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
171 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
||
172 | ) |
||
173 | )), |
||
174 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
||
175 | new IntersectionNode(array(new ResourceListNode(array( |
||
176 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
177 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q43'))) |
||
178 | )))), |
||
179 | new ResourceListNode(array( |
||
180 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
181 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q43'))) |
||
182 | )), |
||
183 | new ResourceListNode(array( |
||
184 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
185 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q43'))) |
||
186 | )), |
||
187 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
188 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))), |
||
189 | array( |
||
190 | $q42, |
||
191 | new Item(new ItemId('Q43')), |
||
192 | new Property(new PropertyId('P214'), null, 'string') |
||
193 | ) |
||
194 | ), |
||
195 | array( |
||
196 | new IntersectionNode(array( |
||
197 | new ResourceListNode(array( |
||
198 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
199 | new StringResourceNode('foo') |
||
200 | )), |
||
201 | new TripleNode( |
||
202 | new MissingNode(), |
||
203 | new ResourceListNode(array(new StringResourceNode('VIAF'))), |
||
204 | new ResourceListNode(array(new StringResourceNode('113230702'))) |
||
205 | ) |
||
206 | )), |
||
207 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
||
208 | new IntersectionNode(array(new ResourceListNode(array( |
||
209 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
210 | new StringResourceNode('foo') |
||
211 | )))), |
||
212 | new ResourceListNode(array( |
||
213 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
214 | new StringResourceNode('foo') |
||
215 | )), |
||
216 | new ResourceListNode(array( |
||
217 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))), |
||
218 | new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q43'))) |
||
219 | )), |
||
220 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
221 | new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))), |
||
222 | array( |
||
223 | $q42, |
||
224 | new Item(new ItemId('Q43')), |
||
225 | new Property(new PropertyId('P214'), null, 'string') |
||
226 | ) |
||
227 | ), |
||
228 | ); |
||
229 | } |
||
230 | } |
||
231 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.