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.
Completed
Push — hotfix/2.3.4 ( 8fdf7c...fb6e65 )
by Stuart
08:35
created

Console::writePhaseGroupSucceeded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
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/Console
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\Console;
45
46
use DataSift\Storyplayer\PlayerLib\Phase_Result;
47
use DataSift\Storyplayer\PlayerLib\PhaseGroup_Result;
48
use DataSift\Storyplayer\PlayerLib\Story;
49
use DataSift\Storyplayer\PlayerLib\Story_Result;
50
use DataSift\Storyplayer\OutputLib\CodeFormatter;
51
use DataSift\Storyplayer\OutputLib\OutputPlugin;
52
53
/**
54
 * the API for console plugins
55
 *
56
 * @category  Libraries
57
 * @package   Storyplayer/Console
58
 * @author    Stuart Herbert <[email protected]>
59
 * @copyright 2011-present Mediasift Ltd www.datasift.com
60
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
61
 * @link      http://datasift.github.io/storyplayer
62
 */
63
abstract class Console extends OutputPlugin
64
{
65
    /**
66
     *
67
     * @var array(PhaseGroup_Result)
68
     */
69
    protected $results;
70
71
    /**
72
     * are we running totally silently?
73
     * @var boolean
74
     */
75
    private $silentActivity = false;
76
77
    public function resetSilentMode()
78
    {
79
        $this->silentActivity = false;
80
    }
81
82
    public function setSilentMode()
83
    {
84
        $this->silentActivity = true;
85
    }
86
87
    public function isSilent()
88
    {
89
        return $this->silentActivity;
90
    }
91
92
    /**
93
     * called when Storyplayer exits
94
     *
95
     * @return void
96
     */
97
    public function writeFinalReport($duration, $summary)
98
    {
99
        // keep count of what the final results are
100
        $succeededGroups = [];
101
        $skippedGroups   = [];
102
        $failedGroups    = [];
103
104
        // do we have any results?
105
        if (!isset($this->results)) {
106
            // huh - nothing happened at all
107
            $this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle);
108
            $this->writeDuration($duration, $this->writer->puzzledSummaryStyle);
109
            $this->write(PHP_EOL . PHP_EOL);
110
            return;
111
        }
112
113
        // this is our opportunity to tell the user how our story(ies)
114
        // went in detail
115
        foreach ($this->results as $result)
116
        {
117
            // so what happened?
118
            switch ($result->resultCode)
119
            {
120
                case PhaseGroup_Result::OKAY:
121
                    // this is a good result
122
                    $succeededGroups[] = $result;
123
                    break;
124
125
                case PhaseGroup_Result::SKIPPED:
126
                case PhaseGroup_Result::BLACKLISTED:
127
                    // this can legitimately happen
128
                    $skippedGroups[] = $result;
129
                    break;
130
131
                default:
132
                    // everything else is an error of some kind
133
                    $failedGroups[] = $result;
134
            }
135
        }
136
137
        // what's the final tally?
138
        $this->write(PHP_EOL);
139
        if (empty($succeededGroups) && empty($skippedGroups) && empty($failedGroups)) {
140
            // huh - nothing happened at all
141
            $this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle);
142
            $this->writeDuration($duration, $this->writer->puzzledSummaryStyle);
143
            $this->write(PHP_EOL . PHP_EOL);
144
            return;
145
        }
146
        if (empty($failedGroups)) {
147
            // nothing failed
148
            $this->write("SUCCESS - " . count($succeededGroups) . ' PASSED, ' . count($skippedGroups) . ' SKIPPED. Time taken: ', $this->writer->successSummaryStyle);
149
            $this->writeDuration($duration, $this->writer->successSummaryStyle);
150
            $this->write(PHP_EOL . PHP_EOL);
151
            return;
152
        }
153
154
        // if we get here, then at least one thing failed
155
        $this->write("FAILURE - "
156
            . count($succeededGroups) . ' PASSED, '
157
            . count($skippedGroups) . ' SKIPPED, '
158
            . count($failedGroups) . ' FAILED :( Time taken: ',
159
            $this->writer->failSummaryStyle);
