ColumnDTO::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace MySQLReplication\Event\RowEvent;
6
7
use MySQLReplication\BinaryDataReader\BinaryDataReader;
8
use MySQLReplication\Definitions\ConstFieldType;
9
use MySQLReplication\Repository\FieldDTO;
10
11
class ColumnDTO
12
{
13
    private $fieldDTO;
14
    private $maxLength;
15
    private $size;
16
    private $fsp;
17
    private $lengthSize;
18
    private $precision;
19
    private $decimals;
20
    private $bits;
21
    private $bytes;
22
    private $type;
23
24 53
    public function __construct(
25
        FieldDTO $fieldDTO,
26
        int $type,
27
        int $maxLength,
28
        int $size,
29
        int $fsp,
30
        int $lengthSize,
31
        int $precision,
32
        int $decimals,
33
        int $bits,
34
        int $bytes
35
    ) {
36 53
        $this->fieldDTO = $fieldDTO;
37 53
        $this->type = $type;
38 53
        $this->maxLength = $maxLength;
39 53
        $this->size = $size;
40 53
        $this->fsp = $fsp;
41 53
        $this->lengthSize = $lengthSize;
42 53
        $this->precision = $precision;
43 53
        $this->decimals = $decimals;
44 53
        $this->bits = $bits;
45 53
        $this->bytes = $bytes;
46 53
    }
47
48 53
    public static function make(
49
        int $columnType,
50
        FieldDTO $fieldDTO,
51
        BinaryDataReader $binaryDataReader
52
    ): self {
53 53
        $maxLength = 0;
54 53
        $size = 0;
55 53
        $fsp = 0;
56 53
        $lengthSize = 0;
57 53
        $precision = 0;
58 53
        $decimals = 0;
59 53
        $bits = 0;
60 53
        $bytes = 0;
61
62 53
        if ($columnType === ConstFieldType::VARCHAR) {
63 6
            $maxLength = $binaryDataReader->readUInt16();
64 51
        } elseif ($columnType === ConstFieldType::DOUBLE) {
65 1
            $size = $binaryDataReader->readUInt8();
66 50
        } elseif ($columnType === ConstFieldType::FLOAT) {
67 1
            $size = $binaryDataReader->readUInt8();
68 49
        } elseif ($columnType === ConstFieldType::TIMESTAMP2) {
69 2
            $fsp = $binaryDataReader->readUInt8();
70 47
        } elseif ($columnType === ConstFieldType::DATETIME2) {
71 4
            $fsp = $binaryDataReader->readUInt8();
72 44
        } elseif ($columnType === ConstFieldType::TIME2) {
73 2
            $fsp = $binaryDataReader->readUInt8();
74 43
        } elseif ($columnType === ConstFieldType::VAR_STRING || $columnType === ConstFieldType::STRING) {
75 5
            $metadata = ($binaryDataReader->readUInt8() << 8) + $binaryDataReader->readUInt8();
76 5
            $realType = $metadata >> 8;
77 5
            if ($realType === ConstFieldType::SET || $realType === ConstFieldType::ENUM) {
78 2
                $columnType = $realType;
79 2
                $size = $metadata & 0x00ff;
80
            } else {
81 5
                $maxLength = ((($metadata >> 4) & 0x300) ^ 0x300) + ($metadata & 0x00ff);
82
            }
83 38
        } elseif ($columnType === ConstFieldType::BLOB || $columnType === ConstFieldType::IGNORE) {
84 5
            $lengthSize = $binaryDataReader->readUInt8();
85 34
        } elseif ($columnType === ConstFieldType::GEOMETRY) {
86 1
            $lengthSize = $binaryDataReader->readUInt8();
87 33
        } elseif ($columnType === ConstFieldType::JSON) {
88
            $lengthSize = $binaryDataReader->readUInt8();
89 33
        } elseif ($columnType === ConstFieldType::NEWDECIMAL) {
90 10
            $precision = $binaryDataReader->readUInt8();
91 10
            $decimals = $binaryDataReader->readUInt8();
92 23
        } elseif ($columnType === ConstFieldType::BIT) {
93 1
            $bits = $binaryDataReader->readUInt8();
94 1
            $bytes = $binaryDataReader->readUInt8();
95
96 1
            $bits = ($bytes * 8) + $bits;
97 1
            $bytes = (int)(($bits + 7) / 8);
98
        }
99
100 53
        return new self(
101 53
            $fieldDTO,
102
            $columnType,
103
            $maxLength,
104
            $size,
105
            $fsp,
106
            $lengthSize,
107
            $precision,
108
            $decimals,
109
            $bits,
110
            $bytes
111
        );
112
    }
113
114
    public function getFieldDTO(): FieldDTO
115
    {
116
        return $this->fieldDTO;
117
    }
118
119 8
    public function getMaxLength(): int
120
    {
121 8
        return $this->maxLength;
122
    }
123
124 2
    public function getSize(): int
125
    {
126 2
        return $this->size;
127
    }
128
129 8
    public function getFsp(): int
130
    {
131 8
        return $this->fsp;
132
    }
133
134 6
    public function getLengthSize(): int
135
    {
136 6
        return $this->lengthSize;
137
    }
138
139 10
    public function getPrecision(): int
140
    {
141 10
        return $this->precision;
142
    }
143
144 10
    public function getDecimals(): int
145
    {
146 10
        return $this->decimals;
147
    }
148
149 1
    public function getBits(): int
150
    {
151 1
        return $this->bits;
152
    }
153
154 1
    public function getBytes(): int
155
    {
156 1
        return $this->bytes;
157
    }
158
159 52
    public function getType(): int
160
    {
161 52
        return $this->type;
162
    }
163
164 52
    public function getName(): string
165
    {
166 52
        return $this->fieldDTO->getColumnName();
167
    }
168
169 1
    public function getEnumValues(): array
170
    {
171 1
        if ($this->type === ConstFieldType::ENUM) {
172 1
            return explode(',', str_replace(['enum(', ')', '\''], '', $this->fieldDTO->getColumnType()));
173
        }
174
175
        return [];
176
    }
177
178 1
    public function getSetValues(): array
179
    {
180 1
        if ($this->type === ConstFieldType::SET) {
181 1
            return explode(',', str_replace(['set(', ')', '\''], '', $this->fieldDTO->getColumnType()));
182
        }
183
184
        return [];
185
    }
186
187 18
    public function isUnsigned(): bool
188
    {
189 18
        return !(stripos($this->fieldDTO->getColumnType(), 'unsigned') === false);
190
    }
191
192
    public function isPrimary(): bool
193
    {
194
        return $this->fieldDTO->getColumnKey() === 'PRI';
195
    }
196
}