Normalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 50
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 14 2
A getScaledRank() 0 9 1
A __construct() 0 6 1
A getDivider() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpScience\PageRank\Service\PageRankAlgorithm;
6
7
use PhpScience\PageRank\Data\NodeCollectionInterface;
8
9 1
class Normalizer implements NormalizerInterface
10
{
11
    private float $scaleBottom;
12
    private float $scaleTop;
13
14 6
    public function __construct(
15
        float $scaleBottom = 1,
16
        float $scaleTop = 10
17
    ) {
18 6
        $this->scaleBottom = $scaleBottom;
19 6
        $this->scaleTop = $scaleTop;
20 6
    }
21
22 5
    public function normalize(
23
        NodeCollectionInterface $nodeCollection,
24
        float $lowestRank,
25
        float $highestRank
26
    ): void {
27 5
        $divider = $this->getDivider($lowestRank, $highestRank);
28
29 5
        foreach ($nodeCollection->getNodes() as $node) {
30 5
            $rank = $this->getScaledRank(
31 5
                $node->getRank(),
32
                $lowestRank,
33
                $divider
34
            );
35 5
            $node->setRank($rank);
36
        }
37 5
    }
38
39 5
    private function getDivider(float $lowestRank, float $highestRank): float
40
    {
41 5
        $divider = $highestRank - $lowestRank;
42
43 5
        if (.0 === $divider) {
0 ignored issues
show
introduced by
The condition 0.0 === $divider is always false.
Loading history...
44 2
            $divider = 1;
45
        }
46
47 5
        return $divider;
48
    }
49
50 5
    private function getScaledRank(
51
        float $value,
52
        float $lowestRank,
53
        float $divider
54
    ): float {
55 5
        $normalized = ($value - $lowestRank) / $divider;
56 5
        $multiplier = $this->scaleTop - $this->scaleBottom;
57
58 5
        return ($normalized * $multiplier) + $this->scaleBottom;
59
    }
60
}
61