B
B
BrotherIce2022-02-14 14:25:24
PHP
BrotherIce, 2022-02-14 14:25:24

What is the best way to move the logic into a common class?

What is the best way to hide the logic in which thread the class is generic
<?php
require_once "../vendor/autoload.php";

use src\ExchangeRates;
use src\SalaryReporter;
use src\Validator;

$exchangeRates = new ExchangeRates();
$rateDollar = $exchangeRates->getDollar();
$report = null;
$textValidationError = null;
$numberHours = filter_input(INPUT_POST, "numberHours", FILTER_SANITIZE_STRING);
$rate = filter_input(INPUT_POST, "rate", FILTER_SANITIZE_STRING);
$validator = new Validator();
if (isset($numberHours, $rate)) {
$arrayErrors = $validator->check($numberHours, $rate);
if (empty($arrayErrors)) {
$salaryReporter = new SalaryReporter();
$report = $salaryReporter->generateReport($numberHours, $rate, $rateDollar);
} else {
$validationError = $arrayErrors;
}
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-14
@Akela_wolf

  1. First you need to think about what you will get from this. Can you reuse this logic? Can you cover the logic with tests? In other words, to formulate the purpose of such refactoring.
  2. Actually the purpose also will define what logic you will allocate. If the task is to reuse, then there is some second usage scenario. Look at it, highlight the elements common with the first. Highlight different elements - think about how to correctly format common and different elements in the code (inheritance, composition, decorators, etc. tricks). Actually at this stage you have a refactoring plan. If the goal is to make testing easier, you start by thinking about the test. And treat the test as a second use case.
  3. Then it remains to implement this refactoring plan.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question