RemoveTask::run()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 5
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\Filesystem\Task;
13
14
use Bldr\Exception\TaskRuntimeException;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * @author Aaron Scherer <[email protected]>
19
 */
20
class RemoveTask extends FilesystemTask
21
{
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function configure()
26
    {
27
        parent::configure();
28
        $this->setName('remove')
29
            ->setDescription('Removes all the directories and files provided in `files`');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function run(OutputInterface $output)
36
    {
37
        foreach ($this->resolveFiles() as $file) {
0 ignored issues
show
Bug introduced by
The expression $this->resolveFiles() 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...
38
            if (!$this->fileSystem->exists($file)) {
39
                if (!$this->continueOnError()) {
40
                    throw new TaskRuntimeException($this->getName(), "File `$file` does not exist.");
41
                }
42
43
                if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) {
44
                    $output->writeln(
45
                        [
46
                            "",
47
                            sprintf(
48
                                "<error> [Error] Task: %s \n Message: %s</error>",
49
                                $this->getName(),
50
                                "File `$file` does not exist."
51
                            ),
52
                            ""
53
                        ]
54
                    );
55
                }
56
57
                continue;
58
            }
59
60
            $this->fileSystem->remove([$file]);
61
            $output->writeln(
62
                ["", sprintf("    <info>[%s]</info> - <comment>Deleting %s</comment>", $this->getName(), $file), ""]
63
            );
64
        }
65
    }
66
}
67