DefaultApplicationConfig::createIO()   F
last analyzed

Complexity

Conditions 16
Paths 768

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 3.266
c 0
b 0
f 0
cc 16
eloc 36
nc 768
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Console\Config;
12
13
use Cubiche\Core\Console\Api\Config\ApplicationConfig;
14
use Webmozart\Console\Api\Application\Application;
15
use Webmozart\Console\Api\Args\Format\Argument;
16
use Webmozart\Console\Api\Args\Format\Option;
17
use Webmozart\Console\Api\Args\RawArgs;
18
use Webmozart\Console\Api\Event\ConsoleEvents;
19
use Webmozart\Console\Api\Event\PreHandleEvent;
20
use Webmozart\Console\Api\Event\PreResolveEvent;
21
use Webmozart\Console\Api\IO\Input;
22
use Webmozart\Console\Api\IO\InputStream;
23
use Webmozart\Console\Api\IO\IO;
24
use Webmozart\Console\Api\IO\Output;
25
use Webmozart\Console\Api\IO\OutputStream;
26
use Webmozart\Console\Api\Resolver\ResolvedCommand;
27
use Webmozart\Console\Formatter\AnsiFormatter;
28
use Webmozart\Console\Formatter\PlainFormatter;
29
use Webmozart\Console\Handler\Help\HelpHandler;
30
use Webmozart\Console\IO\ConsoleIO;
31
use Webmozart\Console\IO\InputStream\StandardInputStream;
32
use Webmozart\Console\IO\OutputStream\ErrorOutputStream;
33
use Webmozart\Console\IO\OutputStream\StandardOutputStream;
34
use Webmozart\Console\UI\Component\NameVersion;
35
36
/**
37
 * DefaultApplicationConfig class.
38
 *
39
 * @author Ivannis Suárez Jerez <[email protected]>
40
 */
41
class DefaultApplicationConfig extends ApplicationConfig
42
{
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function configure()
47
    {
48
        $this
49
            ->setIOFactory(array($this, 'createIO'))
50
            ->addEventListener(ConsoleEvents::PRE_RESOLVE, array($this, 'resolveHelpCommand'))
51
            ->addEventListener(ConsoleEvents::PRE_HANDLE, array($this, 'printVersion'))
52
            ->addOption('help', 'h', Option::NO_VALUE, 'Display help about the command')
53
            ->addOption('quiet', 'q', Option::NO_VALUE, 'Do not output any message')
54
            ->addOption(
55
                'verbose',
56
                'v',
57
                Option::OPTIONAL_VALUE,
58
                'Increase the verbosity of messages: "-v" for normal output, "-vv"
59
                for more verbose output and "-vvv" for debug',
60
                null,
61
                'level'
62
            )
63
            ->addOption('ansi', null, Option::NO_VALUE, 'Force ANSI output')
64
            ->addOption('no-ansi', null, Option::NO_VALUE, 'Disable ANSI output')
65
            ->addOption('no-interaction', 'n', Option::NO_VALUE, 'Do not ask any interactive question')
66
            ->beginCommand('help')
67
                ->markDefault()
68
                ->setDescription('Display the manual of a command')
69
                ->addArgument('command', Argument::OPTIONAL, 'The command name')
70
                ->addOption('man', 'm', Option::NO_VALUE, 'Output the help as man page')
71
                ->addOption('ascii-doc', null, Option::NO_VALUE, 'Output the help as AsciiDoc document')
72
                ->addOption('text', 't', Option::NO_VALUE, 'Output the help as plain text')
73
                ->addOption('xml', 'x', Option::NO_VALUE, 'Output the help as XML')
74
                ->addOption('json', 'j', Option::NO_VALUE, 'Output the help as JSON')
75
                ->setHandler(function () {
76
                    return new HelpHandler();
77
                })
78
            ->end()
79
        ;
80
    }
81
82
    /**
83
     * @param Application       $application
84
     * @param RawArgs           $args
85
     * @param InputStream|null  $inputStream
86
     * @param OutputStream|null $outputStream
87
     * @param OutputStream|null $errorStream
88
     *
89
     * @return ConsoleIO
90
     */
91
    public function createIO(
92
        Application $application,
93
        RawArgs $args,
94
        InputStream $inputStream = null,
95
        OutputStream $outputStream = null,
96
        OutputStream $errorStream = null
97
    ) {
98
        $inputStream = $inputStream ?: new StandardInputStream();
99
        $outputStream = $outputStream ?: new StandardOutputStream();
100
        $errorStream = $errorStream ?: new ErrorOutputStream();
101
        $styleSet = $application->getConfig()->getStyleSet();
102
103
        if ($args->hasToken('--no-ansi')) {
104
            $outputFormatter = $errorFormatter = new PlainFormatter($styleSet);
105
        } elseif ($args->hasToken('--ansi')) {
106
            $outputFormatter = $errorFormatter = new AnsiFormatter($styleSet);
107
        } else {
108
            $outputFormatter = $outputStream->supportsAnsi()
109
                ? new AnsiFormatter($styleSet)
110
                : new PlainFormatter($styleSet)
111
            ;
112
113
            $errorFormatter = $errorStream->supportsAnsi()
114
                ? new AnsiFormatter($styleSet)
115
                : new PlainFormatter($styleSet)
116
            ;
117
        }
118
119
        $io = new ConsoleIO(
120
            new Input($inputStream),
121
            new Output($outputStream, $outputFormatter),
122
            new Output($errorStream, $errorFormatter)
123
        );
124
125
        if ($args->hasToken('-vvv') || $this->isDebug()) {
126
            $io->setVerbosity(IO::DEBUG);
127
        } elseif ($args->hasToken('-vv')) {
128
            $io->setVerbosity(IO::VERY_VERBOSE);
129
        } elseif ($args->hasToken('-v')) {
130
            $io->setVerbosity(IO::VERBOSE);
131
        }
132
133
        if ($args->hasToken('--quiet') || $args->hasToken('-q')) {
134
            $io->setQuiet(true);
135
        }
136
137
        if ($args->hasToken('--no-interaction') || $args->hasToken('-n')) {
138
            $io->setInteractive(false);
139
        }
140
141
        return $io;
142
    }
143
144
    /**
145
     * @param PreResolveEvent $event
146
     */
147
    public function resolveHelpCommand(PreResolveEvent $event)
148
    {
149
        $args = $event->getRawArgs();
150
151
        if ($args->hasToken('-h') || $args->hasToken('--help')) {
152
            $command = $event->getApplication()->getCommand('help');
153
154
            // Enable lenient args parsing
155
            $parsedArgs = $command->parseArgs($args, true);
156
157
            $event->setResolvedCommand(new ResolvedCommand($command, $parsedArgs));
158
            $event->stopPropagation();
159
        }
160
    }
161
162
    /**
163
     * @param PreHandleEvent $event
164
     */
165
    public function printVersion(PreHandleEvent $event)
166
    {
167
        if ($event->getArgs()->isOptionSet('version')) {
168
            $version = new NameVersion($event->getCommand()->getApplication()->getConfig());
169
            $version->render($event->getIO());
170
171
            $event->setHandled(true);
172
        }
173
    }
174
}
175