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

RangeValidator::getClientOptions()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 21
cp 0
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 3
crap 20
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\jquery\ValidationAsset;
11
use yii\validators\client\ClientValidator;
12
13
/**
14
 * RangeValidator composes client-side validation code from [[\yii\validators\RangeValidator]].
15
 *
16
 * @see \yii\validators\RangeValidator
17
 * @see ValidationAsset
18
 *
19
 * @author Paul Klimov <[email protected]>
20
 * @since 2.1.0
21
 */
22
class RangeValidator extends ClientValidator
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public function build($validator, $model, $attribute, $view)
28
    {
29
        /* @var $validator \yii\validators\RangeValidator */
30
        if ($validator->range instanceof \Closure) {
31
            $validator->range = call_user_func($validator->range, $model, $attribute);
32
        }
33
        ValidationAsset::register($view);
34
        $options = $validator->getClientOptions($model, $attribute);
0 ignored issues
show
Documentation Bug introduced by
The method getClientOptions does not exist on object<yii\validators\RangeValidator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35
        return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
36
    }
37
38
    /**
39
     * Returns the client-side validation options.
40
     * @param \yii\validators\RangeValidator $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
        $range = [];
48
        foreach ($validator->range as $value) {
0 ignored issues
show
Bug introduced by
The expression $validator->range of type array|object<Traversable>|object<Closure> 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...
49
            $range[] = (string) $value;
50
        }
51
        $options = [
52
            'range' => $range,
53
            'not' => $validator->not,
54
            'message' => $validator->formatMessage($validator->message, [
55
                'attribute' => $model->getAttributeLabel($attribute),
56
            ]),
57
        ];
58
        if ($validator->skipOnEmpty) {
59
            $options['skipOnEmpty'] = 1;
60
        }
61
        if ($validator->allowArray) {
62
            $options['allowArray'] = 1;
63
        }
64
65
        return $options;
66
    }
67
}