Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

FileValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 8
dl 0
loc 87
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 1
C getClientOptions() 0 67 9
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\jquery\validators\client;
9
10
use Yii;
11
use yii\helpers\Html;
12
use yii\helpers\Json;
13
use yii\jquery\ValidationAsset;
14
use yii\validators\client\ClientValidator;
15
use yii\web\JsExpression;
16
17
/**
18
 * FileValidator composes client-side validation code from [[\yii\validators\FileValidator]].
19
 *
20
 * @see \yii\validators\FileValidator
21
 * @see ValidationAsset
22
 *
23
 * @author Paul Klimov <[email protected]>
24
 * @since 2.1.0
25
 */
26
class FileValidator extends ClientValidator
27
{
28
    /**
29
     * @inheritdoc
30
     */
31
    public function build($validator, $model, $attribute, $view)
32
    {
33
        ValidationAsset::register($view);
34
        $options = $this->getClientOptions($validator, $model, $attribute);
0 ignored issues
show
Compatibility introduced by
$validator of type object<yii\validators\Validator> is not a sub-type of object<yii\validators\FileValidator>. It seems like you assume a child class of the class yii\validators\Validator to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
35
        return 'yii.validation.file(attribute, messages, ' . Json::encode($options) . ');';
36
    }
37
38
    /**
39
     * Returns the client-side validation options.
40
     * @param \yii\validators\FileValidator $validator the server-side validator.
41
     * @param \yii\base\Model $model the model being validated
42
     * @param string $attribute the attribute name being validated
43
     * @return array the client-side validation options
44
     */
45
    public function getClientOptions($validator, $model, $attribute)
46
    {
47
        $label = $model->getAttributeLabel($attribute);
48
49
        $options = [];
50
        if ($validator->message !== null) {
51
            $options['message'] = $validator->formatMessage($validator->message, [
52
                'attribute' => $label,
53
            ]);
54
        }
55
56
        $options['skipOnEmpty'] = $validator->skipOnEmpty;
57
58
        if (!$validator->skipOnEmpty) {
59
            $options['uploadRequired'] = $validator->formatMessage($validator->uploadRequired, [
60
                'attribute' => $label,
61
            ]);
62
        }
63
64
        if ($validator->mimeTypes !== null) {
65
            $mimeTypes = [];
66
            foreach ($validator->mimeTypes as $mimeType) {
0 ignored issues
show
Bug introduced by
The expression $validator->mimeTypes of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
67
                $mimeTypes[] = new JsExpression(Html::escapeJsRegularExpression($validator->buildMimeTypeRegexp($mimeType)));
68
            }
69
            $options['mimeTypes'] = $mimeTypes;
70
            $options['wrongMimeType'] = $validator->formatMessage($validator->wrongMimeType, [
71
                'attribute' => $label,
72
                'mimeTypes' => implode(', ', $validator->mimeTypes),
73
            ]);
74
        }
75
76
        if ($validator->extensions !== null) {
77
            $options['extensions'] = $validator->extensions;
78
            $options['wrongExtension'] = $validator->formatMessage($validator->wrongExtension, [
79
                'attribute' => $label,
80
                'extensions' => implode(', ', $validator->extensions),
81
            ]);
82
        }
83
84
        if ($validator->minSize !== null) {
85
            $options['minSize'] = $validator->minSize;
86
            $options['tooSmall'] = $validator->formatMessage($validator->tooSmall, [
87
                'attribute' => $label,
88
                'limit' => $validator->minSize,
89
                'formattedLimit' => Yii::$app->formatter->asShortSize($validator->minSize),
90
            ]);
91
        }
92
93
        if ($validator->maxSize !== null) {
94
            $options['maxSize'] = $validator->maxSize;
95
            $options['tooBig'] = $validator->formatMessage($validator->tooBig, [
96
                'attribute' => $label,
97
                'limit' => $validator->getSizeLimit(),
98
                'formattedLimit' => Yii::$app->formatter->asShortSize($validator->getSizeLimit()),
99
            ]);
100
        }
101
102
        if ($validator->maxFiles !== null) {
103
            $options['maxFiles'] = $validator->maxFiles;
104
            $options['tooMany'] = $validator->formatMessage($validator->tooMany, [
105
                'attribute' => $label,
106
                'limit' => $validator->maxFiles,
107
            ]);
108
        }
109
110
        return $options;
111
    }
112
}