| Conditions | 11 |
| Paths | 31 |
| Total Lines | 46 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 135 | protected function getRelationshipAsPropertyName($value, $className, ReflectionClass $reflection) |
||
| 136 | { |
||
| 137 | $methods = []; |
||
| 138 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 139 | if (\ltrim($method->class, '\\') === \ltrim($className, '\\')) { |
||
| 140 | $name = $method->name; |
||
| 141 | $reflectionMethod = $reflection->getMethod($name); |
||
| 142 | |||
| 143 | // Eloquent relations do not include parameters, so we'll be filtering based on this criteria. |
||
| 144 | if (0 == $reflectionMethod->getNumberOfParameters()) { |
||
| 145 | try { |
||
| 146 | |||
| 147 | if (false === in_array($value, $this->forbiddenFunction, true)) { |
||
| 148 | $returned = $reflectionMethod->invoke($value); |
||
| 149 | //All operations (eg: boolean operations) are now filtered out. |
||
| 150 | if (\is_object($returned)) { |
||
| 151 | |||
| 152 | // Only keep those methods as properties if these are returning Eloquent relations. |
||
| 153 | // But do not run the operation as it is an expensive operation. |
||
| 154 | if (false !== \strpos(\get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { |
||
| 155 | $items = []; |
||
| 156 | foreach ($returned->getResults() as $model) { |
||
| 157 | if (\is_object($model)) { |
||
| 158 | /** @var Model $model */ |
||
| 159 | $stdClass = (object) $model->getAttributes(); |
||
| 160 | $data = $this->serializeData($stdClass); |
||
| 161 | $data[self::CLASS_IDENTIFIER_KEY] = \get_class($model); |
||
| 162 | |||
| 163 | $items[] = $data; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | if (!empty($items)) { |
||
| 167 | $methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | } catch (ErrorException $e) { |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $methods; |
||
| 180 | } |
||
| 181 | } |
||
| 182 |
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.