ApplicationHelp::renderUsage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[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 Webmozart\Console\UI\Help;
13
14
use Webmozart\Console\Api\Application\Application;
15
use Webmozart\Console\Api\Args\Format\ArgsFormat;
16
use Webmozart\Console\Api\Args\Format\Argument;
17
use Webmozart\Console\Api\Command\Command;
18
use Webmozart\Console\Api\Command\CommandCollection;
19
use Webmozart\Console\UI\Component\EmptyLine;
20
use Webmozart\Console\UI\Component\LabeledParagraph;
21
use Webmozart\Console\UI\Component\NameVersion;
22
use Webmozart\Console\UI\Component\Paragraph;
23
use Webmozart\Console\UI\Layout\BlockLayout;
24
25
/**
26
 * Renders the help of a console application.
27
 *
28
 * @since  1.0
29
 *
30
 * @author Bernhard Schussek <[email protected]>
31
 */
32
class ApplicationHelp extends AbstractHelp
33
{
34
    /**
35
     * @var Application
36
     */
37
    private $application;
38
39
    /**
40
     * Creates the help.
41
     *
42
     * @param Application $application The application to render.
43
     */
44 19
    public function __construct(Application $application)
45
    {
46 19
        $this->application = $application;
47 19
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 19
    protected function renderHelp(BlockLayout $layout)
53
    {
54 19
        $help = $this->application->getConfig()->getHelp();
55 19
        $commands = $this->application->getNamedCommands();
56 19
        $globalArgsFormat = $this->application->getGlobalArgsFormat();
57
58 19
        $argsFormat = ArgsFormat::build()
59 19
            ->addArgument(new Argument('command', Argument::REQUIRED, 'The command to execute'))
60 19
            ->addArgument(new Argument('arg', Argument::MULTI_VALUED, 'The arguments of the command'))
61
            // Global arguments are rendered in the command usage only
62 19
            ->addOptions($globalArgsFormat->getOptions())
63 19
            ->getFormat();
64
65 19
        $this->renderName($layout, $this->application);
66 19
        $this->renderUsage($layout, $this->application, $argsFormat);
67 19
        $this->renderArguments($layout, $argsFormat->getArguments());
68
69 19
        if ($argsFormat->hasOptions()) {
70 14
            $this->renderGlobalOptions($layout, $argsFormat->getOptions());
71
        }
72
73 19
        if (!$commands->isEmpty()) {
74 13
            $this->renderCommands($layout, $commands);
75
        }
76
77 19
        if ($help) {
78 1
            $this->renderDescription($layout, $help);
79
        }
80 19
    }
81
82
    /**
83
     * Renders the application name.
84
     *
85
     * @param BlockLayout $layout      The layout.
86
     * @param Application $application The application.
87
     */
88 19
    protected function renderName(BlockLayout $layout, Application $application)
89
    {
90 19
        $layout->add(new NameVersion($application->getConfig()));
91 19
        $layout->add(new EmptyLine());
92 19
    }
93
94
    /**
95
     * Renders the "Usage" section.
96
     *
97
     * @param BlockLayout $layout      The layout.
98
     * @param Application $application The application to describe.
99
     * @param ArgsFormat  $argsFormat  The format of the console arguments.
100
     */
101 19
    protected function renderUsage(BlockLayout $layout, Application $application, ArgsFormat $argsFormat)
102
    {
103 19
        $appName = $application->getConfig()->getName();
104
105 19
        $layout->add(new Paragraph('<b>USAGE</b>'));
106 19
        $layout->beginBlock();
107
108 19
        $this->renderSynopsis($layout, $argsFormat, $appName);
109
110 19
        $layout->endBlock();
111 19
        $layout->add(new EmptyLine());
112 19
    }
113
114
    /**
115
     * Renders the "Commands" section.
116
     *
117
     * @param BlockLayout       $layout   The layout.
118
     * @param CommandCollection $commands The commands to describe.
119
     */
120 13 View Code Duplication
    protected function renderCommands(BlockLayout $layout, CommandCollection $commands)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122 13
        $layout->add(new Paragraph('<b>AVAILABLE COMMANDS</b>'));
123 13
        $layout->beginBlock();
124
125 13
        $commands = $commands->toArray();
126 13
        ksort($commands);
127
128 13
        foreach ($commands as $command) {
129 13
            $this->renderCommand($layout, $command);
130
        }
131
132 13
        $layout->endBlock();
133 13
        $layout->add(new EmptyLine());
134 13
    }
135
136
    /**
137
     * Renders a command in the "Commands" section.
138
     *
139
     * @param BlockLayout $layout  The layout.
140
     * @param Command     $command The command to describe.
141
     */
142 13
    protected function renderCommand(BlockLayout $layout, Command $command)
143
    {
144 13
        $description = $command->getConfig()->getDescription();
145 13
        $name = '<c1>'.$command->getName().'</c1>';
146
147 13
        $layout->add(new LabeledParagraph($name, $description));
148 13
    }
149
150
    /**
151
     * Renders the "Description" section.
152
     *
153
     * @param BlockLayout $layout The layout.
154
     * @param string      $help   The help text.
155
     */
156 1
    protected function renderDescription(BlockLayout $layout, $help)
157
    {
158
        $layout
159 1
            ->add(new Paragraph('<b>DESCRIPTION</b>'))
160 1
            ->beginBlock()
161 1
                ->add(new Paragraph($help))
162 1
            ->endBlock()
163 1
            ->add(new EmptyLine())
164
        ;
165 1
    }
166
}
167