GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Command::validateName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[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 Symfony\Component\Console\Command;
13
14
use Symfony\Component\Console\Descriptor\TextDescriptor;
15
use Symfony\Component\Console\Descriptor\XmlDescriptor;
16
use Symfony\Component\Console\Input\InputDefinition;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\BufferedOutput;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Application;
23
use Symfony\Component\Console\Helper\HelperSet;
24
25
/**
26
 * Base class for all commands.
27
 *
28
 * @author Fabien Potencier <[email protected]>
29
 *
30
 * @api
31
 */
32
class Command
33
{
34
    private $application;
35
    private $name;
36
    private $processTitle;
37
    private $aliases = array();
38
    private $definition;
39
    private $help;
40
    private $description;
41
    private $ignoreValidationErrors = false;
42
    private $applicationDefinitionMerged = false;
43
    private $applicationDefinitionMergedWithArgs = false;
44
    private $code;
45
    private $synopsis;
46
    private $helperSet;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param string|null $name The name of the command; passing null means it must be set in configure()
52
     *
53
     * @throws \LogicException When the command name is empty
54
     *
55
     * @api
56
     */
57
    public function __construct($name = null)
58
    {
59
        $this->definition = new InputDefinition();
60
61
        if (null !== $name) {
62
            $this->setName($name);
63
        }
64
65
        $this->configure();
66
67
        if (!$this->name) {
68
            throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
69
        }
70
    }
71
72
    /**
73
     * Ignores validation errors.
74
     *
75
     * This is mainly useful for the help command.
76
     */
77
    public function ignoreValidationErrors()
78
    {
79
        $this->ignoreValidationErrors = true;
80
    }
81
82
    /**
83
     * Sets the application instance for this command.
84
     *
85
     * @param Application $application An Application instance
86
     *
87
     * @api
88
     */
89
    public function setApplication(Application $application = null)
90
    {
91
        $this->application = $application;
92
        if ($application) {
93
            $this->setHelperSet($application->getHelperSet());
94
        } else {
95
            $this->helperSet = null;
96
        }
97
    }
98
99
    /**
100
     * Sets the helper set.
101
     *
102
     * @param HelperSet $helperSet A HelperSet instance
103
     */
104
    public function setHelperSet(HelperSet $helperSet)
105
    {
106
        $this->helperSet = $helperSet;
107
    }
108
109
    /**
110
     * Gets the helper set.
111
     *
112
     * @return HelperSet A HelperSet instance
113
     */
114
    public function getHelperSet()
115
    {
116
        return $this->helperSet;
117
    }
118
119
    /**
120
     * Gets the application instance for this command.
121
     *
122
     * @return Application An Application instance
123
     *
124
     * @api
125
     */
126
    public function getApplication()
127
    {
128
        return $this->application;
129
    }
130
131
    /**
132
     * Checks whether the command is enabled or not in the current environment.
133
     *
134
     * Override this to check for x or y and return false if the command can not
135
     * run properly under the current conditions.
136
     *
137
     * @return bool
138
     */
139
    public function isEnabled()
140
    {
141
        return true;
142
    }
143
144
    /**
145
     * Configures the current command.
146
     */
147
    protected function configure()
148
    {
149
    }
150
151
    /**
152
     * Executes the current command.
153
     *
154
     * This method is not abstract because you can use this class
155
     * as a concrete class. In this case, instead of defining the
156
     * execute() method, you set the code to execute by passing
157
     * a Closure to the setCode() method.
158
     *
159
     * @param InputInterface  $input  An InputInterface instance
160
     * @param OutputInterface $output An OutputInterface instance
161
     *
162
     * @return null|int null or 0 if everything went fine, or an error code
163
     *
164
     * @throws \LogicException When this abstract method is not implemented
165
     *
166
     * @see setCode()
167
     */
168
    protected function execute(InputInterface $input, OutputInterface $output)
169
    {
170
        throw new \LogicException('You must override the execute() method in the concrete command class.');
171
    }
172
173
    /**
174
     * Interacts with the user.
175
     *
176
     * This method is executed before the InputDefinition is validated.
177
     * This means that this is the only place where the command can
178
     * interactively ask for values of missing required arguments.
179
     *
180
     * @param InputInterface  $input  An InputInterface instance
181
     * @param OutputInterface $output An OutputInterface instance
182
     */
183
    protected function interact(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
    }
186
187
    /**
188
     * Initializes the command just after the input has been validated.
189
     *
190
     * This is mainly useful when a lot of commands extends one main command
191
     * where some things need to be initialized based on the input arguments and options.
192
     *
193
     * @param InputInterface  $input  An InputInterface instance
194
     * @param OutputInterface $output An OutputInterface instance
195
     */
196
    protected function initialize(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
    {
198
    }
199
200
    /**
201
     * Runs the command.
202
     *
203
     * The code to execute is either defined directly with the
204
     * setCode() method or by overriding the execute() method
205
     * in a sub-class.
206
     *
207
     * @param InputInterface  $input  An InputInterface instance
208
     * @param OutputInterface $output An OutputInterface instance
209
     *
210
     * @return int The command exit code
211
     *
212
     * @throws \Exception
213
     *
214
     * @see setCode()
215
     * @see execute()
216
     *
217
     * @api
218
     */
219
    public function run(InputInterface $input, OutputInterface $output)
220
    {
221
        // force the creation of the synopsis before the merge with the app definition
222
        $this->getSynopsis();
223
224
        // add the application arguments and options
225
        $this->mergeApplicationDefinition();
226
227
        // bind the input against the command specific arguments/options
228
        try {
229
            $input->bind($this->definition);
230
        } catch (\Exception $e) {
231
            if (!$this->ignoreValidationErrors) {
232
                throw $e;
233
            }
234
        }
235
236
        $this->initialize($input, $output);
237
238
        if (null !== $this->processTitle) {
239
            if (function_exists('cli_set_process_title')) {
240
                cli_set_process_title($this->processTitle);
241
            } elseif (function_exists('setproctitle')) {
242
                setproctitle($this->processTitle);
243
            } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
244
                $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
245
            }
246
        }
247
248
        if ($input->isInteractive()) {
249
            $this->interact($input, $output);
250
        }
251
252
        $input->validate();
253
254
        if ($this->code) {
255
            $statusCode = call_user_func($this->code, $input, $output);
256
        } else {
257
            $statusCode = $this->execute($input, $output);
258
        }
259
260
        return is_numeric($statusCode) ? (int) $statusCode : 0;
261
    }
262
263
    /**
264
     * Sets the code to execute when running this command.
265
     *
266
     * If this method is used, it overrides the code defined
267
     * in the execute() method.
268
     *
269
     * @param callable $code A callable(InputInterface $input, OutputInterface $output)
270
     *
271
     * @return Command The current instance
272
     *
273
     * @throws \InvalidArgumentException
274
     *
275
     * @see execute()
276
     *
277
     * @api
278
     */
279
    public function setCode($code)
280
    {
281
        if (!is_callable($code)) {
282
            throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
283
        }
284
285
        $this->code = $code;
286
287
        return $this;
288
    }
289
290
    /**
291
     * Merges the application definition with the command definition.
292
     *
293
     * This method is not part of public API and should not be used directly.
294
     *
295
     * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments
296
     */
297
    public function mergeApplicationDefinition($mergeArgs = true)
298
    {
299
        if (null === $this->application || (true === $this->applicationDefinitionMerged && ($this->applicationDefinitionMergedWithArgs || !$mergeArgs))) {
300
            return;
301
        }
302
303
        if ($mergeArgs) {
304
            $currentArguments = $this->definition->getArguments();
305
            $this->definition->setArguments($this->application->getDefinition()->getArguments());
306
            $this->definition->addArguments($currentArguments);
307
        }
308
309
        $this->definition->addOptions($this->application->getDefinition()->getOptions());
310
311
        $this->applicationDefinitionMerged = true;
312
        if ($mergeArgs) {
313
            $this->applicationDefinitionMergedWithArgs = true;
314
        }
315
    }
316
317
    /**
318
     * Sets an array of argument and option instances.
319
     *
320
     * @param array|InputDefinition $definition An array of argument and option instances or a definition instance
321
     *
322
     * @return Command The current instance
323
     *
324
     * @api
325
     */
326
    public function setDefinition($definition)
327
    {
328
        if ($definition instanceof InputDefinition) {
329
            $this->definition = $definition;
330
        } else {
331
            $this->definition->setDefinition($definition);
332
        }
333
334
        $this->applicationDefinitionMerged = false;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Gets the InputDefinition attached to this Command.
341
     *
342
     * @return InputDefinition An InputDefinition instance
343
     *
344
     * @api
345
     */
346
    public function getDefinition()
347
    {
348
        return $this->definition;
349
    }
350
351
    /**
352
     * Gets the InputDefinition to be used to create XML and Text representations of this Command.
353
     *
354
     * Can be overridden to provide the original command representation when it would otherwise
355
     * be changed by merging with the application InputDefinition.
356
     *
357
     * This method is not part of public API and should not be used directly.
358
     *
359
     * @return InputDefinition An InputDefinition instance
360
     */
361
    public function getNativeDefinition()
362
    {
363
        return $this->getDefinition();
364
    }
365
366
    /**
367
     * Adds an argument.
368
     *
369
     * @param string $name        The argument name
370
     * @param int    $mode        The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
371
     * @param string $description A description text
372
     * @param mixed  $default     The default value (for InputArgument::OPTIONAL mode only)
373
     *
374
     * @return Command The current instance
375
     *
376
     * @api
377
     */
378
    public function addArgument($name, $mode = null, $description = '', $default = null)
379
    {
380
        $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
381
382
        return $this;
383
    }
384
385
    /**
386
     * Adds an option.
387
     *
388
     * @param string $name        The option name
389
     * @param string $shortcut    The shortcut (can be null)
390
     * @param int    $mode        The option mode: One of the InputOption::VALUE_* constants
391
     * @param string $description A description text
392
     * @param mixed  $default     The default value (must be null for InputOption::VALUE_REQUIRED or InputOption::VALUE_NONE)
393
     *
394
     * @return Command The current instance
395
     *
396
     * @api
397
     */
398
    public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
399
    {
400
        $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
401
402
        return $this;
403
    }
404
405
    /**
406
     * Sets the name of the command.
407
     *
408
     * This method can set both the namespace and the name if
409
     * you separate them by a colon (:)
410
     *
411
     *     $command->setName('foo:bar');
412
     *
413
     * @param string $name The command name
414
     *
415
     * @return Command The current instance
416
     *
417
     * @throws \InvalidArgumentException When the name is invalid
418
     *
419
     * @api
420
     */
421
    public function setName($name)
422
    {
423
        $this->validateName($name);
424
425
        $this->name = $name;
426
427
        return $this;
428
    }
429
430
    /**
431
     * Sets the process title of the command.
432
     *
433
     * This feature should be used only when creating a long process command,
434
     * like a daemon.
435
     *
436
     * PHP 5.5+ or the proctitle PECL library is required
437
     *
438
     * @param string $title The process title
439
     *
440
     * @return Command The current instance
441
     */
442
    public function setProcessTitle($title)
443
    {
444
        $this->processTitle = $title;
445
446
        return $this;
447
    }
448
449
    /**
450
     * Returns the command name.
451
     *
452
     * @return string The command name
453
     *
454
     * @api
455
     */
456
    public function getName()
457
    {
458
        return $this->name;
459
    }
460
461
    /**
462
     * Sets the description for the command.
463
     *
464
     * @param string $description The description for the command
465
     *
466
     * @return Command The current instance
467
     *
468
     * @api
469
     */
470
    public function setDescription($description)
471
    {
472
        $this->description = $description;
473
474
        return $this;
475
    }
476
477
    /**
478
     * Returns the description for the command.
479
     *
480
     * @return string The description for the command
481
     *
482
     * @api
483
     */
484
    public function getDescription()
485
    {
486
        return $this->description;
487
    }
488
489
    /**
490
     * Sets the help for the command.
491
     *
492
     * @param string $help The help for the command
493
     *
494
     * @return Command The current instance
495
     *
496
     * @api
497
     */
498
    public function setHelp($help)
499
    {
500
        $this->help = $help;
501
502
        return $this;
503
    }
504
505
    /**
506
     * Returns the help for the command.
507
     *
508
     * @return string The help for the command
509
     *
510
     * @api
511
     */
512
    public function getHelp()
513
    {
514
        return $this->help;
515
    }
516
517
    /**
518
     * Returns the processed help for the command replacing the %command.name% and
519
     * %command.full_name% patterns with the real values dynamically.
520
     *
521
     * @return string The processed help for the command
522
     */
523
    public function getProcessedHelp()
0 ignored issues
show
Coding Style introduced by
getProcessedHelp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
524
    {
525
        $name = $this->name;
526
527
        $placeholders = array(
528
            '%command.name%',
529
            '%command.full_name%',
530
        );
531
        $replacements = array(
532
            $name,
533
            $_SERVER['PHP_SELF'].' '.$name,
534
        );
535
536
        return str_replace($placeholders, $replacements, $this->getHelp());
537
    }
538
539
    /**
540
     * Sets the aliases for the command.
541
     *
542
     * @param string[] $aliases An array of aliases for the command
543
     *
544
     * @return Command The current instance
545
     *
546
     * @throws \InvalidArgumentException When an alias is invalid
547
     *
548
     * @api
549
     */
550
    public function setAliases($aliases)
551
    {
552
        if (!is_array($aliases) && !$aliases instanceof \Traversable) {
553
            throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
554
        }
555
556
        foreach ($aliases as $alias) {
557
            $this->validateName($alias);
558
        }
559
560
        $this->aliases = $aliases;
561
562
        return $this;
563
    }
564
565
    /**
566
     * Returns the aliases for the command.
567
     *
568
     * @return array An array of aliases for the command
569
     *
570
     * @api
571
     */
572
    public function getAliases()
573
    {
574
        return $this->aliases;
575
    }
576
577
    /**
578
     * Returns the synopsis for the command.
579
     *
580
     * @return string The synopsis
581
     */
582
    public function getSynopsis()
583
    {
584
        if (null === $this->synopsis) {
585
            $this->synopsis = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis()));
586
        }
587
588
        return $this->synopsis;
589
    }
590
591
    /**
592
     * Gets a helper instance by name.
593
     *
594
     * @param string $name The helper name
595
     *
596
     * @return mixed The helper value
597
     *
598
     * @throws \InvalidArgumentException if the helper is not defined
599
     *
600
     * @api
601
     */
602
    public function getHelper($name)
603
    {
604
        return $this->helperSet->get($name);
605
    }
606
607
    /**
608
     * Returns a text representation of the command.
609
     *
610
     * @return string A string representing the command
611
     *
612
     * @deprecated Deprecated since version 2.3, to be removed in 3.0.
613
     */
614 View Code Duplication
    public function asText()
615
    {
616
        $descriptor = new TextDescriptor();
617
        $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
618
        $descriptor->describe($output, $this, array('raw_output' => true));
619
620
        return $output->fetch();
621
    }
622
623
    /**
624
     * Returns an XML representation of the command.
625
     *
626
     * @param bool $asDom Whether to return a DOM or an XML string
627
     *
628
     * @return string|\DOMDocument An XML string representing the command
629
     *
630
     * @deprecated Deprecated since version 2.3, to be removed in 3.0.
631
     */
632 View Code Duplication
    public function asXml($asDom = false)
633
    {
634
        $descriptor = new XmlDescriptor();
635
636
        if ($asDom) {
637
            return $descriptor->getCommandDocument($this);
638
        }
639
640
        $output = new BufferedOutput();
641
        $descriptor->describe($output, $this);
642
643
        return $output->fetch();
644
    }
645
646
    /**
647
     * Validates a command name.
648
     *
649
     * It must be non-empty and parts can optionally be separated by ":".
650
     *
651
     * @param string $name
652
     *
653
     * @throws \InvalidArgumentException When the name is invalid
654
     */
655
    private function validateName($name)
656
    {
657
        if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
658
            throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
659
        }
660
    }
661
}
662