Completed
Pull Request — master (#1)
by Angel
07:01
created

SolicitudeValue::rules()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 39
nc 1
nop 0
dl 0
loc 55
ccs 21
cts 21
cp 1
crap 2
rs 9.296
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace roaresearch\yii2\formgenerator\models;
4
5
use yii\db\ActiveQuery;
6
7
/**
8
 * Model class for table `{{%formgenerator_solicitude_value}}`
9
 *
10
 * @property integer $section_id
11
 * @property integer $field_id
12
 * @property integer $solicitude_id
13
 * @property string $value
14
 *
15
 * @property SectionField $sectionField
16
 * @property Section $section
17
 * @property Field $field
18
 * @property Solicitude $solicitude
19
 */
20
class SolicitudeValue extends \roaresearch\yii2\rmdb\models\Entity
21
{
22
    /**
23
     * @var string full class name of the model used in the relation
24
     * `getSectionField()`.
25
     */
26
    protected $sectionFieldClass = SectionField::class;
27
28
    /**
29
     * @var string full class name of the model used in the relation
30
     * `getSection()`.
31
     */
32
    protected $sectionClass = Section::class;
33
34
    /**
35
     * @var string full class name of the model used in the relation
36
     * `getField()`.
37
     */
38
    protected $fieldClass = Field::class;
39
40
    /**
41
     * @var string full class name of the model used in the relation
42
     * `getSolicitude()`.
43
     */
44
    protected $solicitudeClass = Solicitude::class;
45
46
    /**
47
     * @inheritdoc
48
     */
49 10
    public static function tableName()
50
    {
51 10
        return '{{%formgenerator_solicitude_value}}';
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 6
    protected function attributeTypecast(): array
58
    {
59 6
        return parent::attributeTypecast() + [
60 6
            'section_id' => 'integer',
61
            'field_id' => 'integer',
62
            'solicitude_id' => 'integer',
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 2
    public function rules()
70
    {
71
        return [
72 2
            [['section_id', 'field_id', 'solicitude_id'], 'required'],
73
            [['section_id', 'field_id', 'solicitude_id'], 'integer'],
74
            [
75
                ['solicitude_id'],
76
                'exist',
77
                'skipOnError' => true,
78
                'targetClass' => Solicitude::class,
79
                'targetAttribute' => ['solicitude_id' => 'id'],
80
            ],
81
            [
82
                ['section_id'],
83 2
                'exist',
84
                'skipOnError' => true,
85
                'targetClass' => Section::class,
86
                'targetAttribute' => ['section_id' => 'id'],
87 2
                'when' => function () {
88 2
                    return !$this->hasErrors('solicitude_id');
89 2
                },
90 2
                'filter' => function ($query) {
91 2
                    $query->andWhere(['form_id' => $this->solicitude->form_id]);
92 2
                },
93 2
                'message' => 'Section is not associated to the form.',
94
            ],
95
            [
96
                ['field_id'],
97 2
                'exist',
98
                'skipOnError' => true,
99
                'targetClass' => SectionField::class,
100
                'targetAttribute' => [
101
                    'section_id' => 'section_id',
102
                    'field_id' => 'field_id',
103
                ],
104 2
                'when' => function () {
105 2
                    return !$this->hasErrors('section_id');
106 2
                },
107 2
                'message' => 'Field not associated to the Section.',
108
            ],
109
            [
110
                ['field_id'],
111 2
                'unique',
112
                'targetAttribute' => [
113
                    'section_id',
114
                    'field_id',
115
                    'solicitude_id',
116
                ],
117 2
                'when' => function () {
118 2
                    return !$this->hasErrors('section_id')
119 2
                        && !$this->hasErrors('solicitude_id');
120 2
                },
121 2
                'message' => 'Field already filled.',
122
            ],
123
            [['value'], 'trim'],
124
        ];
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 2
    public function afterValidate()
131
    {
132 2
        if (!$this->hasErrors()) {
133 2
            $field = $this->getField()
134 2
                ->with([
135 2
                    'dataType',
136 2
                    'rules' => function ($query) {
137 2
                        $query->modelClass = FieldRule::class;
138 2
                    },
139 2
                    'rules.properties',
140
                ])
141 2
                ->one();
142 2
            $this->populateRelation('field', $field);
143 2
            foreach (
144
                $field->buildValidators($this, 'value') as $validator
145
            ) {
146 2
                $validator->validateAttributes($this, ['value']);
147
            }
148 2
            $field->dataType->castValue($this, 'value');
0 ignored issues
show
Bug introduced by
The method castValue() does not exist on null. ( Ignorable by Annotation )

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

148
            $field->dataType->/** @scrutinizer ignore-call */ 
149
                              castValue($this, 'value');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
        }
150 2
        parent::afterValidate();
151 2
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156 5
    public function attributeLabels()
157
    {
158 5
        return array_merge([
159 5
            'section_id' => 'Section ID',
160
            'field_id' => 'Field ID',
161
            'label' => 'label',
162 5
        ], parent::attributeLabels());
163
    }
164
165
    /**
166
     * @return ActiveQuery
167
     */
168
    public function getSectionField(): ActiveQuery
169
    {
170
        return $this->hasOne(
171
            $this->sectionFieldClass,
172
            ['section_id' => 'section_id', 'field_id' => 'field_id']
173
        );
174
    }
175
176
    /**
177
     * @return ActiveQuery
178
     */
179 5
    public function getSection(): ActiveQuery
180
    {
181 5
        return $this->hasOne($this->sectionClass, ['id' => 'section_id']);
182
    }
183
184
    /**
185
     * @return ActiveQuery
186
     */
187 5
    public function getField(): ActiveQuery
188
    {
189 5
        return $this->hasOne($this->fieldClass, ['id' => 'field_id']);
190
    }
191
192
    /**
193
     * @return ActiveQuery
194
     */
195 6
    public function getSolicitude(): ActiveQuery
196
    {
197 6
        return $this->hasOne($this->solicitudeClass, ['id' => 'solicitude_id']);
198
    }
199
}
200