| Conditions | 7 |
| Paths | 6 |
| Total Lines | 52 |
| Code Lines | 25 |
| 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 |
||
| 68 | public function getCanvasContext($canvasUrl) |
||
| 69 | { |
||
| 70 | /* |
||
| 71 | * TODO: accept calendar2?contexts links too (they would be an intuitively |
||
| 72 | * obvious link to use, after all) |
||
| 73 | */ |
||
| 74 | /* |
||
| 75 | * FIXME: users aren't working |
||
| 76 | */ |
||
| 77 | /* |
||
| 78 | * TODO: it would probably be better to look up users by email address than |
||
| 79 | * URL |
||
| 80 | */ |
||
| 81 | /* get the context (user, course or group) for the canvas URL */ |
||
| 82 | $canvasContext = array(); |
||
| 83 | if (preg_match( |
||
| 84 | '%(https?://)?(' . |
||
| 85 | parse_url($this->config('TOOL_CANVAS_API')['url'], PHP_URL_HOST) . |
||
| 86 | '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', |
||
| 87 | $canvasUrl, |
||
| 88 | $matches |
||
| 89 | )) { |
||
| 90 | $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953 |
||
| 91 | |||
| 92 | // course or account groups |
||
| 93 | if (isset($matches[9]) || isset($matches[11])) { |
||
| 94 | $canvasContext['context'] = 'group'; // used to for context_code in events |
||
| 95 | $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]); |
||
| 96 | |||
| 97 | /* used once to look up the object to be sure it really exists */ |
||
| 98 | $canvasContext['verification_url'] = "groups/{$canvasContext['id']}"; |
||
| 99 | |||
| 100 | // courses |
||
| 101 | } elseif (isset($matches[7])) { |
||
| 102 | $canvasContext['context'] = 'course'; |
||
| 103 | $canvasContext['id'] = $matches[7]; |
||
| 104 | $canvasContext['verification_url'] = "courses/{$canvasContext['id']}"; |
||
| 105 | |||
| 106 | // users |
||
| 107 | } elseif (isset($matches[5])) { |
||
| 108 | $canvasContext['context'] = 'user'; |
||
| 109 | $canvasContext['id'] = $matches[5]; |
||
| 110 | $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile"; |
||
| 111 | |||
| 112 | // we're somewhere where we don't know where we are |
||
| 113 | } else { |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | return $canvasContext; |
||
| 117 | } |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 154 |