Strength   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 61
ccs 12
cts 16
cp 0.75
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A weak() 0 3 1
A clip() 0 3 1
A required() 0 3 1
A strong() 0 3 1
A medium() 0 3 1
A create() 0 7 1
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
}