| Conditions | 11 |
| Paths | 7 |
| Total Lines | 98 |
| Code Lines | 61 |
| 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 |
||
| 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 = api_get_path(SYS_PATH) . "courses/{$answer['directory']}/document/images/{$answer['picture']}"; |
||
| 43 | $imageSize = getimagesize($imagePath); |
||
| 44 | $widthRatio = $imageSize[0] / 360; |
||
| 45 | $heightRatio = $imageSize[1] / 360; |
||
| 46 | $oldCoords = $answer['hotspot_coordinates']; |
||
| 47 | $oldPairedString = explode('|', $oldCoords); |
||
| 48 | $newPairedString = []; |
||
| 49 | |||
| 50 | switch ($answer['hotspot_type']) { |
||
| 51 | case 'square': |
||
| 52 | $oldCenter = explode(';', $oldPairedString[0]); |
||
| 53 | |||
| 54 | $oldCenterX = intval($oldCenter[0]); |
||
| 55 | $oldCenterY = intval($oldCenter[1]); |
||
| 56 | $oldWidth = intval($oldPairedString[1]); |
||
| 57 | $oldHeight = intval($oldPairedString[2]); |
||
| 58 | |||
| 59 | $newX = floor(($oldCenterX - $oldWidth / 2) * $widthRatio) + ceil($widthRatio); |
||
| 60 | $newY = floor(($oldCenterY - $oldHeight / 2) * $heightRatio) + ceil($heightRatio); |
||
| 61 | $newWidth = ceil($oldWidth * $widthRatio) + ceil(1 * $heightRatio); |
||
| 62 | $newHeight = ceil($oldHeight * $heightRatio) + floor(1 * $heightRatio); |
||
| 63 | |||
| 64 | $newPairedString[] = implode(';', [$newX, $newY]); |
||
| 65 | $newPairedString[] = $newWidth; |
||
| 66 | $newPairedString[] = $newHeight; |
||
| 67 | break; |
||
| 68 | |||
| 69 | case 'circle': |
||
| 70 | $oldCenter = explode(';', $oldPairedString[0]); |
||
| 71 | |||
| 72 | $oldCenterX = intval($oldCenter[0]); |
||
| 73 | $oldCenterY = intval($oldCenter[1]); |
||
| 74 | $oldRadiusX = intval($oldPairedString[1]) / 2; |
||
| 75 | $oldRadiusY = intval($oldPairedString[2]) / 2; |
||
| 76 | |||
| 77 | $newCenterX = floor($oldCenterX * $widthRatio) + ceil($widthRatio); |
||
| 78 | $newCenterY = floor($oldCenterY * $heightRatio) + ceil($heightRatio); |
||
| 79 | $newRadiusX = floor($oldRadiusX * $widthRatio); |
||
| 80 | $newRadiusY = floor($oldRadiusY * $heightRatio); |
||
| 81 | |||
| 82 | $newPairedString[] = implode(';', [$newCenterX, $newCenterY]); |
||
| 83 | $newPairedString[] = $newRadiusX; |
||
| 84 | $newPairedString[] = $newRadiusY; |
||
| 85 | break; |
||
| 86 | |||
| 87 | case 'poly': |
||
| 88 | //no break; |
||
| 89 | case 'delineation': |
||
| 90 | //no break |
||
| 91 | case 'oar': |
||
| 92 | $paired = []; |
||
| 93 | |||
| 94 | foreach ($oldPairedString as $pairString) { |
||
| 95 | $pair = explode(';', $pairString); |
||
| 96 | $x = isset($pair[0]) ? intval($pair[0]) : 0; |
||
| 97 | $y = isset($pair[1]) ? intval($pair[1]) : 0; |
||
| 98 | |||
| 99 | $paired[] = [$x, $y]; |
||
| 100 | } |
||
| 101 | |||
| 102 | foreach ($paired as $pair) { |
||
| 103 | $x = floor($pair[0] * $widthRatio) + ceil($widthRatio); |
||
| 104 | $y = ceil($pair[1] * $heightRatio); |
||
| 105 | |||
| 106 | $newPairedString[] = implode(';', [$x, $y]); |
||
| 107 | } |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | |||
| 111 | $stmt = $this->connection->prepare(" |
||
| 112 | UPDATE c_quiz_answer |
||
| 113 | SET hotspot_coordinates = :coordinates |
||
| 114 | WHERE iid = :iid AND c_id = :cid |
||
| 115 | "); |
||
| 116 | $stmt->bindValue('coordinates', implode('|', $newPairedString), Type::TEXT); |
||
| 117 | $stmt->bindValue('iid', $answer['iid'], Type::INTEGER); |
||
| 118 | $stmt->bindValue('cid', $answer['c_id'], Type::INTEGER); |
||
| 119 | $stmt->execute(); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 130 |