I need refactoring this function with SOLID or clean code or design patterns, any can help me? The switch I don't know if I can refactoring
I have created new functions for decoupling but switch loop it's driving me crazy
I added custom functions created by me, hasCorrectSugars check that number of sugars is 0,1 or 2 and checkSugars check that exists sugars...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->setDrinkType($input);
if (in_array($this->drinkType, $this->allowedDrinkTypes)) {
/**
* Tea --> 0.4
* Coffee --> 0.5
* Chocolate --> 0.6
*/
$money = $input->getArgument('money');
switch ($this->drinkType) {
case 'tea':
if ($money < 0.4) {
$output->writeln('The tea costs 0.4');
return 0;
}
break;
case 'coffee':
if ($money < 0.5) {
$output->writeln('The coffee costs 0.5');
return 0;
}
break;
case 'chocolate':
if ($money < 0.6) {
$output->writeln('The chocolate costs 0.6');
return 0;
}
break;
}
if ($this->hasCorrectSugars($input)) {
$this->checkSugars($input, $output);
return 0;
}
$output->writeln('The number of sugars should be between 0 and 2');
return 0;
}
$output->writeln('The drink type should be tea, coffee or chocolate');
return 0;
}
protected function hasCorrectSugars($input): bool
{
$sugars = $input->getArgument('sugars');
if ($sugars >= $this->minSugars && $sugars <= $this->maxSugars) {
return true;
}
return false;
}
protected function checkSugars($input, $output): void
{
$sugars = $input->getArgument('sugars');
$output->write('You have ordered a ' . $this->drinkType);
$this->isExtraHot($input, $output);
$output->write(' with ' . $sugars . ' sugars');
if ($sugars > 0) {
$output->write(' (stick included)');
}
$output->writeln('');
}
CodePudding user response:
I had a go at refactoring. I'm confused about $this->checkSugars
, how does it differ from $this->hasCorrectSugars
?
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->setDrinkType($input);
// Unwrap the if statement and return early instead
if (!in_array($this->drinkType, $this->allowedDrinkTypes)) {
$output->writeln('The drink type should be tea, coffee or chocolate');
return 0;
}
// Have a map of the costs of each drink type
// This could be member variable, i.e. $this->drinkCosts
$drinkCosts = [
'tea' => 0.4,
'coffee' => 0.5,
'chocolate' => 0.6
];
$money = $input->getArgument('money');
$drinkCost = $drinkCosts[$this->drinkType];
if ($money < $drinkCost) {
$output->writeln('The ' . $this->drinkType . ' costs ' . $drinkCost);
return 0;
}
// Return early if sugars are not correct
if (!$this->hasCorrectSugars($input)) {
$output->writeln('The number of sugars should be between 0 and 2');
return 0;
}
$this->checkSugars($input, $output);
return 0; // Should this not return 1?
}
And refactor of hasCorrectSugars
protected function hasCorrectSugars($input): bool
{
$sugars = $input->getArgument('sugars');
return ($sugars >= $this->minSugars && $sugars <= $this->maxSugars)
}
CodePudding user response:
Before the function execute I have a function configure that I believe it has not refactoring, you can refactoring this function?
protected function configure(): void
{
$this->addArgument(
'drink-type',
InputArgument::REQUIRED,
'The type of the drink. (Tea, Coffee or Chocolate)'
);
$this->addArgument(
'money',
InputArgument::REQUIRED,
'The amount of money given by the user'
);
$this->addArgument(
'sugars',
InputArgument::OPTIONAL,
'The number of sugars you want. (0, 1, 2)',
0
);
$this->addOption(
'extra-hot',
'e',
InputOption::VALUE_NONE,
$description = 'If the user wants to make the drink extra hot'
);
}