makeNegative()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
4
5
Example:
6
7
makeNegative(1)    // return -1
8
makeNegative(-5)   // return -5
9
makeNegative(0)    // return 0
10
makeNegative(0.12) // return -0.12
11
12
Notes:
13
14
    The number can be negative already, in which case no change is required.
15
    Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.
16
*/
17
18
function makeNegative(float $num) : float
19
{
20
    // Use ternary operator to return -$num if $num is greater than 0, else return $num
21
    return $num > 0 ? -$num : $num;
22
}
23