InitCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Client\Command;
13
14
use Slince\Config\Config;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class InitCommand extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this->ignoreValidationErrors();
28
        $this->setName('init')
29
            ->setDefinition($this->createDefinition())
30
            ->setDescription('Create a configuration file in the specified directory');
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $templateConfigFile = __DIR__.'/../../../resources/spike-template.json';
39
        $config = new Config($templateConfigFile);
40
        $dstPath = $input->getOption('dir');
41
        $extension = $input->getOption('format');
42
        if (!in_array($extension, $this->getSupportedFormats())) {
43
            $output->writeln(sprintf('<error>The format "%s" is not supported</error>', $extension));
44
45
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
        }
47
        if (!$config->dump("{$dstPath}/spike.{$extension}")) {
48
            $output->writeln('Can not create the configuration file');
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @codeCoverageIgnore
56
     */
57
    public function getNativeDefinition()
58
    {
59
        return $this->createDefinition();
60
    }
61
62
    /**
63
     * Gets all supported formats.
64
     *
65
     * @return array
66
     */
67
    protected function getSupportedFormats()
68
    {
69
        return [
70
            'json', 'yaml', 'xml',
71
        ];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    private function createDefinition()
78
    {
79
        return new InputDefinition(array(
80
            new InputOption('format', null, InputOption::VALUE_REQUIRED,
81
                'The configuration file format, support json,ini,xml and yaml', 'json'),
82
            new InputOption('dir', null, InputOption::VALUE_REQUIRED,
83
                'The directory', getcwd()),
84
        ));
85
    }
86
}