GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PhaseGroup_Player::playPhases()   F
last analyzed

Complexity

Conditions 16
Paths 272

Size

Total Lines 133
Code Lines 56

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 133
rs 3.7109
cc 16
eloc 56
nc 272
nop 5

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer/PlayerLib
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace DataSift\Storyplayer\PlayerLib;
45
46
use Exception;
47
use DataSift\Storyplayer\Injectables;
48
use DataSift\Storyplayer\Phases\Phase;
49
use Phix_Project\ExceptionsLib1\Legacy_ErrorHandler;
50
use Phix_Project\ExceptionsLib1\Legacy_ErrorException;
51
52
/**
53
 * runs a set of phases, and returns the result
54
 *
55
 * @category  Libraries
56
 * @package   Storyplayer/PlayerLib
57
 * @author    Stuart Herbert <[email protected]>
58
 * @copyright 2011-present Mediasift Ltd www.datasift.com
59
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
60
 * @link      http://datasift.github.io/storyplayer
61
 */
62
class PhaseGroup_Player
63
{
64
    const NEXT_CONTINUE = 1;
65
    const NEXT_SKIP     = 2;
66
    const NEXT_FAIL     = 3;
67
68
    const MSG_PHASE_BLACKLISTED = 'phase is not allowed to run';
69
    const MSG_PHASE_FAILED      = 'phase failed with an unexpected error';
70
    const MSG_PHASE_INCOMPLETE  = 'phase is incomplete';
71
    const MSG_PHASE_NOT_ACTIVE  = 'phase is marked as inactive';
72
73
    /**
74
     * @param StoryTeller $st
75
     * @param Injectables $injectables
76
     * @param array $phases
77
     */
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
212
    /**
213
     *
214
     * @param  StoryTeller $st
215
     * @param  Injectables $injectables
216
     * @param  Phase       $phase
217
     * @param  boolean     $isActive
218
     * @return Phase_Result
219
     */
220
    public function playPhase(StoryTeller $st, Injectables $injectables, Phase $phase, $isActive, $thingBeingPlayed = null)
221
    {
222
        // shorthand
223
        $output    = $st->getOutput();
224
        $phaseName = $phase->getPhaseName();
225
226
        // run the phase if we're allowed to
227
        if ($isActive) {
228
            $st->setCurrentPhase($phase);
229
            $phaseResult = $phase->doPhase($thingBeingPlayed);
230
        }
231
        else {
232
            $phaseResult = new Phase_Result($phaseName);
233
            $phaseResult->setContinuePlaying($phaseResult::SKIPPED);
234
            $output->logPhaseSkipped($phaseName, self::MSG_PHASE_NOT_ACTIVE);
235
        }
236
237
        // close off any open log actions
238
        $st->closeAllOpenActions();
239
240
        // close off any log actions left open by closing down
241
        // the test device
242
        $st->closeAllOpenActions();
243
244
        // all done
245
        return $phaseResult;
246
    }
247
}
248