Completed
Push — master ( 390e4e...b0ec12 )
by Taosikai
12:57
created

InitCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Client\Command;
7
8
use Slince\Config\Config;
9
use Symfony\Component\Console\Input\InputDefinition;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class InitCommand extends Command
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function configure()
20
    {
21
        $this->ignoreValidationErrors();
22
        $this->setName('init')
23
            ->setDefinition($this->createDefinition())
24
            ->setDescription('Create a configuration file in the specified directory');
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $templateConfigFile = __DIR__ . '/../../../spike-template.json';
33
        $config = new Config($templateConfigFile);
34
        $dstPath = $input->getOption('dir');
35
        $extension = $input->getOption('format');
36
        if (!in_array($extension, $this->getSupportedFormats())) {
37
            $output->writeln(sprintf('<error>The format "%s" is not supported</error>', $extension));
38
            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...
39
        }
40
        if (!$config->dump("{$dstPath}/spike.{$extension}")) {
41
            $output->writeln("Can not create the configuration file");
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getNativeDefinition()
49
    {
50
        return $this->createDefinition();
51
    }
52
53
    /**
54
     * Gets all supported formats
55
     * @return array
56
     */
57
    protected function getSupportedFormats()
58
    {
59
        return [
60
            'json', 'yaml', 'xml'
61
        ];
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    private function createDefinition()
68
    {
69
        return new InputDefinition(array(
70
            new InputOption('format', null, InputOption::VALUE_OPTIONAL,
71
                'The configuration file format, support json,ini,xml and yaml', 'json'),
72
            new InputOption('dir', null, InputOption::VALUE_OPTIONAL,
73
                'The directory', getcwd()),
74
        ));
75
    }
76
}