DefaultAdrUnit::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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

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 Inspirum\Balikobot\Model\AdrUnit;
6
7
use Inspirum\Arrayable\BaseModel;
8
9
/**
10
 * @extends \Inspirum\Arrayable\BaseModel<string,mixed>
11
 */
12
final class DefaultAdrUnit extends BaseModel implements AdrUnit
13
{
14 4
    public function __construct(
15
        private readonly string $carrier,
16
        private readonly string $id,
17
        private readonly string $code,
18
        private readonly string $name,
19
        private readonly string $class,
20
        private readonly ?string $packaging,
21
        private readonly ?string $tunnelCode,
22
        private readonly string $transportCategory,
23
    ) {
24 4
    }
25
26 1
    public function getCarrier(): string
27
    {
28 1
        return $this->carrier;
29
    }
30
31 1
    public function getId(): string
32
    {
33 1
        return $this->id;
34
    }
35
36 1
    public function getCode(): string
37
    {
38 1
        return $this->code;
39
    }
40
41 1
    public function getName(): string
42
    {
43 1
        return $this->name;
44
    }
45
46 1
    public function getClass(): string
47
    {
48 1
        return $this->class;
49
    }
50
51 1
    public function getPackaging(): ?string
52
    {
53 1
        return $this->packaging;
54
    }
55
56 1
    public function getTunnelCode(): ?string
57
    {
58 1
        return $this->tunnelCode;
59
    }
60
61 1
    public function getTransportCategory(): string
62
    {
63 1
        return $this->transportCategory;
64
    }
65
66
    /** @inheritDoc */
67 2
    public function __toArray(): array
68
    {
69 2
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('id' => $th...his->transportCategory) returns the type array<string,null|string> which is incompatible with the return type mandated by Inspirum\Arrayable\BaseModel::__toArray() of Inspirum\Arrayable\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
70 2
            'id' => $this->id,
71 2
            'code' => $this->code,
72 2
            'name' => $this->name,
73 2
            'class' => $this->class,
74 2
            'packaging' => $this->packaging,
75 2
            'tunnelCode' => $this->tunnelCode,
76 2
            'transportCategory' => $this->transportCategory,
77 2
        ];
78
    }
79
}
80