Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

Application::getDefaultInputDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
rs 9.6666
nc 1
cc 1
eloc 6
nop 0
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[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 CacheTool\Console;
13
14
use CacheTool\Adapter\FastCGI;
15
use CacheTool\Adapter\Cli;
16
use CacheTool\CacheTool;
17
use CacheTool\Command as CacheToolCommand;
18
use CacheTool\Monolog\ConsoleHandler;
19
use Monolog\Logger;
20
use Symfony\Component\Console\Application as BaseApplication;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
26
use Symfony\Component\DependencyInjection\Container;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
29
class Application extends BaseApplication
30
{
31
    const VERSION = '@package_version@';
32
33
    /**
34
     * @var Config
35
     */
36
    protected $config;
37
38
    /**
39
     * @var Logger
40
     */
41
    protected $logger;
42
43
    /**
44
     * @param Config $config
45
     */
46
    public function __construct(Config $config)
47
    {
48
        parent::__construct('CacheTool', self::VERSION);
49
50
        $this->config = $config;
51
        $this->logger = new Logger('cachetool');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function getDefaultCommands()
58
    {
59
        $commands = parent::getDefaultCommands();
60
        $commands[] = new CacheToolCommand\SelfUpdateCommand();
61
62
        $commands[] = new CacheToolCommand\ApcBinDumpCommand();
63
        $commands[] = new CacheToolCommand\ApcBinLoadCommand();
64
        $commands[] = new CacheToolCommand\ApcCacheClearCommand();
65
        $commands[] = new CacheToolCommand\ApcCacheInfoCommand();
66
        $commands[] = new CacheToolCommand\ApcCacheInfoFileCommand();
67
        $commands[] = new CacheToolCommand\ApcKeyDeleteCommand();
68
        $commands[] = new CacheToolCommand\ApcKeyExistsCommand();
69
        $commands[] = new CacheToolCommand\ApcKeyFetchCommand();
70
        $commands[] = new CacheToolCommand\ApcKeyStoreCommand();
71
        $commands[] = new CacheToolCommand\ApcSmaInfoCommand();
72
        $commands[] = new CacheToolCommand\ApcRegexpDeleteCommand();
73
74
        $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
75
        $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
76
        $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
77
        $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
78
        $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
79
        $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
80
        $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
81
        $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
82
        $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
83
84
        $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
85
        $commands[] = new CacheToolCommand\OpcacheResetCommand();
86
        $commands[] = new CacheToolCommand\OpcacheStatusCommand();
87
        $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
88
        $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
89
90
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
91
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
92
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
93
94
        return $commands;
0 ignored issues
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (CacheTool\Command\SelfUp...StatRealpathSizeCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    protected function getDefaultInputDefinition()
101
    {
102
        $definition = parent::getDefaultInputDefinition();
103
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
104
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
105
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
106
107
        return $definition;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function doRun(InputInterface $input, OutputInterface $output)
114
    {
115
        $handler = new ConsoleHandler();
116
        $handler->setOutput($output);
117
        $this->logger->pushHandler($handler);
118
119
        $exitCode = parent::doRun($input, $output);
120
121
        $handler->close();
122
123
        return $exitCode;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
130
    {
131
        if ($command instanceof ContainerAwareInterface) {
132
            $container = $this->buildContainer($input);
133
            $command->setContainer($container);
134
        }
135
136
        return parent::doRunCommand($command, $input, $output);
137
    }
138
139
    /**
140
     * @param  InputInterface     $input
141
     * @return ContainerInterface
142
     */
143
    public function buildContainer(InputInterface $input)
144
    {
145
        if ($input->hasParameterOption('--cli')) {
146
            $this->config['adapter'] = 'cli';
147
        } elseif ($input->hasParameterOption('--fcgi')) {
148
            $this->config['adapter'] = 'fastcgi';
149
            $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
150
        }
151
152
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
153
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
154
        }
155
156
        switch ($this->config['adapter']) {
157
            case 'cli':
158
                $adapter = new Cli();
159
                break;
160
161
            case 'fastcgi':
162
                $adapter = new FastCGI($this->config['fastcgi']);
163
                break;
164
165
            default:
166
                throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli or fastcgi");
167
        }
168
169
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
170
        $container = new Container();
171
        $container->set('cachetool', $cacheTool);
172
        $container->set('logger', $this->logger);
173
174
        return $container;
175
    }
176
}
177