Renderable::editable()   F
last analyzed

Complexity

Conditions 20
Paths 270

Size

Total Lines 94
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 62
c 1
b 0
f 1
dl 0
loc 94
rs 2.4583
cc 20
nc 270
nop 3

How to fix   Long Method    Complexity   

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 declare(strict_types=1);
2
3
namespace Formularium\Frontend\VeeValidate;
4
5
use Formularium\Datatype;
6
use Formularium\Extradata;
7
use Formularium\Field;
8
use Formularium\Frontend\Vue\Framework as FrameworkVue;
9
use Formularium\Frontend\Vue\VueCode;
10
use Formularium\HTMLNode;
11
use Formularium\Validator\Equals;
12
use Formularium\Validator\Filled;
13
use Formularium\Validator\In;
14
use Formularium\Validator\Max;
15
use Formularium\Validator\MaxLength;
16
use Formularium\Validator\Min;
17
use Formularium\Validator\MinLength;
18
use Formularium\Validator\NotIn;
19
use Formularium\Validator\Password;
20
use Formularium\Validator\Regex;
21
use Formularium\Validator\SameAs;
22
23
class Renderable extends \Formularium\Renderable
24
{
25
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
26
    {
27
        return $previous;
28
    }
29
30
    public function editable($value, Field $field, HTMLNode $previous): HTMLNode
31
    {
32
        $validators = $field->getValidators();
33
        
34
        // get the tag
35
        /**
36
         * @var HTMLNode|null $validationNode
37
         */
38
        $validationNode = null;
39
        $input = $previous->get('input');
40
        if (count($input)) {
41
            $validationNode = $input[0];
42
        }
43
        if (!$validationNode) {
44
            $input = $previous->get('textarea');
45
            if (count($input)) {
46
                $validationNode = $input[0];
47
            }
48
        }
49
        if (!$validationNode) {
50
            $input = $previous->get('select');
51
            if (count($input)) {
52
                $validationNode = $input[0];
53
            }
54
        }
55
        if (!$validationNode) {
56
            return $previous;
57
        }
58
59
        // add rules
60
        $newInput = clone $validationNode;
61
        $validationNode->setTag('validation-provider');
62
        $validationNode->clearAttributes()->clearContent();
63
        $validationNode->addAttribute('v-slot', '{ errors }')
64
            ->addContent([
65
                $newInput,
66
                new HTMLNode('span', [], '{{ v.errors[0] }}')
67
            ]);
68
69
        $rules = [];
70
        foreach ($validators as $validator => $data) {
71
            switch ($validator) {
72
            case Datatype::REQUIRED:
73
            case Filled::class:
74
                $rules['required'] = true;
75
                break;
76
77
            case Equals::class:
78
                $rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', ''));
0 ignored issues
show
Bug introduced by
It seems like $field->getValidatorOpti...validator, 'value', '') can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

78
                $rules['oneOf'] = implode(',', /** @scrutinizer ignore-type */ $field->getValidatorOption($validator, 'value', ''));
Loading history...
79
                break;
80
81
            case In::class:
82
                $rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', ''));
83
                break;
84
85
            case Max::class:
86
                $rules['max_value'] = $field->getValidatorOption($validator, 'value', '');
87
                break;
88
            case Min::class:
89
                $rules['min_value'] = $field->getValidatorOption($validator, 'value', '');
90
                break;
91
92
            case MaxLength::class:
93
                $rules['max'] = $field->getValidatorOption($validator, 'value', '');
94
                break;
95
            case MinLength::class:
96
                $rules['min'] = $field->getValidatorOption($validator, 'value', '');
97
                break;
98
99
            case NotIn::class:
100
                // TODO
101
                break;
102
103
            case Password::class:
104
                // TODO
105
                break;
106
107
            case Regex::class:
108
                $rules['regex'] = $field->getValidatorOption($validator, 'value', '');
109
                break;
110
111
            case SameAs::class:
112
                // TODO
113
                break;
114
            default:
115
                break;
116
            }
117
        }
118
        $validationNode->addAttribute(
119
            'rules',
120
            json_encode(array_merge($rules, $this->rules($value, $field, $newInput)))
121
        );
122
123
        return $previous;
124
    }
125
126
    /**
127
     * Other rules to add
128
     *
129
     * @param mixed $value
130
     * @param Field $field
131
     * @param HTMLNode $input
132
     * @return array
133
     */
134
    protected function rules($value, Field $field, HTMLNode $input): array
0 ignored issues
show
Unused Code introduced by
The parameter $input 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

134
    protected function rules($value, Field $field, /** @scrutinizer ignore-unused */ HTMLNode $input): array

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...
135
    {
136
        return [];
137
    }
138
}
139