TSizes::calculateSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_images\Traits;
4
5
6
/**
7
 * Trait TSizes
8
 * Calculate image sizes
9
 * @package kalanis\kw_images\Traits
10
 */
11
trait TSizes
12
{
13
    /**
14
     * @param int $currentWidth
15
     * @param int $maxWidth
16
     * @param int $currentHeight
17
     * @param int $maxHeight
18
     * @return array{width: int, height: int}
19
     */
20 13
    protected function calculateSize(int $currentWidth, int $maxWidth, int $currentHeight, int $maxHeight): array
21
    {
22 13
        $newWidth = $currentWidth / $maxWidth;
23 13
        $newHeight = $currentHeight / $maxHeight;
24 13
        $ratio = max($newWidth, $newHeight); // due this it's necessary to pass all
25 13
        $ratio = max($ratio, 1.0);
26 13
        $newWidth = (int) ($currentWidth / $ratio);
27 13
        $newHeight = (int) ($currentHeight / $ratio);
28 13
        return ['width' => $newWidth, 'height' => $newHeight];
29
    }
30
}
31