| Conditions | 12 |
| Paths | 8 |
| Total Lines | 98 |
| Code Lines | 66 |
| 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 |
||
| 24 | public function up(Schema $schema) |
||
| 25 | { |
||
| 26 | $this->addSql("ALTER TABLE track_e_hotspot ADD c_id INT NULL"); |
||
| 27 | $this->addSql("ALTER TABLE track_e_hotspot MODIFY COLUMN hotspot_coordinate LONGTEXT NOT NULL"); |
||
| 28 | $this->addSql("UPDATE track_e_hotspot SET c_id = (SELECT id FROM course WHERE code = hotspot_course_code)"); |
||
| 29 | |||
| 30 | $answers = $this->connection->fetchAll(" |
||
| 31 | SELECT a.iid, a.c_id, a.question_id, a.hotspot_coordinates, a.hotspot_type, q.picture, c.directory |
||
| 32 | FROM c_quiz_answer a |
||
| 33 | INNER JOIN c_quiz_question q |
||
| 34 | ON (a.question_id = q.id AND a.c_id = q.c_id) |
||
| 35 | INNER JOIN course c |
||
| 36 | ON (a.c_id = c.id AND q.c_id = c.id) |
||
| 37 | WHERE a.hotspot_type IN ('square', 'circle', 'poly', 'delineation', 'oar') |
||
| 38 | "); |
||
| 39 | |||
| 40 | foreach ($answers as $answer) { |
||
| 41 | // Recover the real image size to recalculate coordinates |
||
| 42 | $imagePath = __DIR__ . "/../../../../courses/{$answer['directory']}/document/images/{$answer['picture']}"; |
||
| 43 | if (!file_exists($imagePath)) { |
||
| 44 | error_log("Migration: Image does not exists: $imagePath"); |
||
| 45 | $imagePath = realpath($imagePath); |
||
| 46 | error_log("Hotspot realpath: $imagePath"); |
||
| 47 | error_log("api_get_path: SYS_PATH: ".api_get_path(SYS_PATH)); |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | $imageSize = getimagesize($imagePath); |
||
| 51 | $widthRatio = $imageSize[0] / 360; |
||
| 52 | $heightRatio = $imageSize[1] / 360; |
||
| 53 | $oldCoords = $answer['hotspot_coordinates']; |
||
| 54 | $oldPairedString = explode('|', $oldCoords); |
||
| 55 | $newPairedString = []; |
||
| 56 | |||
| 57 | switch ($answer['hotspot_type']) { |
||
| 58 | case 'square': |
||
| 59 | $oldCenter = explode(';', $oldPairedString[0]); |
||
| 60 | $oldCenterX = intval($oldCenter[0]); |
||
| 61 | $oldCenterY = intval($oldCenter[1]); |
||
| 62 | $oldWidth = intval($oldPairedString[1]); |
||
| 63 | $oldHeight = intval($oldPairedString[2]); |
||
| 64 | |||
| 65 | $newX = floor(($oldCenterX - $oldWidth / 2) * $widthRatio) + ceil($widthRatio); |
||
| 66 | $newY = floor(($oldCenterY - $oldHeight / 2) * $heightRatio) + ceil($heightRatio); |
||
| 67 | $newWidth = ceil($oldWidth * $widthRatio) + ceil(1 * $heightRatio); |
||
| 68 | $newHeight = ceil($oldHeight * $heightRatio) + floor(1 * $heightRatio); |
||
| 69 | |||
| 70 | $newPairedString[] = implode(';', [$newX, $newY]); |
||
| 71 | $newPairedString[] = $newWidth; |
||
| 72 | $newPairedString[] = $newHeight; |
||
| 73 | break; |
||
| 74 | case 'circle': |
||
| 75 | $oldCenter = explode(';', $oldPairedString[0]); |
||
| 76 | $oldCenterX = intval($oldCenter[0]); |
||
| 77 | $oldCenterY = intval($oldCenter[1]); |
||
| 78 | $oldRadiusX = intval($oldPairedString[1]) / 2; |
||
| 79 | $oldRadiusY = intval($oldPairedString[2]) / 2; |
||
| 80 | |||
| 81 | $newCenterX = floor($oldCenterX * $widthRatio) + ceil($widthRatio); |
||
| 82 | $newCenterY = floor($oldCenterY * $heightRatio) + ceil($heightRatio); |
||
| 83 | $newRadiusX = floor($oldRadiusX * $widthRatio); |
||
| 84 | $newRadiusY = floor($oldRadiusY * $heightRatio); |
||
| 85 | |||
| 86 | $newPairedString[] = implode(';', [$newCenterX, $newCenterY]); |
||
| 87 | $newPairedString[] = $newRadiusX; |
||
| 88 | $newPairedString[] = $newRadiusY; |
||
| 89 | break; |
||
| 90 | case 'poly': |
||
| 91 | //no break; |
||
| 92 | case 'delineation': |
||
| 93 | //no break |
||
| 94 | case 'oar': |
||
| 95 | $paired = []; |
||
| 96 | foreach ($oldPairedString as $pairString) { |
||
| 97 | $pair = explode(';', $pairString); |
||
| 98 | $x = isset($pair[0]) ? intval($pair[0]) : 0; |
||
| 99 | $y = isset($pair[1]) ? intval($pair[1]) : 0; |
||
| 100 | |||
| 101 | $paired[] = [$x, $y]; |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach ($paired as $pair) { |
||
| 105 | $x = floor($pair[0] * $widthRatio) + ceil($widthRatio); |
||
| 106 | $y = ceil($pair[1] * $heightRatio); |
||
| 107 | |||
| 108 | $newPairedString[] = implode(';', [$x, $y]); |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | |||
| 113 | $stmt = $this->connection->prepare(" |
||
| 114 | UPDATE c_quiz_answer |
||
| 115 | SET hotspot_coordinates = :coordinates |
||
| 116 | WHERE iid = :iid AND c_id = :cid |
||
| 117 | "); |
||
| 118 | $stmt->bindValue('coordinates', implode('|', $newPairedString), Type::TEXT); |
||
| 119 | $stmt->bindValue('iid', $answer['iid'], Type::INTEGER); |
||
| 120 | $stmt->bindValue('cid', $answer['c_id'], Type::INTEGER); |
||
| 121 | $stmt->execute(); |
||
| 122 | } |
||
| 132 |