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 — develop ( fdb351...008b6e )
by Stuart
05:27
created

Console::setIsVerbose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
    /**
78
     * are we in verbose mode?
79
     * @var boolean
80
     */
81
    private $isVerbose = false;
82
83
    public function resetSilentMode()
84
    {
85
        $this->silentActivity = false;
86
    }
87
88
    public function setSilentMode()
89
    {
90
        $this->silentActivity = true;
91
    }
92
93
    public function isSilent()
94
    {
95
        return $this->silentActivity;
96
    }
97
98
    public function getIsVerbose()
99
    {
100
        return $this->isVerbose;
101
    }
102
103
    public function setIsVerbose($isVerbose)
104
    {
105
        $this->isVerbose = $isVerbose;
106
    }
107
108
    /**
109
     * called when Storyplayer exits
110
     *
111
     * @return void
112
     */
113
    public function writeFinalReport($duration, $summary)
114
    {
115
        // keep count of what the final results are
116
        $succeededGroups = [];
117
        $skippedGroups   = [];
118
        $failedGroups    = [];
119
120
        // do we have any results?
121
        if (!isset($this->results)) {
122
            // huh - nothing happened at all
123
            $this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle);
124
            $this->writeDuration($duration, $this->writer->puzzledSummaryStyle);
125
            $this->write(PHP_EOL . PHP_EOL);
126
            return;
127
        }
128
129
        // this is our opportunity to tell the user how our story(ies)
130
        // went in detail
131
        foreach ($this->results as $result)
132
        {
133
            // so what happened?
134
            switch ($result->resultCode)
135
            {
136
                case PhaseGroup_Result::OKAY:
137
                    // this is a good result
138
                    $succeededGroups[] = $result;
139
                    break;
140
141
                case PhaseGroup_Result::SKIPPED:
142
                case PhaseGroup_Result::BLACKLISTED:
143
                    // this can legitimately happen
144
                    $skippedGroups[] = $result;
145
                    break;
146
147
                default:
148
                    // everything else is an error of some kind
149
                    $failedGroups[] = $result;
150
            }
151
        }
152
153
        // what's the final tally?
154
        $this->write(PHP_EOL);
155
        if (empty($succeededGroups) && empty($skippedGroups) && empty($failedGroups)) {
156
            // huh - nothing happened at all
157
            $this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle);
158
            $this->writeDuration($duration, $this->writer->puzzledSummaryStyle);
159
            $this->write(PHP_EOL . PHP_EOL);
160
            return;
161
        }
162
        if (empty($failedGroups)) {
163
            // nothing failed
164
            $this->write("SUCCESS - " . count($succeededGroups) . ' PASSED, ' . count($skippedGroups) . ' SKIPPED. Time taken: ', $this->writer->successSummaryStyle);
165
            $this->writeDuration($duration, $this->writer->successSummaryStyle);
166
            $this->write(PHP_EOL . PHP_EOL);
167
            return;
168
        }
169
170
        // if we get here, then at least one thing failed
171
        $this->write("FAILURE - "
172
            . count($succeededGroups) . ' PASSED, '
173
            . count($skippedGroups) . ' SKIPPED, '
174
            . count($failedGroups) . ' FAILED :( Time taken: ',
175
            $this->writer->failSummaryStyle);
176
        $this->writeDuration($duration, $this->writer->failSummaryStyle);
177
        $this->write(PHP_EOL . PHP_EOL);
178
179
        // write out a list of failed tests - someone will want to look
180
        // at them in detail
181
        $this->write("Here's the list of everything that failed:" . PHP_EOL . PHP_EOL);
182
        // foreach ($skippedGroups as $skippedGroup) {
183
        //  $this->writePhaseGroupSkipped();
184
        //  $this->write(' ' . $skippedGroup->activity, $this->writer->activityStyle);
185
        //  $this->write(' ' . $skippedGroup->name . PHP_EOL, $this->writer->nameStyle);
186
187
        //  if (isset($skippedGroup->filename)) {
188
        //      $this->write('       (', $this->writer->punctuationStyle);
189
        //      $this->write($skippedGroup->filename, $this->writer->punctuationStyle);
190
        //      $this->write(')' . PHP_EOL, $this->writer->punctuationStyle);
191
        //  }
192
        // }
193
        foreach ($failedGroups as $failedGroup) {
194
            $this->writePhaseGroupFailed();
195
            $this->write(' ' . $failedGroup->activity, $this->writer->activityStyle);
196
            $this->write(' ' . $failedGroup->name . PHP_EOL, $this->writer->nameStyle);
197
198
            if (isset($failedGroup->filename)) {
199
                $this->write('       (', $this->writer->punctuationStyle);
200
                $this->write($failedGroup->filename, $this->writer->punctuationStyle);
201
                $this->write(')' . PHP_EOL, $this->writer->punctuationStyle);
202
            }
203
        }
204
        $this->write(PHP_EOL);
205
206
        // do we stop here?
207
        if (!$summary) {
208
            // we're in dev mode, which means the error reports have
209
            // already been shown where they happened
210
            return;
211
        }
212
213
        // are we being run by hand?
214
        if (function_exists("posix_isatty") && posix_isatty(STDOUT)) {
215
            $this->write("See ");
216
            $this->write("storyplayer.log", $this->writer->argStyle);
217
            $this->write(" for details on what went wrong." . PHP_EOL . PHP_EOL);
218
        }
