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.

CreateTestEnv_Command::processCommand()   C
last analyzed

Complexity

Conditions 12
Paths 42

Size

Total Lines 135
Code Lines 40

Duplication

Lines 135
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 135
loc 135
rs 5.034
cc 12
eloc 40
nc 42
nop 3

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/Cli
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com
41
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link      http://datasift.github.io/storyplayer
43
 */
44
45
namespace DataSift\Storyplayer\Cli;
46
47
use Exception;
48
use Phix_Project\CliEngine;
49
use Phix_Project\CliEngine\CliCommand;
50
use Phix_Project\CliEngine\CliResult;
51
use Phix_Project\ExceptionsLib1\Legacy_ErrorHandler;
52
53
/**
54
 * A command to create a new test environment to fill in
55
 *
56
 * @category  Libraries
57
 * @package   Storyplayer/Cli
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 View Code Duplication
class CreateTestEnv_Command extends CliCommand
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
{
65
    public function __construct()
66
    {
67
        // define the command
68
        $this->setName('create-test-env');
69
        $this->setShortDescription('create a new test environment file');
70
        $this->setLongDescription(
71
            "Use this command to create a new Env.php file, complete with "
72
            ."the necessary PHP 'use' statement and comments to help guide you "
73
            ."as you bring your story to life."
74
            .PHP_EOL
75
        );
76
        $this->setArgsList(array(
77
            "namedEnv.php" => "the Env.php file to create"
78
        ));
79
        $this->setSwitches(array(
80
            new CreateTestEnv_BasedOnSwitch,
81
            new CreateTestEnv_ForceSwitch
82
        ));
83
    }
84
85
    /**
86
     *
87
     * @param  CliEngine $engine
88
     * @param  array     $params
89
     * @param  mixed     $additionalContext
90
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be null|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92
    public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null)
93
    {
94
        // do we have the name of the file to create?
95
        if (!isset($params[0])) {
96
            echo "*** error: you must specify which Env.php file to create\n";
97
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
98
        }
99
100
        // we're going to be dealing with some prehistoric parts of PHP
101
        $legacyHandler = new Legacy_ErrorHandler();
102
103
        // create the path to the environment
104
        $storyFolder = dirname($params[0]);
105
        if (!file_exists($storyFolder)) {
106
            try {
107
                $legacyHandler->run(function() use ($storyFolder) {
108
                    mkdir($storyFolder, 0755, true);
109
                });
110
            }
111
            catch (Exception $e) {
112
                echo "*** error: unable to create folder '{$storyFolder}'\n";
113
                exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
114
            }
115
        }
116
117
        // create the environment inside the folder
118
        $env = <<<EOS
119
<?php
120
121
use Storyplayer\SPv3\Modules\Asserts;
122
use Storyplayer\SPv3\Modules\Checkpoint;
123
use Storyplayer\SPv3\Modules\Log;
124
use Storyplayer\SPv3\Stories\BuildTestEnvironment;
125
126
EOS;
127
128
        if (isset($engine->options->basedOn)) {
129
            foreach ($engine->options->basedOn as $templateClass) {
130
                $story .= "use {$templateClass};\n";
0 ignored issues
show
Bug introduced by
The variable $story does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
131
            }
132
        }
133
        $env .= <<<EOS
134
135
// ========================================================================
136
//
137
// TEST ENVIRONMENT DETAILS
138
//
139
// ------------------------------------------------------------------------
140
141
\$env = BuildTestEnvironment::newTestEnvironment();
142
EOS;
143
144
        if (isset($engine->options->basedOn)) {
145
            foreach ($engine->options->basedOn as $templateClass) {
146
                $story .= "\n\$env->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ");";
147
            }
148
        }
149
150
        $env .= <<<EOS
151
152
153
// ========================================================================
154
//
155
// TEST ENVIRONMENT SETUP
156
//
157
// Add one function per step. This makes it easier to debug and maintain
158
// your test environment construction.
159
//
160
// ------------------------------------------------------------------------
161
162
\$env->addTestEnvironmentSetup(function() {
163
    // what are we doing?
164
    \$log = Log::usingLog()->startAction("describe what we are doing");
165
166
    // add the instructions required to build the environment
167
168
    // all done
169
    \$log->endAction();
170
});
171
172
// ========================================================================
173
//
174
// TEST ENVIRONMENT TEARDOWN
175
//
176
// Add one function per step. This makes it easier to debug and maintain
177
// your test environment cleanup.
178
//
179
// ------------------------------------------------------------------------
180
181
\$env->addTestEnvironmentTeardown(function() {
182
    // what are we doing?
183
    \$log = Log::usingLog()->startAction("describe what we are doing");
184
185
    // undo anything that you did in addTestEnvironmentSetup()
186
187
    // all done
188
    \$log->endAction();
189
});
190
191
// ========================================================================
192
//
193
// ALL DONE
194
//
195
// Return your constructed test environment object, for Storyplayer
196
// to execute.
197
//
198
// ------------------------------------------------------------------------
199
200
return \$env;
201
202
EOS;
203
204
        // does the file already exist?
205
        if (file_exists($params[0])) {
206
            // has the user used --force?
207
            if (!isset($engine->options->force) || !$engine->options->force) {
208
                echo "*** error: file '{$params[0]}' already exists\n";
209
                echo "use --force to replace this file with the new Env.php file\n";
210
                exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
211
            }
212
        }
213
214
        try {
215
            $legacyHandler->run(function() use($params, $env) {
216
                file_put_contents($params[0], $env);
217
            });
218
        }
219
        catch (Exception $e) {
220
            echo "*** error: " . $e->getMessage() . "\n";
221
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
222
        }
223
224
        // all done
225
        return 0;
226
    }
227
}
228