DumpTask   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A run() 0 14 3
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 Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * @author Aaron Scherer <[email protected]>
18
 */
19
class DumpTask extends FilesystemTask
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function configure()
25
    {
26
        $this->setName('dump')
27
            ->setDescription('Dumps the `content` into the given file')
28
            ->addParameter('file', true, 'File to dump content to')
29
            ->addParameter('append', true, 'If true, will append to the given file.', false)
30
            ->addParameter('character', true, 'If append is true, character to use to append.', "\n")
31
            ->addParameter('content', true, 'Content to dump to file')
32
        ;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function run(OutputInterface $output)
39
    {
40
        $file = $this->getParameter('file');
41
        if (strpos($file, '/') !== 0) {
42
            $file = getcwd().'/'.ltrim($file, '/');
43
        }
44
45
        $content = $this->getParameter('content');
46
        if ($this->getParameter('append')) {
47
            $content = file_get_contents($file).$this->getParameter('character').$content;
48
        }
49
50
        $this->fileSystem->dumpFile($file, $content);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->getParameter('file') on line 40 can also be of type array; however, Symfony\Component\Filesy...\Filesystem::dumpFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $content defined by $this->getParameter('content') on line 45 can also be of type array; however, Symfony\Component\Filesy...\Filesystem::dumpFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
    }
52
}
53