ElementMetricValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 18 4
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\validators;
10
11
use Craft;
12
use flipbox\craft\scorecard\metrics\MetricInterface;
13
use yii\validators\Validator;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class ElementMetricValidator extends Validator
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function validateAttribute($model, $attribute)
25
    {
26
        $class = $model->$attribute;
27
28
        // Handles are always required, so if it's blank, the required validator will catch this.
29
        if ($class) {
30
            if (!$class instanceof MetricInterface &&
31
                !is_subclass_of($class, MetricInterface::class)
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \flipbox\craft\scorecard...\MetricInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
32
            ) {
33
                $message = Craft::t(
34
                    'scorecard',
35
                    '“{class}” is a not a valid metric.',
36
                    ['class' => $class]
37
                );
38
                $this->addError($model, $attribute, $message);
39
            }
40
        }
41
    }
42
}
43