| Conditions | 1 |
| Paths | 1 |
| Total Lines | 78 |
| Code Lines | 56 |
| 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 |
||
| 110 | public function simplificationProvider() { |
||
| 111 | $list = array(); |
||
| 112 | |||
| 113 | //Value |
||
| 114 | $douglasAdamItem = new Item(); |
||
| 115 | $douglasAdamItem->setId(new ItemId('Q42')); |
||
| 116 | $birthPlaceStatement = new Statement( |
||
| 117 | new PropertyValueSnak(new PropertyId('P214'), new StringValue('113230702')) |
||
| 118 | ); |
||
| 119 | $birthPlaceStatement->setGuid('42'); |
||
| 120 | $douglasAdamItem->getStatements()->addStatement($birthPlaceStatement); |
||
| 121 | $list[] = array( |
||
| 122 | new TripleNode( |
||
| 123 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
||
| 124 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
||
| 125 | new MissingNode() |
||
| 126 | ), |
||
| 127 | new ResourceListNode(array( |
||
| 128 | new ResourceListNode(array( |
||
| 129 | new WikibaseResourceNode( |
||
| 130 | '', |
||
| 131 | new StringValue('113230702'), |
||
| 132 | new ItemId('Q42'), |
||
| 133 | new PropertyId('P214') |
||
| 134 | ) |
||
| 135 | )) |
||
| 136 | )), |
||
| 137 | $douglasAdamItem, |
||
| 138 | new PropertyId('P214') |
||
| 139 | ); |
||
| 140 | |||
| 141 | //SomeValue |
||
| 142 | $douglasAdamItem = new Item(); |
||
| 143 | $douglasAdamItem->setId(new ItemId('Q42')); |
||
| 144 | $birthPlaceStatement = new Statement(new PropertySomeValueSnak(new PropertyId('P19'))); |
||
| 145 | $birthPlaceStatement->setGuid('42'); |
||
| 146 | $douglasAdamItem->getStatements()->addStatement($birthPlaceStatement); |
||
| 147 | $list[] = array( |
||
| 148 | new TripleNode( |
||
| 149 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
||
| 150 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P19'))))), |
||
| 151 | new MissingNode() |
||
| 152 | ), |
||
| 153 | new ResourceListNode(array()), |
||
| 154 | $douglasAdamItem, |
||
| 155 | new PropertyId('P19') |
||
| 156 | ); |
||
| 157 | |||
| 158 | //No result |
||
| 159 | $douglasAdamItem = new Item(); |
||
| 160 | $douglasAdamItem->setId(new ItemId('Q42')); |
||
| 161 | $list[] = array( |
||
| 162 | new TripleNode( |
||
| 163 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
||
| 164 | new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P19'))))), |
||
| 165 | new MissingNode() |
||
| 166 | ), |
||
| 167 | new ResourceListNode(array()), |
||
| 168 | $douglasAdamItem, |
||
| 169 | new PropertyId('P19') |
||
| 170 | ); |
||
| 171 | |||
| 172 | //Parsing |
||
| 173 | $douglasAdamItem = new Item(); |
||
| 174 | $douglasAdamItem->setId(new ItemId('Q42')); |
||
| 175 | $list[] = array( |
||
| 176 | new TripleNode( |
||
| 177 | new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
||
| 178 | new ResourceListNode(array(new StringResourceNode('VIAF'))), |
||
| 179 | new MissingNode() |
||
| 180 | ), |
||
| 181 | new ResourceListNode(array()), |
||
| 182 | $douglasAdamItem, |
||
| 183 | new PropertyId('P214') |
||
| 184 | ); |
||
| 185 | |||
| 186 | return $list; |
||
| 187 | } |
||
| 188 | } |
||
| 189 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.