TableHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 40
ccs 0
cts 25
cp 0
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C output() 0 30 8
1
<?php
2
/**
3
 * CakePHPify : CakePHP Plugin for Shopify API Authentication
4
 * Copyright (c) Multidimension.al (http://multidimension.al)
5
 * Github : https://github.com/multidimension-al/cakephpify
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE file
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright (c) Multidimension.al (http://multidimension.al)
12
 * @link      https://github.com/multidimension-al/cakephpify CakePHPify Github
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace Multidimensional\Cakephpify\Shell\Helper;
17
18
use Cake\Console\Helper;
19
use Cake\Console\Shell;
20
21
class TableHelper extends Helper
22
{
23
24
    /**
25
     * @param array $data          array of data to be displayed
26
     * @param int   $columns       maximum number of table columns
27
     * @param int   $terminalWidth maximum table width in characters1
28
     * @return void
29
     */
30
    public function output($data, $columns = 10, $terminalWidth = 80)
31
    {
32
        $dataCount = count($data);
33
        while ($dataCount % $columns != 0 && $columns > 5) {
34
            $columns--;
35
        }
36
37
        $maxLength = max(array_map('strlen', $data));
38
39
        while (((($columns * ($maxLength + 3)) + 3) > $terminalWidth) && ($columns >= 1)) {
40
            $columns--;
41
        }
42
43
        $this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ ');
44
45
        $dataCount = count($data);
46
        for ($i = 0; $i < $dataCount;) {
47
            $output = ' ';
48
            $j = $i;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $j. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
49
            for ($k = $i; $k < ($columns + $j); $k++) {
50
                if (!isset($data[$i])) {
51
                    $data[$i] = '';
52
                }
53
                $output .= '| ' . str_repeat(' ', floor(($maxLength - strlen($data[$i])) / 2)) . $data[$i] . str_repeat(' ', ceil(($maxLength - strlen($data[$i])) / 2)) . ' ';
54
                $i++;
55
            }
56
            $this->_io->out($output . '|');
57
            $this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ ');
58
        }
59
    }
60
}
61