| Conditions | 10 |
| Paths | 24 |
| Total Lines | 41 |
| 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 |
||
| 31 | public function checkCode($obj, $createCode = false, $field = "Code") |
||
|
|
|||
| 32 | { |
||
| 33 | //exception dealing with Strings |
||
| 34 | $isObject = true; |
||
| 35 | if (! is_object($obj)) { |
||
| 36 | $str = $obj; |
||
| 37 | $obj = new DataObject(); |
||
| 38 | $obj->$field = strval($str); |
||
| 39 | $isObject = false; |
||
| 40 | } |
||
| 41 | if ($createCode) { |
||
| 42 | if (!$obj->$field || strlen($obj->$field) != $this->Config()->get("length")) { |
||
| 43 | $obj->$field = $this->CreateCode(); |
||
| 44 | } |
||
| 45 | } else { |
||
| 46 | $obj->$field = trim($obj->$field); |
||
| 47 | foreach ($this->replacements as $regex => $replace) { |
||
| 48 | $obj->$field = preg_replace($regex, $replace, $obj->$field); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | if (!$obj->$field) { |
||
| 52 | $obj->$field = strtoupper($field)."-NOT-SET"; |
||
| 53 | } |
||
| 54 | //make upper-case |
||
| 55 | $obj->$field = trim(strtoupper($obj->$field)); |
||
| 56 | //check for other ones. |
||
| 57 | $count = 0; |
||
| 58 | $code = $obj->$field; |
||
| 59 | while ( |
||
| 60 | $isObject && |
||
| 61 | $obj::get() |
||
| 62 | ->filter([$field => $obj->$field]) |
||
| 63 | ->exclude(["ID" => intval($obj->ID) - 0])->Count() > 0 && |
||
| 64 | $count < 1000 |
||
| 65 | ) { |
||
| 66 | $obj->$field = $this->CreateCode(); |
||
| 67 | $count++; |
||
| 68 | } |
||
| 69 | |||
| 70 | return $obj->$field; |
||
| 71 | } |
||
| 72 | |||
| 84 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.