160
        $this->writeDuration($duration, $this->writer->failSummaryStyle);
161
        $this->write(PHP_EOL . PHP_EOL);
162
163
        // write out a list of failed tests - someone will want to look
164
        // at them in detail
165
        $this->write("Here's the list of everything that failed:" . PHP_EOL . PHP_EOL);
166
        // foreach ($skippedGroups as $skippedGroup) {
167
        //  $this->writePhaseGroupSkipped();
168
        //  $this->write(' ' . $skippedGroup->activity, $this->writer->activityStyle);
169
        //  $this->write(' ' . $skippedGroup->name . PHP_EOL, $this->writer->nameStyle);
170
171
        //  if (isset($skippedGroup->filename)) {
172
        //      $this->write('       (', $this->writer->punctuationStyle);
173
        //      $this->write($skippedGroup->filename, $this->writer->punctuationStyle);
174
        //      $this->write(')' . PHP_EOL, $this->writer->punctuationStyle);
175
        //  }
176
        // }
177
        foreach ($failedGroups as $failedGroup) {
178
            $this->writePhaseGroupFailed();
179
            $this->write(' ' . $failedGroup->activity, $this->writer->activityStyle);
180
            $this->write(' ' . $failedGroup->name . PHP_EOL, $this->writer->nameStyle);
181
182
            if (isset($failedGroup->filename)) {
183
                $this->write('       (', $this->writer->punctuationStyle);
184
                $this->write($failedGroup->filename, $this->writer->punctuationStyle);
185
                $this->write(')' . PHP_EOL, $this->writer->punctuationStyle);
186
            }
187
        }
188
        $this->write(PHP_EOL);
189
190
        // do we stop here?
191
        if (!$summary) {
192
            // we're in dev mode, which means the error reports have
193
            // already been shown where they happened
194
            return;
195
        }
196
197
        // are we being run by hand?
198
        if (function_exists("posix_isatty") && posix_isatty(STDOUT)) {
199
            $this->write("See ");
200
            $this->write("storyplayer.log", $this->writer->argStyle);
201
            $this->write(" for details on what went wrong." . PHP_EOL . PHP_EOL);
202
        }
203
    }
204
205
    protected function writeDetailedErrorReport($result)
206
    {
207
        // did anything go wrong?
208
        if ($result->getPhaseGroupSucceeded() || $result->getPhaseGroupSkipped()) {
209
            // everything is fine
210
            return;
211
        }
212
213
        // sanity check: we should always have a failedPhase
214
        if (!$result->failedPhase instanceof Phase_Result) {
215
            throw new E5xx_MissingFailedPhase();
216
        }
217
218
        // is it a story?
219
        if ($result instanceof Story_Result) {
220
            $this->showActivityForPhase($result->story, $result->failedPhase);
221
        }
222
    }
223
224
    protected function writeActivity($message, $codeLine = null, $timestamp = null)
225
    {
226
        // when did this happen?
227
        if (!$timestamp) {
228
            $timestamp = time();
229
        }
230
231
        // prepare date/time for output
232
        $now = date('Y-m-d H:i:s', $timestamp);
233
234
        // do we have a codeLine to output?
235
        if ($codeLine) {
236
            $this->write('[', $this->writer->punctuationStyle);
237
            $this->write($now, $this->writer->timeStyle);
238
            $this->write('] ', $this->writer->punctuationStyle);
239
240
            // prepare the code for output
241
            //
242
            // if the code is longer than a single line, we want to display
243
            // it on its own line, to make the output more readable
244
            $parts = explode("\n", $codeLine['code']);
245
            if (count($parts) > 1) {
246
                $codeLine['code'] = PHP_EOL . $codeLine['code'];
247
            }
248
            else {
249
                $codeLine['code'] = ' ' . $codeLine['code'];
250
            }
251
252
            // how many spaces do we need to write?
253
            $shorterMsg = trim($message);
254
            $indent = strlen($message) - strlen($shorterMsg);
255
            $this->write(str_repeat(" ", $indent));
256
            $this->write("code --", $this->writer->commentStyle);
257
            $this->writeCodePointCode($codeLine, $this->writer->commentStyle);
0 ignored issues
show
Bug introduced by
The method writeCodePointCode() does not seem to exist on object<DataSift\Storyplayer\Console\Console>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
258
            $this->write(PHP_EOL);
259
        }
260
261
        // output date/time
262
        $this->write('[', $this->writer->punctuationStyle);
263
        $this->write($now, $this->writer->timeStyle);
264
        $this->write('] ', $this->writer->punctuationStyle);
265
266
        // output the message
267
        $this->write(rtrim($message) . PHP_EOL);
268
    }
