Passed
Push — main ( 20361b...6d1b9c )
by Thierry
06:44 queued 04:24
created

DataRowReader::getInputValue()   C

Complexity

Conditions 16
Paths 29

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 27
nc 29
nop 3
dl 0
loc 49
rs 5.5666
c 1
b 0
f 0

How to fix   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
2
3
namespace Lagdo\DbAdmin\Db\Page\Dml;
4
5
use Lagdo\DbAdmin\Db\Page\AppPage;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
8
use Lagdo\DbAdmin\Driver\Utils\Utils;
9
10
use function count;
11
use function implode;
12
use function is_array;
13
use function is_string;
14
use function json_decode;
15
use function preg_match;
16
use function substr;
17
18
/**
19
 * Reads data from the user inputs for data row insert and update.
20
 */
21
class DataRowReader
22
{
23
    /**
24
     * The constructor
25
     *
26
     * @param AppPage $page
27
     * @param DriverInterface $driver
28
     * @param Utils $utils
29
     */
30
    public function __construct(private AppPage $page,
31
        private DriverInterface $driver, private Utils $utils)
32
    {}
33
34
    /**
35
     * Get the user input values for data save on insert and update
36
     * Function process_input() in html.inc.php.
37
     *
38
     * @param TableFieldEntity $field
39
     * @param array $values
40
     * @param array $enumValues
41
     *
42
     * @return mixed
43
     */
44
    private function getInputValue(TableFieldEntity $field, array $values, array $enumValues): mixed
45
    {
46
        if ($field->isDisabled()) {
0 ignored issues
show
Bug introduced by
The method isDisabled() does not exist on Lagdo\DbAdmin\Driver\Entity\TableFieldEntity. ( Ignorable by Annotation )

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

46
        if ($field->/** @scrutinizer ignore-call */ isDisabled()) {

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...
47
            return false;
48
        }
49
50
        $fieldId = $this->driver->bracketEscape($field->name);
51
        $value = $values['fields'][$fieldId];
52
        if ($field->type === "enum" || count($enumValues) > 0) {
53
            $value = $value[0];
54
            if ($value === "orig") {
55
                return false;
56
            }
57
            if ($value === "null") {
58
                return "NULL";
59
            }
60
61
            $value = substr($value, 4); // 4 - strlen("val-")
62
        }
63
64
        if ($field->autoIncrement && $value === '') {
65
            return null;
66
        }
67
68
        // The function is not provided for auto-incremented fields or enums.
69
        $function = $values['function'][$fieldId] ?? '';
70
        if ($function === 'orig') {
71
            return preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate) ?
72
                $this->driver->escapeId($field->name) : false;
73
        }
74
75
        if ($field->type === 'set') {
76
            $value = implode(',', (array)$value);
77
        }
78
79
        if ($function === 'json') {
80
            $function = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $function is dead and can be removed.
Loading history...
81
            $value = json_decode($value, true);
82
            //! report errors
83
            return !is_array($value) ? false : $value;
84
        }
85
86
        if ($this->utils->isBlob($field) && $this->utils->iniBool('file_uploads')) {
0 ignored issues
show
Bug introduced by
The method iniBool() does not exist on Lagdo\DbAdmin\Driver\Utils\Utils. ( Ignorable by Annotation )

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

86
        if ($this->utils->isBlob($field) && $this->utils->/** @scrutinizer ignore-call */ iniBool('file_uploads')) {

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...
Bug introduced by
The method isBlob() does not exist on Lagdo\DbAdmin\Driver\Utils\Utils. ( Ignorable by Annotation )

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

86
        if ($this->utils->/** @scrutinizer ignore-call */ isBlob($field) && $this->utils->iniBool('file_uploads')) {

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...
87
            $file = $this->page->getFileContents("fields-$fieldId");
88
            //! report errors
89
            return !is_string($file) ? false : $this->driver->quoteBinary($file);
90
        }
91
92
        return $this->page->getUnconvertedFieldValue($field, $value, $function);
93
    }
94
95
    /**
96
     * @param array<TableFieldEntity> $fields The table fields
97
     * @param array $inputs The user form inputs
98
     *
99
     * @return array
100
     */
101
    public function getInputValues(array $fields, array $inputs): array
102
    {
103
        $userTypes = $this->driver->userTypes(true);
104
        // From edit.inc.php
105
        $values = [];
106
        foreach ($fields as $name => $field) {
107
            $userType = $userTypes[$field->type] ?? null;
108
            $enumValues = !$userType ? [] : $userType->enums;
109
            $value = $this->getInputValue($field, $inputs, $enumValues);
110
            if ($value !== false && $value !== null) {
111
                $values[$this->driver->escapeId($name)] = $value;
112
            }
113
        }
114
115
        return $values;
116
    }
117
}
118