Passed
Push — develop ( 2115be...ca4435 )
by Andrew
04:18
created

EmbeddedModelValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateAttribute() 0 22 7
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS 3.x
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\validators;
12
13
use Craft;
14
15
use yii\base\Model;
16
use yii\validators\Validator;
17
18
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
19
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.5.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
23
class EmbeddedModelValidator extends Validator
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
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...
29
     * @inheritdoc
30
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
31
    public function validateAttribute($model, $attribute)
32
    {
33
        /** @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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
34
        $value = $model->$attribute;
35
36
        if (!empty($value) && \is_object($value) && $value instanceof Model) {
37
            /** @var Model $value */
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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
38
            if (!$value->validate()) {
39
                $errors = $value->getErrors();
40
                foreach ($errors as $attributeError => $valueErrors) {
41
                    /** @var array $valueErrors */
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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
42
                    foreach ($valueErrors as $valueError) {
43
                        $model->addError(
44
                            $attribute,
45
                            Craft::t('image-optimize', 'Object failed to validate')
46
                            .'-'.$attributeError.' - '.$valueError
47
                        );
48
                    }
49
                }
50
            }
51
        } else {
52
            $model->addError($attribute, Craft::t('image-optimize', 'Is not a Model object.'));
53
        }
54
    }
55
}
56