From 12ef51b96ad802a6fb5b81c2e9f7bf91e82ede24 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 13:51:21 +0100 Subject: [PATCH 01/11] build: create Dockerfile and script bash --- .../php-divergent_change-01_base/Dockerfile | 6 ++++ .../php-divergent_change-01_base/scripts.sh | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/php/php-divergent_change-01_base/Dockerfile create mode 100755 examples/php/php-divergent_change-01_base/scripts.sh diff --git a/examples/php/php-divergent_change-01_base/Dockerfile b/examples/php/php-divergent_change-01_base/Dockerfile new file mode 100644 index 00000000..675123a1 --- /dev/null +++ b/examples/php/php-divergent_change-01_base/Dockerfile @@ -0,0 +1,6 @@ +FROM php:7.4 +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer +RUN apt-get update && apt-get install -y git zip unzip + +COPY . app +WORKDIR /app diff --git a/examples/php/php-divergent_change-01_base/scripts.sh b/examples/php/php-divergent_change-01_base/scripts.sh new file mode 100755 index 00000000..9e017976 --- /dev/null +++ b/examples/php/php-divergent_change-01_base/scripts.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +create_docker_image() { + echo "Create docker image image" + docker build . -t refactoring:latest + echo "install libraries" + docker run --rm --interactive --tty --volume $PWD:/app refactoring composer install +} +help() { + echo "./scripts.sh create: Create the docker image" + echo "./scripts.sh test: Run phpunit" +} +run_tests() { + echo "run tests" + docker run --rm --volume $PWD:/app refactoring php /app/vendor/bin/phpunit --colors="always" --no-logging --no-coverage ${args} +} + +case $1 in + removeImage) + echo "remove docker image" + docker rmi refactoring:latest + ;; + createImage) + create_docker_image + ;; + test) + run_tests + ;; + help) + help + ;; + *) + echo "Esta opciĆ³n $1 no existe." + help + ;; +esac From d616b1171c4af469d22db0a442a472c48328b810 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 13:54:28 +0100 Subject: [PATCH 02/11] refactor: rename variables --- .../Controller/CourseStepsGetController.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index cd144d72..077e261e 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -30,16 +30,19 @@ public function get(string $courseId): string continue; } - $type = $row[1]; + $id = $row[0]; + $type = $row[1]; + $durationVideo = $row[3]; + $durationQuiz = $row[2]; + $duration = 0; $points = 0; if ($type === 'video') { - $duration = $row[3] * 1.1; // 1.1 = due to video pauses + $duration = $durationVideo * 1.1; // 1.1 = due to video pauses } - if ($type === 'quiz') { - $duration = $row[2] * 0.5; // 0.5 = time in minutes per question + $duration = $durationQuiz * 0.5; // 0.5 = time in minutes per question } if ($type !== 'video' && $type !== 'quiz') { @@ -47,17 +50,17 @@ public function get(string $courseId): string } if ($type === 'video') { - $points = $row[3] * 1.1 * 100; + $points = $durationVideo * 1.1 * 100; } if ($type === 'quiz') { - $points = $row[2] * 0.5 * 10; + $points = $durationQuiz * 0.5 * 10; } $results .= json_encode( [ - 'id' => $row[0], - 'type' => $row[1], + 'id' => $id, + 'type' => $type, 'duration' => $duration, 'points' => $points ], From 14af2ec732603e30b6753b6d40ac286ad1d841b8 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 13:56:33 +0100 Subject: [PATCH 03/11] refactor: move if sentence --- .../src/Controller/CourseStepsGetController.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index 077e261e..45076ffa 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -30,8 +30,12 @@ public function get(string $courseId): string continue; } - $id = $row[0]; $type = $row[1]; + if ($type !== 'video' && $type !== 'quiz') { + continue; + } + + $id = $row[0]; $durationVideo = $row[3]; $durationQuiz = $row[2]; @@ -45,9 +49,6 @@ public function get(string $courseId): string $duration = $durationQuiz * 0.5; // 0.5 = time in minutes per question } - if ($type !== 'video' && $type !== 'quiz') { - continue; - } if ($type === 'video') { $points = $durationVideo * 1.1 * 100; From ba33b62410876f8cde3018b3e7b0e0c151be9f0c Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 13:58:42 +0100 Subject: [PATCH 04/11] refactor: evaluate if $csv is empty when we have its content --- .../src/Controller/CourseStepsGetController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index 45076ffa..fbe8471c 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -18,6 +18,9 @@ public function __construct(Platform $platform) public function get(string $courseId): string { $csv = $this->platform->findCourseSteps($courseId); + if (empty($csv)) { + return '[]'; + } $results = '['; @@ -26,9 +29,7 @@ public function get(string $courseId): string foreach ($csvLines as $index => $row) { $row = str_getcsv($row); - if (empty($csv)) { - continue; - } + $type = $row[1]; if ($type !== 'video' && $type !== 'quiz') { From 132b9e0a67033621546846c154e61005ddf4c1f6 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 14:05:19 +0100 Subject: [PATCH 05/11] refactor: create function duration and points --- .../Controller/CourseStepsGetController.php | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index fbe8471c..8ca998a1 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -28,9 +28,6 @@ public function get(string $courseId): string foreach ($csvLines as $index => $row) { $row = str_getcsv($row); - - - $type = $row[1]; if ($type !== 'video' && $type !== 'quiz') { continue; @@ -40,31 +37,12 @@ public function get(string $courseId): string $durationVideo = $row[3]; $durationQuiz = $row[2]; - $duration = 0; - $points = 0; - - if ($type === 'video') { - $duration = $durationVideo * 1.1; // 1.1 = due to video pauses - } - if ($type === 'quiz') { - $duration = $durationQuiz * 0.5; // 0.5 = time in minutes per question - } - - - if ($type === 'video') { - $points = $durationVideo * 1.1 * 100; - } - - if ($type === 'quiz') { - $points = $durationQuiz * 0.5 * 10; - } - $results .= json_encode( [ 'id' => $id, 'type' => $type, - 'duration' => $duration, - 'points' => $points + 'duration' => $this->duration($type, $durationVideo, $durationQuiz), + 'points' => $this->points($type, $durationVideo, $durationQuiz) ], JSON_THROW_ON_ERROR ); @@ -78,4 +56,28 @@ public function get(string $courseId): string return $results; } + + private function duration(string $type, $durationVideo, $durationQuiz): float + { + $duration = 0; + if ($type === 'video') { + $duration = $durationVideo * 1.1; // 1.1 = due to video pauses + } + if ($type === 'quiz') { + $duration = $durationQuiz * 0.5; // 0.5 = time in minutes per question + } + return $duration; + } + + private function points(string $type, $durationVideo, $durationQuiz): float + { + $points = 0; + if ($type === 'video') { + $points = $durationVideo * 1.1 * 100; + } + if ($type === 'quiz') { + $points = $durationQuiz * 0.5 * 10; + } + return $points; + } } From 418a4689824e0446e1640cffa48893e2c86d774c Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 14:10:11 +0100 Subject: [PATCH 06/11] refactor: create a loop to get all the steps --- .../Controller/CourseStepsGetController.php | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index 8ca998a1..5fef2c78 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -22,31 +22,29 @@ public function get(string $courseId): string return '[]'; } - $results = '['; - $csvLines = explode(PHP_EOL, $csv); - foreach ($csvLines as $index => $row) { + $steps = []; + foreach ($csvLines as $row) { $row = str_getcsv($row); $type = $row[1]; if ($type !== 'video' && $type !== 'quiz') { continue; } - $id = $row[0]; - $durationVideo = $row[3]; - $durationQuiz = $row[2]; - - $results .= json_encode( - [ - 'id' => $id, - 'type' => $type, - 'duration' => $this->duration($type, $durationVideo, $durationQuiz), - 'points' => $this->points($type, $durationVideo, $durationQuiz) - ], - JSON_THROW_ON_ERROR - ); + $durationInitialVideo = $row[3]; + $durationInitialQuiz = $row[2]; + $steps[] = [ + 'id' => $row[0], + 'type' => $type, + 'duration' => $this->duration($type, $durationInitialVideo, $durationInitialQuiz), + 'points' => $this->points($type, $durationInitialVideo, $durationInitialQuiz) + ]; + } + $results = '['; + foreach ($steps as $index => $step) { + $results .= json_encode($step, JSON_THROW_ON_ERROR); if ($index !== count($csvLines) - 1) { $results .= ','; } From bc0dc2fb7786b23cab67792886d2ab9f5f8c6b2c Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 14:14:09 +0100 Subject: [PATCH 07/11] refactor: generate result string --- .../src/Controller/CourseStepsGetController.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index 5fef2c78..fe9de11e 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -42,17 +42,14 @@ public function get(string $courseId): string ]; } - $results = '['; - foreach ($steps as $index => $step) { - $results .= json_encode($step, JSON_THROW_ON_ERROR); - if ($index !== count($csvLines) - 1) { - $results .= ','; - } + $results = []; + foreach ($steps as $step) { + $results[] = json_encode($step, JSON_THROW_ON_ERROR); } - $results .= ']'; + $resultString = '[' . implode(',', $results) . ']'; - return $results; + return $resultString; } private function duration(string $type, $durationVideo, $durationQuiz): float From 356d78177a4f451c6c13c3a010d56c37a6c71e0b Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 14:17:46 +0100 Subject: [PATCH 08/11] refactor: create two functions --- .../Controller/CourseStepsGetController.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index fe9de11e..b742e2cd 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -21,9 +21,24 @@ public function get(string $courseId): string if (empty($csv)) { return '[]'; } - $csvLines = explode(PHP_EOL, $csv); + $steps = $this->createSteps($csvLines); + $resultString = $this->serialize($steps); + return $resultString; + } + + private function serialize(array $steps): string + { + $results = []; + foreach ($steps as $step) { + $results[] = json_encode($step, JSON_THROW_ON_ERROR); + } + $resultString = '[' . implode(',', $results) . ']'; + return $resultString; + } + private function createSteps(array $csvLines): array + { $steps = []; foreach ($csvLines as $row) { $row = str_getcsv($row); @@ -41,15 +56,7 @@ public function get(string $courseId): string 'points' => $this->points($type, $durationInitialVideo, $durationInitialQuiz) ]; } - - $results = []; - foreach ($steps as $step) { - $results[] = json_encode($step, JSON_THROW_ON_ERROR); - } - - $resultString = '[' . implode(',', $results) . ']'; - - return $resultString; + return $steps; } private function duration(string $type, $durationVideo, $durationQuiz): float From e60702d07bebed929f23e324c7cebb1c293fa591 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 14:21:50 +0100 Subject: [PATCH 09/11] refactor: simplify the function get in CourseStepsGetController --- .../src/Controller/CourseStepsGetController.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index b742e2cd..726c54bb 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -18,13 +18,8 @@ public function __construct(Platform $platform) public function get(string $courseId): string { $csv = $this->platform->findCourseSteps($courseId); - if (empty($csv)) { - return '[]'; - } - $csvLines = explode(PHP_EOL, $csv); - $steps = $this->createSteps($csvLines); - $resultString = $this->serialize($steps); - return $resultString; + $steps = $this->createSteps($csv); + return $this->serialize($steps); } private function serialize(array $steps): string @@ -33,12 +28,12 @@ private function serialize(array $steps): string foreach ($steps as $step) { $results[] = json_encode($step, JSON_THROW_ON_ERROR); } - $resultString = '[' . implode(',', $results) . ']'; - return $resultString; + return '[' . implode(',', $results) . ']'; } - private function createSteps(array $csvLines): array + private function createSteps(string $csv): array { + $csvLines = explode(PHP_EOL, $csv); $steps = []; foreach ($csvLines as $row) { $row = str_getcsv($row); From 216b95d1f60b6718cec34a900e4c59d26a09d365 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 16:55:41 +0100 Subject: [PATCH 10/11] refactor: create constants --- .../Controller/CourseStepsGetController.php | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index 726c54bb..bd354b11 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -8,6 +8,13 @@ final class CourseStepsGetController { + private const VIDEO_TYPE = 'video'; + private const QUIZ_TYPE = 'quiz'; + private const DURATION_VIDEO_MINUTES = 1.1; + private const DURATION_QUIZ_MINUTES = 0.5; + private const VIDEO_POINTS_PER_MINUTE = 100; + private const QUIZ_POINTS_PER_MINUTE = 10; + private Platform $platform; public function __construct(Platform $platform) @@ -38,7 +45,7 @@ private function createSteps(string $csv): array foreach ($csvLines as $row) { $row = str_getcsv($row); $type = $row[1]; - if ($type !== 'video' && $type !== 'quiz') { + if (in_array($type, [self::VIDEO_TYPE, self::QUIZ_TYPE], true)) { continue; } @@ -54,27 +61,19 @@ private function createSteps(string $csv): array return $steps; } - private function duration(string $type, $durationVideo, $durationQuiz): float + private function duration(string $type, float $durationVideo, float $durationQuiz): float { - $duration = 0; - if ($type === 'video') { - $duration = $durationVideo * 1.1; // 1.1 = due to video pauses - } - if ($type === 'quiz') { - $duration = $durationQuiz * 0.5; // 0.5 = time in minutes per question + if ($type === self::VIDEO_TYPE) { + return $durationVideo * self::DURATION_VIDEO_MINUTES; } - return $duration; + return $durationQuiz * self::DURATION_QUIZ_MINUTES; } - private function points(string $type, $durationVideo, $durationQuiz): float + private function points(string $type, float $durationVideo, float $durationQuiz): float { - $points = 0; - if ($type === 'video') { - $points = $durationVideo * 1.1 * 100; - } - if ($type === 'quiz') { - $points = $durationQuiz * 0.5 * 10; + if ($type === self::VIDEO_TYPE) { + return $durationVideo * self::DURATION_VIDEO_MINUTES * self::VIDEO_POINTS_PER_MINUTE; } - return $points; + return $durationQuiz * self::DURATION_QUIZ_MINUTES * self::QUIZ_POINTS_PER_MINUTE; } } From ffb76f41de2328c573ceec44adb7c0112bee43ee Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Sun, 6 Nov 2022 17:01:02 +0100 Subject: [PATCH 11/11] refactor: rename functions --- .../src/Controller/CourseStepsGetController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index bd354b11..8080e285 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -25,11 +25,11 @@ public function __construct(Platform $platform) public function get(string $courseId): string { $csv = $this->platform->findCourseSteps($courseId); - $steps = $this->createSteps($csv); - return $this->serialize($steps); + $steps = $this->parseSteps($csv); + return $this->serializeSteps($steps); } - private function serialize(array $steps): string + private function serializeSteps(array $steps): string { $results = []; foreach ($steps as $step) { @@ -38,7 +38,7 @@ private function serialize(array $steps): string return '[' . implode(',', $results) . ']'; } - private function createSteps(string $csv): array + private function parseSteps(string $csv): array { $csvLines = explode(PHP_EOL, $csv); $steps = [];