219
    }
220
221
    protected function writeDetailedErrorReport($result)
222
    {
223
        // did anything go wrong?
224
        if ($result->getPhaseGroupSucceeded() || $result->getPhaseGroupSkipped()) {
225
            // everything is fine
226
            return;
227
        }
228
229
        // sanity check: we should always have a failedPhase
230
        if (!$result->failedPhase instanceof Phase_Result) {
231
            throw new E5xx_MissingFailedPhase();
232
        }
233
234
        // is it a story?
235
        if ($result instanceof Story_Result) {
236
            $this->showActivityForPhase($result->story, $result->failedPhase);
237
        }
238
    }
239
240
    protected function writeActivity($message, $codeLine = null, $timestamp = null)
241
    {
242
        // when did this happen?
243
        if (!$timestamp) {
244
            $timestamp = time();
245
        }
246
247
        // prepare date/time for output
248
        $now = date('Y-m-d H:i:s', $timestamp);
249
250
        // do we have a codeLine to output?
251
        if ($codeLine) {
252
            $this->write('[', $this->writer->punctuationStyle);
253
            $this->write($now, $this->writer->timeStyle);
254
            $this->write('] ', $this->writer->punctuationStyle);
255
256
            // prepare the code for output
257
            //
258
            // if the code is longer than a single line, we want to display
259
            // it on its own line, to make the output more readable
260
            $parts = explode("\n", $codeLine['code']);
261
            if (count($parts) > 1) {
262
                $codeLine['code'] = PHP_EOL . $codeLine['code'];
263
            }
264
            else {
265
                $codeLine['code'] = ' ' . $codeLine['code'];
266
            }
267
268
            // how many spaces do we need to write?
269
            $shorterMsg = trim($message);
270
            $indent = strlen($message) - strlen($shorterMsg);
271
            $this->write(str_repeat(" ", $indent));
272
            $this->write("code --", $this->writer->commentStyle);
273
            $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...
274
            $this->write(PHP_EOL);
275
        }
276
277
        // output date/time
278
        $this->write('[', $this->writer->punctuationStyle);
279
        $this->write($now, $this->writer->timeStyle);
280
        $this->write('] ', $this->writer->punctuationStyle);
281
282
        // output the message
283
        $this->write(rtrim($message) . PHP_EOL);
284
    }
285
286
    protected function writePhaseGroupSucceeded($msg = 'OKAY')
287
    {
288
        $this->write('[', $this->writer->punctuationStyle);
289
        $this->write($msg, $this->writer->successStyle);
290
        $this->write(']', $this->writer->punctuationStyle);
291
    }
292
293
    protected function writePhaseGroupFailed($msg = 'FAIL')
294
    {
295
        $this->write('[', $this->writer->punctuationStyle);
296
        $this->write($msg, $this->writer->failStyle);
297
        $this->write(']', $this->writer->punctuationStyle);
298
    }
299
300
    protected function writePhaseGroupSkipped($msg = 'SKIP')
301
    {
302
        $this->write('[', $this->writer->punctuationStyle);
303
        $this->write($msg, $this->writer->skippedStyle);
304
        $this->write(']', $this->writer->punctuationStyle);
305
    }
306
307
    /**
308
     * @param Phase_Result $phaseResult
309
     */
310
    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...
311
    {
312
        // what is the phase that we are dealing with?
313
        $phaseName = $phaseResult->getPhaseName();
314
        $activityLog = $phaseResult->activityLog;
315
316
        // our final messages to show the user
317
        $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...
318
        $trace = null;
319
320
        // does the phase have an exception?
321
        $e = $phaseResult->getException();
322
        if ($e)
323
        {
324
            $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...
325
            $trace = $e->getTraceAsString();
326
        }
327
328
        // let's tell the user what we found
329
        $this->write("=============================================================" . PHP_EOL, $this->writer->commentStyle);
330
        $this->write("DETAILED ERROR REPORT" . PHP_EOL);
331
        $this->write("----------------------------------------" . PHP_EOL . PHP_EOL, $this->writer->commentStyle);
332
333
        $this->write("The story failed in the ");
334
        $this->write($phaseName, $this->writer->failedPhaseStyle);
335
        $this->write(" phase." . PHP_EOL);
336
337
        if (!empty($activityLog)) {
338
            $this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle);
339
            $this->write("This is the detailed output from the ");
340
            $this->write($phaseName, $this->writer->failedPhaseStyle);
341
            $this->write(" phase:" . PHP_EOL . PHP_EOL);
342
343
            foreach ($activityLog as $msg) {
344
                $this->writeActivity($msg['text'], $msg['codeLine'], $msg['ts']);
345
            }
346
        }
347
348
        if ($trace !== null) {
349
            $this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle);
350
            $this->write("This is the stack trace for this failure:"
351
                 . PHP_EOL . PHP_EOL
352
                 . CodeFormatter::indentBySpaces($trace, 4) . PHP_EOL . PHP_EOL);
353
        }
354
355
        // all done
356
        $this->write("----------------------------------------" . PHP_EOL, $this->writer->commentStyle);
357
        $this->write("END OF ERROR REPORT" . PHP_EOL);
358
        $this->write("=============================================================" . PHP_EOL . PHP_EOL, $this->writer->commentStyle);
359
    }
360
}
361