Conditions | 6 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 29 |
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 |
||
91 | private function fixHtmlLinks(string $content, int $documentId, int $resourceNodeId, int $currentCourseId): string |
||
92 | { |
||
93 | // Define the pattern to find resource links |
||
94 | $pattern = '/\/r\/document\/files\/([a-f0-9\-]+)\/view/'; |
||
95 | |||
96 | return preg_replace_callback($pattern, function ($matches) use ($documentId, $resourceNodeId, $currentCourseId) { |
||
97 | $uuid = $matches[1]; |
||
98 | |||
99 | // Normalize UUID by removing dashes and converting to binary |
||
100 | $cleanUuid = strtoupper(str_replace('-', '', $uuid)); |
||
101 | $uuidBinary = pack('H*', $cleanUuid); |
||
102 | |||
103 | // Check the resource link for the given UUID and fetch title |
||
104 | $query = ' |
||
105 | SELECT rl.resource_node_id, rl.c_id, rn.title |
||
106 | FROM resource_link rl |
||
107 | JOIN resource_node rn ON rl.resource_node_id = rn.id |
||
108 | WHERE rn.uuid = UNHEX(:uuid) |
||
109 | '; |
||
110 | $resourceData = $this->connection->fetchAssociative($query, ['uuid' => $cleanUuid]); |
||
111 | |||
112 | if ($resourceData && $resourceData['c_id'] === $currentCourseId) { |
||
113 | // Log debugging information |
||
114 | error_log("Document ID $documentId, Resource Node $resourceNodeId: Valid resource found for UUID $uuid in the correct course $currentCourseId"); |
||
115 | |||
116 | // Return the corrected link with original UUID format |
||
117 | return "/r/document/files/$uuid/view"; |
||
118 | } elseif ($resourceData) { |
||
119 | // If the course ID doesn't match, find the correct file in the current course |
||
120 | error_log("Document ID $documentId, Resource Node $resourceNodeId: UUID $uuid does not belong to course $currentCourseId. Searching for the correct file..."); |
||
121 | |||
122 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
123 | $courseRepo = $this->container->get(CourseRepository::class); |
||
124 | $title = $resourceData['title']; |
||
125 | |||
126 | // Load the Course object |
||
127 | $course = $courseRepo->find($currentCourseId); |
||
128 | if (!$course) { |
||
129 | error_log("Document ID $documentId, Resource Node $resourceNodeId: Course with ID $currentCourseId not found."); |
||
130 | return $matches[0]; // Return original link if course not found |
||
131 | } |
||
132 | |||
133 | // Search for a document in the current course by title |
||
134 | $correctDocument = $documentRepo->findResourceByTitleInCourse($title, $course); |
||
135 | |||
136 | if ($correctDocument) { |
||
137 | $newUrl = $documentRepo->getResourceFileUrl($correctDocument); |
||
138 | error_log("Document ID $documentId: Correct URL found: $newUrl"); |
||
139 | |||
140 | return $newUrl; |
||
141 | } else { |
||
142 | error_log("Document ID $documentId, Resource Node $resourceNodeId: No document found for title $title in course $currentCourseId."); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // Log missing resource data |
||
147 | error_log("Document ID $documentId, Resource Node $resourceNodeId: Resource link for UUID $uuid not found."); |
||
148 | return $matches[0]; // Return original link if no match found |
||
149 | }, $content); |
||
150 | } |
||
158 |
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.