K
K
karpyuk72014-05-30 10:04:37
PHP
karpyuk7, 2014-05-30 10:04:37

PHP code refactoring

There is only a part of the application code intended for planning, it needs to be improved somehow. The task says that it would be nice to make one more method (makeDay) that will recursively call itself ???

<?php 
private function _getProgramWeeksByChildProgram($childProgram)
    {
        $weeks = array(); 
        
        $child = $childProgram->getChild();  //new child
        $program = $childProgram->getProgram(); //child program
        $programdays = $program->getDays(); //child programs day
        for ($i=0; $i<count($programdays); $i++) {
            
                $programday = $programdays[$i];
                if ($programday->isVisable()) {
                    $day = new stdClass();
                    $day->id = $programday->getId();
                    $day->date = $programday->getDate();
                    $day->name = $programday->getName();
                    $day->date = $programday->getDate();
                    $day->description = $programday->getDescription();
                    $day->checkable = $programday->isCheckable();
                    $day->ageGroup = $programday->getAgeGroup();
                    $day->checked = $this->_isCheckedDay($programday, $child);
                    
                    // has other age group ?
                    if (isset($programdays[$i+1])) {
                        $nextProgramDay = $programdays[$i+1];
                        if (is_object($nextProgramDay) &&
                            $nextProgramDay->getDate() == $programday->getDate()) {
                            $secondDay = new stdClass();
                            $secondDay->id = $nextProgramDay->getId();
                            $secondDay->date = $nextProgramDay->getDate();
                            $secondDay->name = $nextProgramDay->getName();
                            $secondDay->date = $nextProgramDay->getDate();
                            $secondDay->description = $nextProgramDay->getDescription();
                            $secondDay->checkable = $nextProgramDay->isCheckable();
                            $secondDay->checked = $this->_isCheckedDay($nextProgramDay, $child);
                            $secondDay->ageGroup = $nextProgramDay->getAgeGroup();
                            $day->secondAgeGroup = $secondDay;
                            $i++;
                        }
                    }

                    $weeks[$programday->getWeekNr()][] = $day;
                }
        }
        return $weeks;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-05-30
Protko @Fesor

Read Kent Beck's Extreme Programming for a good description of the basic patterns for refactoring. True, there is more in the context of TDD, but refactoring without tests is always a pain.
As for the above code:
enter individual entities instead of StdClass. For cloning, you can use clone.
And so it is necessary to understand business logic. But now you have too much code, you can easily get confused.

A
Alexander Kubintsev, 2014-05-30
@akubintsev

Yes, there is just an unplowed field for refactoring both inside the method and outside it.
"makeDay" has nothing to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question