Passed
Push — develop ( e3ad9f...5a99f0 )
by Andrew
11:10
created

TwigExpressionValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 12 4
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\validators;
13
14
use nystudio107\seomatic\helpers\PluginTemplate;
15
16
use Craft;
17
18
use yii\base\Model;
19
use yii\validators\Validator;
20
21
/**
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
23
 * @package   Seomatic
24
 * @since     3.4.0
25
 */
26
class TwigExpressionValidator extends Validator
27
{
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $model should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
32
     * @inheritdoc
33
     */
34
    public function validateAttribute($model, $attribute)
35
    {
36
        /** @var Model $model */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
37
        $value = $model->$attribute;
38
39
        if (!empty($value) && \is_string($value)) {
40
            $error = PluginTemplate::isStringTemplateValid($value, []);
41
            if (!empty($error)) {
42
                $model->addError($attribute, $error);
43
            }
44
        } else {
45
            $model->addError($attribute, Craft::t('seomatic', 'Is not a string.'));
46
        }
47
    }
48
}
49