Completed
Pull Request — master (#55)
by Greg
02:19
created

ColumnWidths::calculate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 9
nc 3
nop 3
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations\Wrap;
3
4
use Symfony\Component\Console\Helper\TableStyle;
5
6
/**
7
 * Calculate column widths for table cells.
8
 *
9
 * Influenced by Drush and webmozart/console.
10
 */
11
class ColumnWidths
12
{
13
    public function __construct()
14
    {
15
    }
16
17
    /**
18
     * Given the total amount of available space, and the width of
19
     * the columns to place, calculate the optimum column widths to use.
20
     */
21
    public function calculate($availableWidth, DataCellWidths $dataWidths, DataCellWidths $minimumWidths)
22
    {
23
        // First, check to see if all columns will fit at their full widths.
24
        // If so, do no further calculations. (This may be redundant with
25
        // the short column width calculation.)
26
        if ($dataWidths->totalWidth() <= $availableWidth) {
27
            return $dataWidths->enforceMinimums($minimumWidths);
28
        }
29
30
        // Get the short columns first. If there are none, then distribute all
31
        // of the available width among the remaining columns.
32
        $shortColWidths = $this->getShortColumns($availableWidth, $dataWidths, $minimumWidths);
33
        if ($shortColWidths->isEmpty()) {
34
            return $this->distributeLongColumns($availableWidth, $dataWidths, $minimumWidths);
35
        }
36
37
        // If some short columns were removed, then account for the length
38
        // of the removed columns and make a recursive call (since the average
39
        // width may be higher now, if the removed columns were shorter in
40
        // length than the previous average).
41
        $availableWidth -= $shortColWidths->totalWidth();
42
        $remainingColWidths = $this->calculate($availableWidth, $dataWidths, $minimumWidths);
43
44
        return $shortColWidths->combine($remainingColWidths);
45
    }
46
47
    /**
48
     * Return all of the columns whose longest line length is less than or
49
     * equal to the average width.
50
     */
51
    public function getShortColumns($availableWidth, DataCellWidths $dataWidths, DataCellWidths $minimumWidths)
52
    {
53
        $averageWidth = $this->averageWidth($availableWidth, $dataWidths);
54
        $shortColWidths = $dataWidths->removeShortColumns($averageWidth);
55
        return $shortColWidths->enforceMinimums($minimumWidths);
56
    }
57
58
    /**
59
     * Distribute the remainig space among the columns that were not
60
     * included in the list of "short" columns.
61
     */
62
    public function distributeLongColumns($availableWidth, DataCellWidths $dataWidths, DataCellWidths $minimumWidths)
0 ignored issues
show
Unused Code introduced by
The parameter $minimumWidths is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        // Just distribute the remainder without regard to the
65
        // minimum widths. For now.
66
        return $dataWidths->distribute($availableWidth);
67
/*
68
        // Check to see if there is only one column remaining.
69
        // If so, give it all of the remaining width.
70
        $remainingWidths = $dataWidths->widths();
71
        if (count($remainingWidths) <= 1) {
72
            return $remainingWidths;
73
        }
74
75
        // Start by giving each column its minimum width
76
        $result = $minimumWidths;
77
        $availableWidth -= DataCellWidths::sumWidth($result);
78
79
80
        return $result;
81
*/
82
    }
83
84
    /**
85
     * Calculate how much space is available on average for all columns.
86
     */
87
    public function averageWidth($availableWidth, DataCellWidths $dataWidths)
88
    {
89
        $colkeys = $dataWidths->keys();
90
        return $availableWidth / count($colkeys);
91
    }
92
}
93