Completed
Pull Request — master (#52)
by Moshe
02:13
created

HelpFormatter::write()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 56
Code Lines 34

Duplication

Lines 15
Ratio 26.79 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 56
rs 6.5481
cc 11
eloc 34
nc 32
nop 3

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
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
use Consolidation\OutputFormatters\Validate\ValidDataTypesInterface;
6
use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Helper\TableCell;
10
11
/**
12
 * Format an array into CLI help string.
13
 */
14
class HelpFormatter implements FormatterInterface
15
{
16
17
  /**
18
   * @inheritdoc
19
   */
20
  public function write(OutputInterface $output, $data, FormatterOptions $options)
21
  {
22
    $table = new Table($this->output());
0 ignored issues
show
Bug introduced by
The method output() does not seem to exist on object<Consolidation\Out...rmatters\HelpFormatter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
23
    $table->setStyle('compact');
24
25
    // @todo. Get input data as an array.
26
    $output->writeln($command->getDescription());
0 ignored issues
show
Bug introduced by
The variable $command does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
27
28 View Code Duplication
    if ($usages = $command->getExampleUsages()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29
      $table->addRow(['','']);
30
      $table->addRow([new TableCell('Examples:', array('colspan' => 2))]);
31
      foreach ($usages as $key => $description) {
32
        $table->addRow(['  ' . $key, $description]);
33
      }
34
    }
35
36
    if ($arguments = $def->getArguments()) {
0 ignored issues
show
Bug introduced by
The variable $def does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
37
      $table->addRow(['','']);
38
      $table->addRow([new TableCell('Arguments:', array('colspan' => 2))]);
39
      foreach ($arguments as $argument) {
40
        $formatted = $this->formatArgumentName($argument);
0 ignored issues
show
Bug introduced by
The method formatArgumentName() does not seem to exist on object<Consolidation\Out...rmatters\HelpFormatter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
        $description = $argument->getDescription();
42
        if ($argument->getDefault()) {
43
          $description .= ' [default: ' . $argument->getDefault() . ']';
44
        }
45
        $table->addRow(['  ' . $formatted, $description]);
46
      }
47
    }
48
49 View Code Duplication
    if ($options = $def->getOptions()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
      $table->addRow(['','']);
51
      $table->addRow([new TableCell('Options:', array('colspan' => 2))]);
52
      foreach ($options as $option) {
53
        $formatted = $this->formatOption($option);
0 ignored issues
show
Bug introduced by
The method formatOption() does not seem to exist on object<Consolidation\Out...rmatters\HelpFormatter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        $table->addRow(['  ' . $formatted, $option->getDescription()]);
55
      }
56
    }
57
58
    if ($topics = $command->getTopics()) {
59
      $table->addRow(['','']);
60
      $table->addRow([new TableCell('Topics:', array('colspan' => 2))]);
61
      foreach ($topics as $topic) {
62
        // @todo deal with long descriptions
63
        $table->addRow(['  ' . $topic, substr($allTopics[$topic]['description'], 0, 30)]);
0 ignored issues
show
Bug introduced by
The variable $allTopics does not exist. Did you mean $topics?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
64
      }
65
    }
66
67
    if ($aliases = $command->getAliases()) {
68
      $table->addRow(['','']);
69
      $table->addRow([new TableCell('Aliases: '. implode(' ', $aliases), array('colspan' => 2))]);
70
    }
71
72
    $table->render();
73
74
    $output->writeln($help);
0 ignored issues
show
Bug introduced by
The variable $help does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
75
  }
76
}
77