| Conditions | 13 |
| Paths | 80 |
| Total Lines | 59 |
| Code Lines | 39 |
| 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 |
||
| 37 | private static function relationAddRelationshipTo($link, $plurality, $extra=[]){ |
||
| 38 | $options = static::relationOptions(); |
||
| 39 | |||
| 40 | preg_match('((?<FOREIGN_CLASS>\w+)(\.(?<FOREIGN_KEY>\w+))?(:(?<LOCAL_KEY>\w+))?)', $link, $parts); |
||
| 41 | |||
| 42 | $foreign_class = isset($parts['FOREIGN_CLASS']) ? $parts['FOREIGN_CLASS'] : false; |
||
| 43 | $foreign_key = isset($parts['FOREIGN_KEY']) ? $parts['FOREIGN_KEY'] : false; |
||
| 44 | $local_key = isset($parts['LOCAL_KEY']) ? $parts['LOCAL_KEY'] : false; |
||
| 45 | |||
| 46 | if ( ! $foreign_class ) |
||
| 47 | throw new Exception("[Core.Relation] Class ".get_called_class()." must define a foreign Model when assigning a relation.", 1); |
||
| 48 | |||
| 49 | if ( ! is_subclass_of($foreign_class,"Model") ) |
||
| 50 | throw new Exception("[Core.Relation] Class ".get_called_class()." want to relate to $foreign_class but it's not a Model.", 1); |
||
| 51 | |||
| 52 | if ( ! $foreign_key ) { |
||
| 53 | // Retrieve primary key from foreign class |
||
| 54 | $foreign_key = $foreign_class::persistenceOptions("key"); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( ! $local_key ) { |
||
| 58 | // Retrieve local primary key |
||
| 59 | $local_key = static::persistenceOptions("key"); |
||
| 60 | } |
||
| 61 | |||
| 62 | $single = $plurality == 'single'; |
||
| 63 | |||
| 64 | $method = preg_replace_callback('([A-Z])', function($m){ |
||
| 65 | return "_" . strtolower($m[0]); |
||
| 66 | }, lcfirst($foreign_class) . ($single ? '' : 's')); |
||
| 67 | |||
| 68 | $hh = [$foreign_class,$foreign_key,$local_key]; |
||
| 69 | sort($hh); |
||
| 70 | $options->links[md5(serialize($hh))] = $rel = (object)[ |
||
| 71 | 'foreign_class' => $foreign_class, |
||
| 72 | 'foreign_key' => $foreign_key, |
||
| 73 | 'local_key' => $local_key, |
||
| 74 | 'single' => $single, |
||
| 75 | 'method' => $method, |
||
| 76 | 'extra' => (object) $extra, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | if (empty($options->relations)) $options->relations = (object)[]; |
||
| 80 | $options->relations->$method = $getset = (object)[ |
||
| 81 | 'get' => function($self) use ($foreign_class, $rel) { |
||
| 82 | $val = $self->{$rel->local_key}; |
||
| 83 | $val = is_numeric($val) ? $val : "'" . addslashes($val) . "'"; |
||
| 84 | $data = $foreign_class::where("{$rel->foreign_key} = {$val}"); |
||
| 85 | return $rel->single ? current($data) : $data; |
||
| 86 | }, |
||
| 87 | 'set' => function($value, $self) use ($foreign_class, $rel) { |
||
| 88 | if (!is_a($value, $foreign_class)) |
||
| 89 | throw new Exception("[Core.Relation] Relationship for {$rel->method} must be of class $foreign_class.", 1); |
||
| 90 | $self->local_key = $value->foreign_key; |
||
| 91 | return $value; |
||
| 92 | }, |
||
| 93 | ]; |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 123 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.