Passed
Push — master ( 6393b8...af0694 )
by Daan
01:37
created

Present   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSmallestSideArea() 0 8 1
A fromString() 0 4 1
A __construct() 0 5 1
A getSurfaceArea() 0 7 1
1
<?php
2
3
namespace DaanMooij\AdventOfCode\Year2015\Day2;
4
5
class Present
6
{
7
    /** @var int */
8
    private int $height;
9
10
    /** @var int */
11
    private int $width;
12
13
    /** @var int */
14
    private int $length;
15
16
    /**
17
     * @param int $height
18
     * @param int $width
19
     * @param int $length
20
     */
21
    public function __construct(int $height, int $width, int $length)
22
    {
23
        $this->height = $height;
24
        $this->width = $width;
25
        $this->length = $length;
26
    }
27
28
    /**
29
     * @param string $dimensions
30
     * @return Present
31
     */
32
    public static function fromString(string $dimensions): Present
33
    {
34
        list($height, $width, $length) = explode('x', $dimensions);
35
        return new Present(intval($height), intval($width), intval($length));
36
    }
37
38
    /** @return int */
39
    public function getSurfaceArea(): int
40
    {
41
        $surfaceArea = (2 * $this->length * $this->width)
42
            + (2 * $this->width * $this->height)
43
            + (2 * $this->height * $this->length);
44
45
        return $surfaceArea;
46
    }
47
48
    /** @return int */
49
    public function getSmallestSideArea(): int
50
    {
51
        $dimensions = [$this->height, $this->width, $this->length];
52
        sort($dimensions);
53
        $smallestSide = array_shift($dimensions);
54
        $secondSmallestSide = array_shift($dimensions);
55
56
        return $smallestSide * $secondSmallestSide;
57
    }
58
}
59