OverflowFixer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fix() 0 23 3
A __construct() 0 4 1
1
<?php
2
/**
3
 * File containing the {@see Mistralys\WidthsCalculator\Calculator\OverflowFixer} class.
4
 *
5
 * @package WidthsCalculator
6
 * @see Mistralys\WidthsCalculator\Calculator\OverflowFixer
7
 */
8
9
declare (strict_types=1);
10
11
namespace Mistralys\WidthsCalculator\Calculator;
12
13
use Mistralys\WidthsCalculator\Calculator;
14
15
/**
16
 * Handles converting values that are out of bounds for
17
 * values that the calculator can work with.
18
 *
19
 * @package WidthsCalculator
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class  OverflowFixer
23
{
24
    private Calculator $calculator;
25
    private Operations $operations;
26
    
27
    public function __construct(Calculator $calculator)
28
    {
29
        $this->calculator = $calculator;
30
        $this->operations = $calculator->getOperations();
31
    }
32
    
33
    public function fix() : void
34
    {
35
        $total = $this->operations->calcTotal();
36
        
37
        // to allow space for the missing columns, we base the
38
        // total target percentage on the amount of columns that
39
        // are not missing.
40
        $maxTotal = $this->calculator->getMaxTotal() / ($this->operations->countColumns() - $this->operations->countMissing());
41
        
42
        $cols = $this->calculator->getColumns();
43
        
44
        foreach($cols as $col)
45
        {
46
            // no change for missing columns, they get filled later
47
            if($col->isMissing())
48
            {
49
                continue;
50
            }
51
            
52
            $percentage = $col->getValue() * 100 / $total;
53
            $adjusted = floor($maxTotal * $percentage / 100);
54
            
55
            $col->setValue((int)$adjusted);
56
        }
57
    }
58
}
59