Completed
Push — develop ( 3b8b48...76311f )
by Nate
27:41 queued 12:43
created

AbstractMetric   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
calculateScore() 0 1 ?
A displayName() 0 7 1
A getVersion() 0 4 1
A getWeight() 0 4 1
A getScore() 0 8 2
A toConfig() 0 9 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/scorecard/license
6
 * @link       https://www.flipboxfactory.com/software/scorecard/
7
 */
8
9
namespace flipbox\craft\scorecard\metrics;
10
11
use craft\helpers\StringHelper;
12
use yii\base\BaseObject;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
abstract class AbstractMetric extends BaseObject implements MetricInterface
19
{
20
    /**
21
     * @var float
22
     */
23
    public $weight = 1;
24
25
    /**
26
     * @var string
27
     */
28
    public $version = '1.0.0';
29
30
    /**
31
     * @var float
32
     */
33
    public $score;
34
35
    /**
36
     * @return float
37
     */
38
    abstract protected function calculateScore(): float;
39
40
    /**
41
     * @inheritdoc
42
     * @throws \ReflectionException
43
     */
44
    public static function displayName(): string
45
    {
46
        return StringHelper::titleize(
47
            (new \ReflectionClass(static::class))
48
                ->getShortName()
49
        );
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getVersion(): string
56
    {
57
        return $this->version;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function getWeight(): float
64
    {
65
        return $this->weight;
66
    }
67
68
    /**
69
     * @return float
70
     */
71
    public function getScore(): float
72
    {
73
        if ($this->score === null) {
74
            $this->score = $this->calculateScore();
75
        }
76
77
        return $this->score * $this->getWeight();
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function toConfig(): array
84
    {
85
        return [
86
            'class' => static::class,
87
            'weight' => $this->getWeight(),
88
            'version' => $this->getVersion(),
89
            'score' => $this->getScore()
90
        ];
91
    }
92
}
93