BaseOutput   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 2
dl 0
loc 138
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A printProducts() 0 8 1
A printMessage() 0 4 1
A printTable() 0 16 3
A printBoolean() 0 14 3
B printList() 0 15 5
A getColumnWidth() 0 9 3
A createTable() 0 10 2
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(array_map('trim', $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
            }
37 13
            $table->addRow(array_map('trim', $rowData));
38
        }
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
        } 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
            }
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
        }
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 6
            $products
106
        );
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
        }
133
134 10
        return $width + 5;
135
    }
136
137
    /**
138
     * @param OutputInterface $output
139
     *
140
     * @return Table|TableHelper
141
     */
142 16
    protected function createTable(OutputInterface $output)
143
    {
144 16
        if (!class_exists('Symfony\Component\Console\Helper\Table')) {
145
            $table = new TableHelper(false);
146
        } else {
147 16
            $table = new Table($output);
148
        }
149
150 16
        return $table;
151
    }
152
}
153