Completed
Push — master ( 076ad5...3005ec )
by AJ
14:22
created

TableHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 32
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C output() 0 28 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\Shell;
19
use Cake\Console\Helper;
20
21
class TableHelper extends Helper
22
{
23
24
    public function output($data, $columns = 10, $terminalWidth = 80)
25
    {
26
        while ((count($data) % $columns != 0) && ($columns > 5)) {
27
            $columns--;
28
        }
29
30
        $maxLength = max(array_map('strlen', $data));
31
32
        while (((($columns * ($maxLength + 3)) + 3) > $terminalWidth) && ($columns >= 1)) {
33
            $columns--;
34
        }
35
36
        $this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ ');
37
38
        for ($i = 0; $i < count($data);) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
39
            $output = " ";
40
            $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...
41
            for ($k = $i; $k < ($columns + $j); $k++) {
42
                if (!isset($data[$i])) {
43
                    $data[$i] = '';
44
                }
45
                $output .= '| ' . str_repeat(' ', floor(($maxLength - strlen($data[$i])) / 2)) . $data[$i] . str_repeat(' ', ceil(($maxLength - strlen($data[$i])) / 2)) . ' ';
46
                $i++;
47
            }
48
            $this->_io->out($output . '|');
49
            $this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ ');
50
        }
51
    }
52
}
53