269
270
    protected function writePhaseGroupSucceeded($msg = 'OKAY')
271
    {
272
        $this->write('[', $this->writer->punctuationStyle);
273
        $this->write($msg, $this->writer->successStyle);
274
        $this->write(']', $this->writer->punctuationStyle);
275
    }
276
277
    protected function writePhaseGroupFailed($msg = 'FAIL')
278
    {
279
        $this->write('[', $this->writer->punctuationStyle);
280
        $this->write($msg, $this->writer->failStyle);
281
        $this->write(']', $this->writer->punctuationStyle);
282
    }
283
284
    protected function writePhaseGroupSkipped($msg = 'SKIP')
285
    {
286
        $this->write('[', $this->writer->punctuationStyle);
287
        $this->write($msg, $this->writer->skippedStyle);
288
        $this->write(']', $this->writer->punctuationStyle);
289
    }
290
291
    /**
292
     * @param Phase_Result $phaseResult
293
     */
294
    protected function showActivityForPhase(Story $story, Phase_Result $phaseResult)
0 ignored issues
show
Unused Code introduced by
The parameter $story is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
295
    {
296
        // what is the phase that we are dealing with?
297
        $phaseName = $phaseResult->getPhaseName();
298
        $activityLog = $phaseResult->activityLog;
299
300
        // our final messages to show the user
301
        $codePoints = [];
0 ignored issues
show
Unused Code introduced by
$codePoints is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
302
        $trace = null;
303
304
        // does the phase have an exception?
305
        $e = $phaseResult->getException();
306
        if ($e)
307
        {
308
            $stackTrace = $e->getTrace();
0 ignored issues
show
Unused Code introduced by
$stackTrace is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
309
            $trace = $e->getTraceAsString();
310
        }
311
312
        // let's tell the user what we found
313
        $this->write("=============================================================" . PHP_EOL, $this->writer->commentStyle);
314
        $this->write("DETAILED ERROR REPORT" . PHP_EOL);
315
        $this->write("----------------------------------------" . PHP_EOL . PHP_EOL, $this->writer->commentStyle);
316
317
        $this->write("The story failed in the ");
318
        $this->write($phaseName, $this->writer->failedPhaseStyle);
319
        $this->write(" phase." . PHP_EOL);
320
321
        if (!empty($activityLog)) {
322
            $this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle);
323
            $this->write("This is the detailed output from the ");
324
            $this->write($phaseName, $this->writer->failedPhaseStyle);
325
            $this->write(" phase:" . PHP_EOL . PHP_EOL);
326
327
            foreach ($activityLog as $msg) {
328
                $this->writeActivity($msg['text'], $msg['codeLine'], $msg['ts']);
329
            }
330
        }
331
332
        if ($trace !== null) {
333
            $this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle);
334
            $this->write("This is the stack trace for this failure:"
335
                 . PHP_EOL . PHP_EOL
336
                 . CodeFormatter::indentBySpaces($trace, 4) . PHP_EOL . PHP_EOL);
337
        }
338
339
        // all done
340
        $this->write("----------------------------------------" . PHP_EOL, $this->writer->commentStyle);
341
        $this->write("END OF ERROR REPORT" . PHP_EOL);
342
        $this->write("=============================================================" . PHP_EOL . PHP_EOL, $this->writer->commentStyle);
343
    }
344
}