ExportTask::run()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Miscellaneous\Task;
13
14
use Bldr\Block\Core\Task\AbstractTask;
15
use Bldr\Block\Miscellaneous\Service\EnvironmentVariableRepository;
16
use Bldr\Exception\TaskRuntimeException;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * @author Luis Cordova   <[email protected]>
21
 * @author Raul Rodriguez <[email protected]>
22
 * @author Aaron Scherer  <[email protected]>
23
 */
24
class ExportTask extends AbstractTask
25
{
26
    /**
27
     * @type EnvironmentVariableRepository
28
     */
29
    protected $environmentVariableRepository;
30
31
    /**
32
     * @param EnvironmentVariableRepository $environmentVariableRepository
33
     */
34
    public function __construct(EnvironmentVariableRepository $environmentVariableRepository)
35
    {
36
        $this->environmentVariableRepository = $environmentVariableRepository;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function configure()
43
    {
44
        $this->setName('export')
45
            ->setDescription('Exports an environmental variable within the context of the bldr task run.')
46
            ->addParameter('arguments', true, 'Arguments to run on the export', [])
47
        ;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function run(OutputInterface $output)
54
    {
55
        foreach ($this->getParameter('arguments') as $argument) {
0 ignored issues
show
Bug introduced by
The expression $this->getParameter('arguments') of type array|string 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...
56
            if (2 !== count(explode('=', $argument))) {
57
                throw new TaskRuntimeException(
58
                    $this->getName(),
59
                    'Each argument needs to follow the pattern e.g. SYMFONY_ENV=prod'
60
                );
61
            };
62
            $this->environmentVariableRepository->addEnvironmentVariable($argument);
63
        }
64
    }
65
}
66