Completed
Push — master ( 449b8b...085560 )
by Robert
02:22
created

BaseOutput::createTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 2.1481
1
<?php
2
3
namespace Rs\VersionEye\Output;
4
5
use Rs\VersionEye\Http\Pager;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Helper\TableHelper;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * BaseOutput.
12
 *
13
 * @author Robert Schönthal <[email protected]>
14
 */
15
abstract class BaseOutput
16
{
17
    /**
18
     * prints a table, values can be modified via $callback.
19
     *
20
     * @param OutputInterface $output
21
     * @param string[]        $headings
22
     * @param string[]        $keys
23
     * @param array|Pager     $data
24
     * @param \Closure        $callback
25
     */
26 13
    protected function printTable(OutputInterface $output, array $headings, array $keys, $data, \Closure $callback = null)
27
    {
28 13
        $table = $this->createTable($output);
29
30 13
        $table->setHeaders($headings);
31
32 13
        foreach ($data as $row) {
33 13
            $rowData = array_merge(array_flip($keys), array_intersect_key($row, array_flip($keys)));
34 13
            if ($callback) {
35 5
                $rowData = array_map($callback, array_keys($rowData), $rowData);
36 5
            }
37 13
            $table->addRow($rowData);
38 13
        }
39
40 13
        $table->render($output);
0 ignored issues
show
Unused Code introduced by
The call to Table::render() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
41 13
    }
42
43
    /**
44
     * prints a simple boolean.
45
     *
46
     * @param OutputInterface $output
47
     * @param string          $success
48
     * @param string          $fail
49
     * @param bool            $value
50
     * @param bool            $line
51
     *
52
     * @return string
53
     */
54 18
    protected function printBoolean(OutputInterface $output, $success, $fail, $value, $line = true)
55
    {
56 18
        if ($value) {
57 14
            $message = '<info>' . $success . '</info>';
58 14
        } else {
59 8
            $message = '<error>' . $fail . '</error>';
60
        }
61
62 18
        if (false === $line) {
63 4
            return $message;
64
        }
65
66 14
        $output->writeln($message);
67 14
    }
68
69
    /**
70
     * prints a list combined as <comment>Heading</comment> : <info>Value</info>, values can be modified via $callback.
71
     *
72
     * @param OutputInterface $output
73
     * @param string[]        $headings
74
     * @param string[]        $keys
75
     * @param array           $data
76
     * @param \Closure        $callback
77
     */
78 10
    protected function printList(OutputInterface $output, array $headings, array $keys, array $data, \Closure $callback = null)
79
    {
80 10
        $width = $this->getColumnWidth($headings);
81 10
        $data  = array_merge(array_flip($keys), array_intersect_key($data, array_flip($keys)));
82
83 10
        foreach ($headings as $key => $heading) {
84 10
            $value = array_values($data)[$key];
85 10
            if ($callback) {
86 5
                $value = $callback($heading, $value);
87 5
            }
88 10
            $value = is_bool($value) ? (true === $value ? 'Yes' : 'No') : $value;
89
90 10
            $output->writeln(sprintf('<comment>%s%s</comment> : <info>%s</info>', $heading, str_repeat(' ', $width - strlen($heading)), $value));
91 10
        }
92 10
    }
93
94
    /**
95
     * output for references/search api.
96
     *
97
     * @param OutputInterface $output
98
     * @param array|Pager     $products
99
     */
100 6
    protected function printProducts(OutputInterface $output, $products)
101
    {
102 6
        $this->printTable($output,
103 6
            ['Name', 'Language', 'Version', 'Type'],
104 6
            ['name', 'language', 'version', 'prod_type'],
105
            $products
106 6
        );
107 6
    }
108
109
    /**
110
     * prints a simple message.
111
     *
112
     * @param OutputInterface $output
113
     * @param array           $response
114
     */
115 3
    protected function printMessage(OutputInterface $output, array $response)
116
    {
117 3
        $this->printBoolean($output, $response['message'], $response['message'], true === $response['success']);
118 3
    }
119
120
    /**
121
     * calculates the max width of a given set of string.
122
     *
123
     * @param string[] $headings
124
     *
125
     * @return int
126
     */
127 10
    private function getColumnWidth(array $headings)
128
    {
129 10
        $width = 0;
130 10
        foreach ($headings as $heading) {
131 10
            $width = strlen($heading) > $width ? strlen($heading) : $width;
132 10
        }
133
134 10
        return $width + 5;
135
    }
136
137
    /**
138
     * @param OutputInterface $output
139
     * @return Table|TableHelper
140
     */
141 16
    protected function createTable(OutputInterface $output)
142
    {
143 16
        if (!class_exists('Symfony\Component\Console\Helper\Table')) {
144
            $table = new TableHelper(false);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Console\Helper\TableHelper has been deprecated with message: since version 2.5, to be removed in 3.0 Use {@link Table} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
145
        } else {
146 16
            $table = new Table($output);
147
        }
148
149 16
        return $table;
150
    }
151
}
152