Strength::required()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctefan\Kiwi;
5
6
/**
7
 * Symbolic constraint strengths.
8
 *
9
 * Required constraints must be satisfied. Remaining strengths are satisfied with declining priority.
10
 */
11
class Strength
12
{
13
    /**
14
     * @return float
15
     */
16 21
    static public function required(): float
17
    {
18 21
        return self::create(1000.0, 1000.0, 1000.0);
19
    }
20
21
    /**
22
     * @return float
23
     */
24 1
    static public function strong(): float
25
    {
26 1
        return self::create(1.0, 0.0, 0.0);
27
    }
28
29
    /**
30
     * @return float
31
     */
32
    static public function medium(): float
33
    {
34
        return self::create(0.0, 1.0, 0.0);
35
    }
36
37
    /**
38
     * @return float
39
     */
40 3
    static public function weak(): float
41
    {
42 3
        return self::create(0.0, 0.0, 1.0);
43
    }
44
45
    /**
46
     * Create a new symbolic strength.
47
     *
48
     * @param float $a
49
     * @param float $b
50
     * @param float $c
51
     * @param float $w
52
     * @return float
53
     */
54 21
    static public function create(float $a, float $b, float $c, float $w = 1.0): float
55
    {
56 21
        $strength = 0.0;
57 21
        $strength += max(0.0, min(1000.0, $a * $w)) * 1000000.0;
58 21
        $strength += max(0.0, min(1000.0, $b * $w)) * 1000.0;
59 21
        $strength += max(0.0, min(1000.0, $c * $w));
60 21
        return $strength;
61
    }
62
63
    /**
64
     * Clamp a symbolic strength to the allowed min and max.
65
     *
66
     * @param float $strength
67
     * @return float
68
     */
69
    static public function clip(float $strength): float
70
    {
71
        return max(0.0, min(self::required(), $strength));
72
    }
73
}