Passed
Push — master ( f27feb...17757a )
by Tõnis
02:14
created

VariableNameValidator::containsInvalidCharacters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 1
1
<?php
2
3
namespace dameter\abstracts\validators;
4
5
use yii\validators\StringValidator;
6
7
8
/**
9
 * Class VariableNameValidator
10
 * Validates the variable name based mainly on SPSS variable name limitations
11
 * @link https://www.ibm.com/support/knowledgecenter/en/SSLVMB_23.0.0/spss/base/syn_variables_variable_names.html
12
 * @author Tõnis Ormisson <[email protected]>
13
 */
14
class VariableNameValidator extends StringValidator
15
{
16
    public $max = 64;
17
18
    /** @var string $containsSpacesMsg A message if the value contains spaces */
19
    public $containsSpacesMsg;
20
21
    /** @var string $invalidFirstLetterMsg A message if invalid first letter */
22
    public $invalidFirstLetterMsg;
23
24
    const ALLOWED_NON_ALPHA_CHARACTERS = ['.'];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function init()
30
    {
31
        parent::init();
32
        $this->containsSpacesMsg = \Yii::t('dmabstract', "{attribute} must not contain spaces!");
33
        $this->containsSpacesMsg = \Yii::t('dmabstract', "The first character of {attribute} must be a letter!");
34
    }
35
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function validateAttribute($model, $attribute)
41
    {
42
        parent::validateAttribute($model, $attribute);
43
44
        $value = $model->{$attribute};
45
        if (!is_string($value)) {
46
            $this->addError($model, $attribute, $this->message);
47
        } else {
48
            if (strpos($model->{$attribute}, ' ') !== false) {
49
                $this->addError($model, $attribute, $this->containsSpacesMsg);
50
            }
51
52
            if (!ctype_alpha($model->{$attribute}[0])) {
53
                $this->addError($model, $attribute, $this->invalidFirstLetterMsg);
54
            }
55
56
        }
57
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function validateValue($value)
64
    {
65
66
        $validation = parent::validateValue($value);
67
68
        if (!is_null($validation)) {
69
            return $validation;
70
        }
71
        if (strpos($value, ' ') !== false) {
72
            return [$this->containsSpacesMsg, []];
73
        }
74
        if (!ctype_alpha($value[0])) {
75
            return [$this->invalidFirstLetterMsg, []];
76
        }
77
        return null;
78
    }
79
80
    private function containsInvalidCharacters($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    private function containsInvalidCharacters(/** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method containsInvalidCharacters() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
81
    {
82
        if (ctype_punct()) {
0 ignored issues
show
Bug introduced by
The call to ctype_punct() has too few arguments starting with text. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        if (/** @scrutinizer ignore-call */ ctype_punct()) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
84
        }
85
86
    }
87
88
89
}