Conditions | 1 |
Paths | 1 |
Total Lines | 106 |
Code Lines | 77 |
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 |
||
84 | public function simplifiedTripleProvider() { |
||
85 | return array( |
||
86 | array( |
||
87 | new TripleNode( |
||
88 | new ResourceListNode(array(new StringResourceNode('a'))), |
||
89 | new ResourceListNode(array(new StringResourceNode('foo'))), |
||
90 | new MissingNode() |
||
91 | ), |
||
92 | new TripleNode( |
||
93 | new ResourceListNode(array(new StringResourceNode('a'))), |
||
94 | new ResourceListNode(array(new StringResourceNode('foo'))), |
||
95 | new MissingNode() |
||
96 | ) |
||
97 | ), |
||
98 | array( |
||
99 | new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
||
100 | new TripleNode( |
||
101 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
102 | new ResourceListNode(array(new StringResourceNode('name'))), |
||
103 | new MissingNode() |
||
104 | ) |
||
105 | ), |
||
106 | array( |
||
107 | new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
||
108 | new TripleNode( |
||
109 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
110 | new ResourceListNode(array(new StringResourceNode('definition'))), |
||
111 | new MissingNode() |
||
112 | ) |
||
113 | ), |
||
114 | array( |
||
115 | new UnionNode(array( |
||
116 | new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
||
117 | new TripleNode( |
||
118 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
119 | new ResourceListNode(array(new StringResourceNode('foo'))), |
||
120 | new MissingNode() |
||
121 | ) |
||
122 | )), |
||
123 | new TripleNode( |
||
124 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
125 | new ResourceListNode(array(new StringResourceNode('name'), new StringResourceNode('foo'))), |
||
126 | new MissingNode() |
||
127 | ) |
||
128 | ), |
||
129 | array( |
||
130 | new IntersectionNode(array( |
||
131 | new TripleNode( |
||
132 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
133 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))), |
||
134 | new MissingNode() |
||
135 | ), |
||
136 | new TripleNode( |
||
137 | new MissingNode(), |
||
138 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))), |
||
139 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581097'))))) |
||
140 | ) |
||
141 | )), |
||
142 | new TripleNode( |
||
143 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
144 | new ResourceListNode(array(new StringResourceNode('son'))), |
||
145 | new MissingNode() |
||
146 | ) |
||
147 | ), |
||
148 | array( |
||
149 | new IntersectionNode(array( |
||
150 | new TripleNode( |
||
151 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
152 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))), |
||
153 | new MissingNode() |
||
154 | ), |
||
155 | new TripleNode( |
||
156 | new MissingNode(), |
||
157 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))), |
||
158 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581072'))))) |
||
159 | ) |
||
160 | )), |
||
161 | new TripleNode( |
||
162 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
163 | new ResourceListNode(array(new StringResourceNode('daughter'))), |
||
164 | new MissingNode() |
||
165 | ) |
||
166 | ), |
||
167 | array( |
||
168 | new ResourceListNode(array( |
||
169 | new JsonLdResourceNode( |
||
170 | '63', |
||
171 | (object) array( |
||
172 | '@context' => 'http://schema.org', |
||
173 | '@type' => 'Duration', |
||
174 | 'name' => '63', |
||
175 | 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array( |
||
176 | '@type' => 'Duration', |
||
177 | '@value' => 'P63Y0M1D' |
||
178 | ) |
||
179 | ) |
||
180 | ) |
||
181 | )), |
||
182 | new TripleNode( |
||
183 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
184 | new ResourceListNode(array(new StringResourceNode('age'))), |
||
185 | new MissingNode() |
||
186 | ) |
||
187 | ), |
||
188 | ); |
||
189 | } |
||
190 | } |
||
191 |
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.