Passed
Pull Request — master (#2)
by Ronan
03:53
created

Volume::sphere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Ronanchilvers\Utility\Number;
4
5
/**
6
 * Methods to calculate the volume of various different shapes
7
 *
8
 * @author Ronan Chilvers <[email protected]>
9
 */
10
class Volume
11
{
12
    /**
13
     * Calculate the volume of a cuboid
14
     *
15
     * Formula:
16
     * ```
17
     *  V = w * h * l
18
     * ```
19
     *
20
     * @param float $height
21
     * @param float $width
22
     * @param float $depth
23
     * @return float
24
     * @author Ronan Chilvers <[email protected]>
25
     */
26
    static public function cuboid($height, $width, $depth): ?float
27
    {
28
        if (0 == (float) $height || 0 == (float) $width || 0 == (float) $depth)
29
        {
30
            return null;
31
        }
32
33
        return $height * $width * $depth;
34
    }
35
36
    /**
37
     * Calculate the volume of a cylinder
38
     *
39
     * Formula:
40
     * ```
41
     *  V = π * r2 * h
42
     * ```
43
     *
44
     * @param float $height
45
     * @param float $radius
46
     * @return float
47
     * @author Ronan Chilvers <[email protected]>
48
     */
49
    static public function cylinder($height, $radius): ?float
50
    {
51
        if (0 == (float) $height || 0 == (float) $radius) {
52
            return null;
53
        }
54
55
        return pi() * pow((float) $radius, 2) * $height;
56
    }
57
58
    /**
59
     * Calculate the volume of a sphere
60
     *
61
     * Formula:
62
     * ```
63
     *  V = 4/3 * π * r3
64
     * ```
65
     * @param float $radius
66
     * @param float
67
     * @author Ronan Chilvers <[email protected]>
68
     */
69
    static public function sphere($radius): ?float
70
    {
71
        if (0 == (float) $radius) {
72
            return null;
73
        }
74
75
        return (4/3) * pi() * pow($radius, 3);
76
    }
77
78
    /**
79
     * Calculate the volume of a hexagonal prism
80
     *
81
     * Formula:
82
     * ```
83
     * V = 3/2 * √3 * a2 *h
84
     * ```
85
     * @param float $height
86
     * @param float $edgeLength
87
     * @return float
88
     * @author Ronan Chilvers <[email protected]>
89
     */
90
    public function hexagonalPrism($height, $edgeLength): ?float
91
    {
92
        return (3/2) * sqrt(3) * pow($edgeLength, 2) * $height;
93
    }
94
}
95