Conditions | 16 |
Paths | 272 |
Total Lines | 133 |
Code Lines | 56 |
Lines | 0 |
Ratio | 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 |
||
78 | public function playPhases($activity, StoryTeller $st, Injectables $injectables, $phases, $thingBeingPlayed) |
||
79 | { |
||
80 | // shorthand |
||
81 | $output = $st->getOutput(); |
||
82 | |||
83 | // we are going to need something to help us load each of our |
||
84 | // phases |
||
85 | $phaseLoader = $injectables->phaseLoader; |
||
86 | |||
87 | // pre-load all of the phases, before we execute them |
||
88 | // this will trigger any PHP syntax errors now rather than |
||
89 | // when we're part-way through executing our code |
||
90 | $phasesToPlay = []; |
||
91 | foreach ($phases as $phaseName => $isActive) { |
||
92 | $phase = $phaseLoader->loadPhase($st, $phaseName); |
||
93 | $phasesToPlay[$phaseName] = [ |
||
94 | 'phase' => $phase, |
||
95 | 'isActive' => $isActive |
||
96 | ]; |
||
97 | } |
||
98 | |||
99 | // the result of playing this group of phases |
||
100 | $groupResult = null; |
||
101 | if ($thingBeingPlayed){ |
||
102 | $groupResult = $thingBeingPlayed->getResult(); |
||
103 | $groupResult->setActivity($activity); |
||
104 | } |
||
105 | |||
106 | // we need to wrap our code to catch old-style PHP errors |
||
107 | $legacyHandler = new Legacy_ErrorHandler(); |
||
108 | |||
109 | // execute each phase, until either: |
||
110 | // |
||
111 | // 1. all listed phases have been executed, or |
||
112 | // 2. one of the phases says that the story has failed |
||
113 | foreach ($phasesToPlay as $phaseName => $phaseData) |
||
114 | { |
||
115 | // shorthand |
||
116 | $phase = $phaseData['phase']; |
||
117 | $isActive = $phaseData['isActive']; |
||
118 | |||
119 | try { |
||
120 | // tell the world that we're running this phase |
||
121 | $output->startPhase($phase); |
||
122 | |||
123 | // play the phase |
||
124 | $phaseResult = $legacyHandler->run([$this, 'playPhase'], [$st, $injectables, $phase, $isActive, $thingBeingPlayed]); |
||
125 | |||
126 | // remember the result of this phase |
||
127 | //$phaseResults->addResult($phase, $phaseResult); |
||
128 | |||
129 | // now, what do we do? |
||
130 | $nextAction = $phaseResult->getNextAction(); |
||
131 | switch ($nextAction) |
||
132 | { |
||
133 | case self::NEXT_SKIP: |
||
134 | // why? |
||
135 | if ($phaseResult->getPhaseIsBlacklisted()) { |
||
136 | if ($groupResult) { |
||
137 | $groupResult->setPhaseGroupHasBeenBlacklisted($phaseResult); |
||
138 | } |
||
139 | $output->logPhaseSkipped($phaseName, self::MSG_PHASE_BLACKLISTED . ': ' . $phaseResult->getMessage()); |
||
140 | } |
||
141 | else if ($phaseResult->getPhaseCannotRun()) { |
||
142 | $output->logPhaseSkipped($phaseName, $phaseResult->getMessage()); |
||
143 | } |
||
144 | else { |
||
145 | if ($groupResult) { |
||
146 | $groupResult->setPhaseGroupIsIncomplete($phaseResult); |
||
147 | } |
||
148 | $output->logPhaseSkipped($phaseName, self::MSG_PHASE_INCOMPLETE); |
||
149 | } |
||
150 | |||
151 | // tell the output plugins that this phase is over |
||
152 | $output->endPhase($phase, $phaseResult); |
||
153 | return; |
||
154 | |||
155 | case self::NEXT_FAIL: |
||
156 | if ($groupResult) { |
||
157 | $groupResult->setPhaseGroupHasFailed($phaseResult); |
||
158 | } |
||
159 | $output->logPhaseError($phaseName, self::MSG_PHASE_FAILED . ': ' . $phaseResult->getMessage()); |
||
160 | |||
161 | // tell the output plugins that this phase is over |
||
162 | $output->endPhase($phase, $phaseResult); |
||
163 | return; |
||
164 | |||
165 | case self::NEXT_CONTINUE: |
||
166 | if ($groupResult) { |
||
167 | // keep the result up to date, in case this |
||
168 | // is the last one |
||
169 | if ($phaseResult->getPhaseHasBeenSkipped()) { |
||
170 | $groupResult->setPhaseGroupHasBeenSkipped(); |
||
171 | } |
||
172 | else { |
||
173 | $groupResult->setPhaseGroupHasSucceeded(); |
||
174 | } |
||
175 | } |
||
176 | // tell the output plugins that this phase is over |
||
177 | $output->endPhase($phase, $phaseResult); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | // our ultimate safety net |
||
182 | // |
||
183 | // ANY TIME this gets executed, the phase itself has not |
||
184 | // done sufficient error handling of its own!! |
||
185 | catch (Exception $e) { |
||
186 | // tell our output plugins what happened |
||
187 | $output->logPhaseError($phaseName, "uncaught exception: " . (string)$e->getMessage() . $e->getTraceAsString()); |
||
188 | |||
189 | // we need to create a dummy phase result for this |
||
190 | $phaseResult = new Phase_Result($phaseName); |
||
191 | $phaseResult->setPlayingFailed($phaseResult::ERROR, self::MSG_PHASE_FAILED, $e); |
||
192 | |||
193 | // tell the world that this phase is over |
||
194 | $output->endPhase($phase, $phaseResult); |
||
195 | |||
196 | // this is a fatal exception |
||
197 | if ($groupResult) { |
||
198 | $groupResult->setPhaseGroupHasError($phaseResult); |
||
199 | } |
||
200 | |||
201 | // run no more phases |
||
202 | return; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | // all done |
||
207 | // if ($groupResult) { |
||
208 | // $groupResult->setPhaseGroupHasSucceeded(); |
||
209 | // } |
||
210 | } |
||
211 | |||
248 |