Completed
Push — 3.8.x ( 2179fa...0c72eb )
by Tim
06:38
created

RoboFile::prepareDocker()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.4032
c 0
b 0
f 0
cc 6
nc 6
nop 2

How to fix   Long Method   

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
 * RoboFile.php
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
use Lurker\Event\FilesystemEvent;
22
use Symfony\Component\Finder\Finder;
23
24
/**
25
 * Defines the available build tasks.
26
 *
27
 * @author    Tim Wagner <[email protected]>
28
 * @copyright 2016 TechDivision GmbH <[email protected]>
29
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
30
 * @link      https://github.com/techdivision/import-cli-simple
31
 * @link      http://www.techdivision.com
32
 */
33
class RoboFile extends \Robo\Tasks
34
{
35
36
    /**
37
     * The build properties.
38
     *
39
     * @var array
40
     */
41
    protected $properties = array(
42
        'base.dir' => __DIR__,
43
        'etc.dir' => __DIR__ . '/etc',
44
        'dist.dir' => __DIR__ . '/dist',
45
        'vendor.dir' => __DIR__ . '/vendor',
46
        'target.dir' => __DIR__ . '/target',
47
        'symfony.dir' => __DIR__ . '/symfony',
48
        'webapp.name' => 'import-cli-simple',
49
        'webapp.version' => '3.8.0'
50
    );
51
52
    /**
53
     * Run's the composer install command.
54
     *
55
     * @return void
56
     */
57
    public function composerInstall()
58
    {
59
        // optimize autoloader with custom path
60
        $this->taskComposerInstall()
0 ignored issues
show
Bug introduced by
The method preferDist does only exist in Robo\Task\Composer\Install, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
             ->preferDist()
62
             ->optimizeAutoloader()
63
             ->run();
64
    }
65
66
    /**
67
     * Run's the composer update command.
68
     *
69
     * @return void
70
     */
71
    public function composerUpdate()
72
    {
73
        // optimize autoloader with custom path
74
        $this->taskComposerUpdate()
0 ignored issues
show
Bug introduced by
The method preferDist does only exist in Robo\Task\Composer\Update, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
             ->preferDist()
76
             ->optimizeAutoloader()
77
             ->run();
78
    }
79
80
    /**
81
     * Clean up the environment for a new build.
82
     *
83
     * @return void
84
     */
85
    public function clean()
86
    {
87
        $this->taskDeleteDir($this->properties['target.dir'])->run();
88
    }
89
90
    /**
91
     * Prepare's the environment for a new build.
92
     *
93
     * @return void
94
     */
95
    public function prepare()
96
    {
97
        $this->taskFileSystemStack()
0 ignored issues
show
Bug introduced by
The method mkdir does only exist in Robo\Task\Filesystem\FilesystemStack, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
             ->mkdir($this->properties['dist.dir'])
99
             ->mkdir($this->properties['target.dir'])
100
             ->mkdir(sprintf('%s/reports', $this->properties['target.dir']))
101
             ->run();
102
    }
103
104
    /**
105
     * Prepare's the Docker environment for a new build.
106
     *
107
     * @param string $domainName    The domain name used to invoke the Magento 2 instance inside the Docker container
108
     * @param string $containerName The Docker container name
109
     *
110
     * @return void
111
     */
112
    public function prepareDocker($domainName, $containerName)
113
    {
114
115
        // prepare the filesystem
116
        $this->prepare();
117
118
        // initialize the variables to query whether or not the docker container has been started successfully
119
        $counter = 0;
120
        $magentoNotAvailable = true;
121
122
        do {
123
            // reset the result of the CURL request
124
            $res = null;
125
126
            // query whether or not the image already has been loaded
127
            exec(
128
                str_replace(
129
                    array('{domain-name}'),
130
                    array($domainName),
131
                    'curl --resolve {domain-name}:80:127.0.1.1 http://{domain-name}/magento_version'
132
                ),
133
                $res
134
            );
135
136
            // query whether or not the Docker has been started
137
            foreach ($res as $val) {
0 ignored issues
show
Bug introduced by
The expression $res of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
138
                if (strstr($val, 'Magento/')) {
139
                    $magentoNotAvailable = false;
140
                }
141
            }
142
143
            // raise the counter
144
            $counter++;
145
146
            // sleep while the docker container is not available
147
            if ($magentoNotAvailable === true) {
148
                sleep(1);
149
            }
150
151
        } while ($magentoNotAvailable && $counter < 30);
152
153
        // grant the privilieges to connection from outsite the container
154
        $this->taskDockerExec($containerName)
0 ignored issues
show
Bug introduced by
The method interactive does only exist in Robo\Task\Docker\Exec, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            ->interactive()
156
            ->exec('mysql -uroot -proot -e \'GRANT ALL ON *.* TO "magento"@"%" IDENTIFIED BY "magento"\'')
157
            ->run();
158
159
        // flush the privileges
160
        $this->taskDockerExec($containerName)
161
            ->interactive()
162
            ->exec('mysql -uroot -proot -e "FLUSH PRIVILEGES"')
163
            ->run();
164
    }
165
166
    /**
167
     * Creates the a PHAR archive from the sources.
168
     *
169
     * @return void
170
     */
171
    public function createPhar()
172
    {
173
174
        // run the build process
175
        $this->build();
176
177
        // prepare the PHAR archive name
178
        $archiveName = sprintf(
179
            '%s/%s.phar',
180
            $this->properties['target.dir'],
181
            $this->properties['webapp.name']
182
        );
183
184
        // prepare the target directory
185
        $targetDir = $this->properties['target.dir'] . DIRECTORY_SEPARATOR . $this->properties['webapp.version'];
186
187
        // copy the composer.json file
188
        $this->taskFilesystemStack()
0 ignored issues
show
Bug introduced by
The method copy does only exist in Robo\Task\Filesystem\FilesystemStack, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
189
             ->copy(
190
                  __DIR__ . DIRECTORY_SEPARATOR . 'composer.json',
191
                  $targetDir. DIRECTORY_SEPARATOR. 'composer.json'
192
             )->run();
193
194
          // copy the .semver file
195
          $this->taskFilesystemStack()
196
               ->copy(
197
                   __DIR__ . DIRECTORY_SEPARATOR . '.semver',
198
                   $targetDir. DIRECTORY_SEPARATOR. '.semver'
199
               )->run();
200
201
          // copy the bootstrap.php file
202
          $this->taskFilesystemStack()
203
               ->copy(
204
                  __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php',
205
                  $targetDir. DIRECTORY_SEPARATOR. 'bootstrap.php'
206
               )->run();
207
208
        // install the composer dependencies
209
        $this->taskComposerInstall()
0 ignored issues
show
Bug introduced by
The method dir does only exist in Robo\Task\Composer\Install, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
210
            ->dir($targetDir)
211
            ->noDev()
212
            ->optimizeAutoloader()
213
            ->run();
214
215
        // prepare the task
216
        $pharTask = $this->taskPackPhar($archiveName)
0 ignored issues
show
Bug introduced by
The method compress does only exist in Robo\Task\Development\PackPhar, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
217
            ->compress()
218
            ->stub('stub.php');
219
220
        // load a list with all the source files from the vendor directory
221
        $finder = Finder::create()->files()
222
            ->name('*.php')
223
            ->name('*.json')
224
            ->name('.semver')
225
            ->name('services.xml')
226
            ->name('services-1.0.xsd')
227
            ->in($targetDir)
228
            ->ignoreDotFiles(false);
229
230
        // iterate over the source files of the vendor directory and add them to the PHAR archive
231
        foreach ($finder as $file) {
232
            $pharTask->addFile($file->getRelativePathname(), $file->getRealPath());
233
        }
234
235
        // create the PHAR archive
236
        $pharTask->run();
237
238
        // verify PHAR archive is packed correctly
239
        $this->_exec(sprintf('php %s', $archiveName));
240
241
        // prepare the PHAR archive distribution name
242
        $distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
243
244
        // clean up the dist directory
245
        $this->taskCleanDir($this->properties['dist.dir'])->run();
246
247
        // copy the latest PHAR archive to the dist directory
248
        $this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
249
    }
250
251
    /**
252
     * Run's the PHPUnit testsuite.
253
     *
254
     * @return void
255
     */
256
    public function runTestsUnit()
257
    {
258
259
        // run PHPUnit
260
        $this->taskPHPUnit(
0 ignored issues
show
Bug introduced by
The method configFile does only exist in Robo\Task\Testing\PHPUnit, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
261
                sprintf(
262
                    '%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit testsuite"',
263
                    $this->properties['vendor.dir']
264
                )
265
            )
266
            ->configFile('phpunit.xml')
267
            ->run();
268
    }
269
270
    /**
271
     * Run's the integration testsuite.
272
     *
273
     * This task uses the Magento 2 docker image generator from https://github.com/techdivision/magento2-docker-imgen. To execute
274
     * this task, it is necessary that you've setup a running container with the domain name, passed as argument.
275
     *
276
     * @return void
277
     */
278
    public function runTestsIntegration()
279
    {
280
281
        // run the integration testsuite
282
        $this->taskPHPUnit(
0 ignored issues
show
Bug introduced by
The method configFile does only exist in Robo\Task\Testing\PHPUnit, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
283
                sprintf(
284
                    '%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit integration testsuite"',
285
                    $this->properties['vendor.dir']
286
                )
287
             )
288
             ->configFile('phpunit.xml')
289
             ->run();
290
    }
291
292
    /**
293
     * Run's the acceptance testsuite.
294
     *
295
     * This task uses the Magento 2 docker image generator from https://github.com/techdivision/magento2-docker-imgen. To execute
296
     * this task, it is necessary that you've setup a running container with the domain name, passed as argument.
297
     *
298
     * @return void
299
     */
300
    public function runTestsAcceptance()
301
    {
302
        $this->taskBehat()
0 ignored issues
show
Bug introduced by
The method format does only exist in Robo\Task\Testing\Behat, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
303
            ->format('pretty')
304
            ->noInteraction()
305
            ->run();
306
        }
307
308
    /**
309
     * Raising the semver version number.
310
     *
311
     * @return void
312
     */
313
    public function semver()
314
    {
315
        $this->taskSemVer('.semver')
0 ignored issues
show
Bug introduced by
The method prerelease does only exist in Robo\Task\Development\SemVer, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
316
             ->prerelease('beta')
317
             ->run();
318
    }
319
320
    /**
321
     * The complete build process.
322
     *
323
     * @return void
324
     */
325
    public function build()
326
    {
327
        $this->clean();
328
        $this->prepare();
329
        $this->runTestsUnit();
330
    }
331
}
332