Passed
Push — 3.x ( 885785...4e5cb3 )
by Aleksei
01:36
created

Column::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 8
dl 0
loc 16
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0

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 Cycle\Annotated\Annotation;
6
7
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
8
use Doctrine\Common\Annotations\Annotation\Target;
9
use JetBrains\PhpStorm\ExpectedValues;
10
11
/**
12
 * @Annotation
13
 * @NamedArgumentConstructor
14
 * @Target({"PROPERTY", "ANNOTATION", "CLASS"})
15
 */
16
#[
17
    \Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE),
18
    NamedArgumentConstructor
19
]
20
final class Column
21
{
22
    private bool $hasDefault = false;
23
24
    /**
25
     * @param non-empty-string $type Column type. {@see \Cycle\Database\Schema\AbstractColumn::$mapping}
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
26
     * @param non-empty-string|null $name Column name. Defaults to the property name.
27
     * @param non-empty-string|null $property Property that belongs to column. For virtual columns.
28
     * @param bool $primary Explicitly set column as a primary key.
29
     * @param bool $nullable Set column as nullable.
30
     * @param mixed|null $default Default column value.
31
     * @param non-empty-string|null $typecast Column typecast function.
32
     *        Defaults to one of (int|float|bool|datetime) based on column type
33
     * @param bool $castDefault
34
     */
35 468
    public function __construct(
36
        #[ExpectedValues(values: ['primary', 'bigPrimary', 'enum', 'boolean', 'integer', 'tinyInteger', 'bigInteger',
37
            'string', 'text', 'tinyText', 'longText', 'double', 'float', 'decimal', 'datetime', 'date', 'time',
38
            'timestamp', 'binary', 'tinyBinary', 'longBinary', 'json',
39
        ])]
40
        private string $type,
41
        private ?string $name = null,
42
        private ?string $property = null,
43
        private bool $primary = false,
44
        private bool $nullable = false,
45
        private mixed $default = null,
46
        private mixed $typecast = null,
47
        private bool $castDefault = false,
48
    ) {
49 468
        if ($default !== null) {
50 248
            $this->hasDefault = true;
51
        }
52 468
    }
53
54 468
    public function getType(): ?string
55
    {
56 468
        return $this->type;
57
    }
58
59 468
    public function getColumn(): ?string
60
    {
61 468
        return $this->name;
62
    }
63
64 236
    public function getProperty(): ?string
65
    {
66 236
        return $this->property;
67
    }
68
69 468
    public function isNullable(): bool
70
    {
71 468
        return $this->nullable;
72
    }
73
74 468
    public function isPrimary(): bool
75
    {
76 468
        return $this->primary;
77
    }
78
79 468
    public function hasDefault(): bool
80
    {
81 468
        return $this->hasDefault;
82
    }
83
84 248
    public function getDefault(): mixed
85
    {
86 248
        return $this->default;
87
    }
88
89 468
    public function castDefault(): bool
90
    {
91 468
        return $this->castDefault;
92
    }
93
94 468
    public function getTypecast(): mixed
95
    {
96 468
        return $this->typecast;
97
    }
98
}
99