| Conditions | 11 |
| Paths | 38 |
| Total Lines | 46 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 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 |
||
| 23 | public static function loadModel( |
||
| 24 | $modelName, |
||
| 25 | $id, |
||
| 26 | $createIfEmptyId = false, |
||
| 27 | $useCache = true, |
||
| 28 | $cacheLifetime = 3600, |
||
| 29 | $throwException = true |
||
| 30 | ) { |
||
| 31 | $model = null; |
||
| 32 | if (empty($id)) { |
||
| 33 | if ($createIfEmptyId === true) { |
||
| 34 | $model = new $modelName; |
||
| 35 | } else { |
||
| 36 | if ($throwException) { |
||
| 37 | throw new NotFoundHttpException; |
||
| 38 | } else { |
||
| 39 | return null; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | if ($useCache === true && $model===null) { |
||
| 44 | $model = Yii::$app->cache->get($modelName::className() . ":" . $id); |
||
| 45 | } |
||
| 46 | if (!is_object($model)) { |
||
| 47 | $model = $modelName::findOne($id); |
||
| 48 | |||
| 49 | if (is_object($model) && $useCache === true) { |
||
| 50 | Yii::$app->cache->set( |
||
| 51 | $modelName::className() . ":" . $id, |
||
| 52 | $model, |
||
| 53 | $cacheLifetime, |
||
| 54 | new TagDependency([ |
||
| 55 | 'tags' => ActiveRecordHelper::getCommonTag($modelName::className()), |
||
| 56 | ]) |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | if (!is_object($model)) { |
||
| 61 | if ($throwException) { |
||
| 62 | throw new NotFoundHttpException; |
||
| 63 | } else { |
||
| 64 | return null; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | return $model; |
||
| 68 | } |
||
| 69 | } |
||
| 70 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.