PDO::fillDataFromRow()   B
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 7.6666
cc 10
nc 11
nop 1
crap 10

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 kalanis\nested_tree\Sources\PDO;
4
5
use kalanis\nested_tree\Sources\SourceInterface;
6
use kalanis\nested_tree\Support;
7
use PDO as base;
8
9
abstract class PDO implements SourceInterface
10
{
11
    use Support\ColumnsTrait;
0 ignored issues
show
introduced by
The trait kalanis\nested_tree\Support\ColumnsTrait requires some properties which are not provided by kalanis\nested_tree\Sources\PDO\PDO: $parentIdColumnName, $positionColumnName, $levelColumnName, $rightColumnName, $leftColumnName, $idColumnName
Loading history...
12
13 65
    public function __construct(
14
        protected readonly base $pdo,
15
        protected readonly Support\Node $nodeBase,
16
        protected readonly Support\TableSettings $settings,
17
    ) {
18 65
    }
19
20
    /**
21
     * @param array<array<mixed>> $rows
22
     * @return array<int, Support\Node>
23
     */
24 62
    protected function fromDbRows(array $rows) : array
25
    {
26 62
        $result = [];
27 62
        foreach ($rows as &$row) {
28 62
            $data = $this->fillDataFromRow($row);
29 62
            $result[$data->id] = $data;
30
        }
31
32 62
        return $result;
33
    }
34
35
    /**
36
     * @param array<mixed> $row
37
     * @return Support\Node
38
     */
39 62
    protected function fillDataFromRow(array $row) : Support\Node
40
    {
41 62
        $data = clone $this->nodeBase;
42 62
        foreach ($row as $k => $v) {
43 62
            if ($this->settings->idColumnName === $k) {
44 62
                $data->id = max(0, intval($v));
45 62
            } elseif ($this->settings->parentIdColumnName === $k) {
46 62
                $data->parentId = is_null($v) && $this->settings->rootIsNull ? null : max(0, intval($v));
47 62
            } elseif ($this->settings->levelColumnName === $k) {
48 62
                $data->level = max(0, intval($v));
49 62
            } elseif ($this->settings->leftColumnName === $k) {
50 62
                $data->left = max(0, intval($v));
51 62
            } elseif ($this->settings->rightColumnName === $k) {
52 62
                $data->right = max(0, intval($v));
53 62
            } elseif ($this->settings->positionColumnName === $k) {
54 62
                $data->position = max(0, intval($v));
55
            } else {
56 62
                $data->{$k} = strval($v);
57
            }
58
        }
59
60 62
        return $data;
61
    }
62
}
63