Test Failed
Pull Request — master (#1)
by Angel
04:13
created

SolicitudeValue::rules()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

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

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