| Conditions | 11 |
| Paths | 2 |
| Total Lines | 49 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 112 | private function replaceURLParametersInContent(string $content): string |
||
| 113 | { |
||
| 114 | // Pattern to find and replace cidReq, id_session, and gidReq |
||
| 115 | $pattern = '/((https?:\/\/[^\/\s]*|)\/[^?\s]+?)\?(.*?)(cidReq=([a-zA-Z0-9_]+))((?:&|&)id_session=([0-9]+))?((?:&|&)gidReq=([0-9]+))?(.*)/i'; |
||
| 116 | |||
| 117 | $newContent = @preg_replace_callback( |
||
| 118 | $pattern, |
||
| 119 | function ($matches) { |
||
| 120 | $code = $matches[5]; |
||
| 121 | |||
| 122 | $courseId = null; |
||
| 123 | $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1'; |
||
| 124 | $stmt = $this->connection->executeQuery($sql, ['code' => $code]); |
||
| 125 | $course = $stmt->fetch(); |
||
| 126 | |||
| 127 | if ($course) { |
||
| 128 | $courseId = $course['id']; |
||
| 129 | } |
||
| 130 | |||
| 131 | if (null === $courseId) { |
||
| 132 | return $matches[0]; // If the courseId is not found, return the original URL. |
||
| 133 | } |
||
| 134 | |||
| 135 | // Ensure sid and gid are always populated |
||
| 136 | $sessionId = isset($matches[7]) && !empty($matches[7]) ? $matches[7] : '0'; |
||
| 137 | $groupId = isset($matches[9]) && !empty($matches[9]) ? $matches[9] : '0'; |
||
| 138 | $remainingParams = isset($matches[10]) ? $matches[10] : ''; |
||
| 139 | |||
| 140 | // Prepare new URL with updated parameters |
||
| 141 | $newParams = "cid=$courseId&sid=$sessionId&gid=$groupId"; |
||
| 142 | $beforeCidReqParams = isset($matches[3]) ? $matches[3] : ''; |
||
| 143 | |||
| 144 | // Ensure other parameters are maintained |
||
| 145 | if (!empty($remainingParams)) { |
||
| 146 | $newParams .= '&' . ltrim($remainingParams, '&'); |
||
| 147 | } |
||
| 148 | |||
| 149 | $finalUrl = $matches[1] . '?' . $beforeCidReqParams . $newParams; |
||
| 150 | |||
| 151 | return str_replace('&', '&', $finalUrl); // Replace any remaining & with & |
||
| 152 | }, |
||
| 153 | $content |
||
| 154 | ); |
||
| 155 | |||
| 156 | if (PREG_NO_ERROR !== preg_last_error()) { |
||
| 157 | error_log('Error encountered in preg_replace_callback: ' . preg_last_error()); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $newContent; |
||
| 161 | } |
||
| 163 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.