DefaultAccount::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
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 11
dl 0
loc 13
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\Account;
6
7
use Inspirum\Arrayable\BaseModel;
8
use Inspirum\Balikobot\Model\Carrier\CarrierCollection;
9
10
/**
11
 * @extends \Inspirum\Arrayable\BaseModel<string,mixed>
12
 */
13
final class DefaultAccount extends BaseModel implements Account
14
{
15 3
    public function __construct(
16
        private readonly string $name,
17
        private readonly string $contactPerson,
18
        private readonly string $email,
19
        private readonly string $phone,
20
        private readonly string $url,
21
        private readonly string $street,
22
        private readonly string $city,
23
        private readonly string $zip,
24
        private readonly string $country,
25
        private readonly bool $live,
26
        private readonly CarrierCollection $carriers,
27
    ) {
28 3
    }
29
30 1
    public function getName(): string
31
    {
32 1
        return $this->name;
33
    }
34
35 1
    public function getContactPerson(): string
36
    {
37 1
        return $this->contactPerson;
38
    }
39
40 1
    public function getEmail(): string
41
    {
42 1
        return $this->email;
43
    }
44
45 1
    public function getPhone(): string
46
    {
47 1
        return $this->phone;
48
    }
49
50 1
    public function getUrl(): string
51
    {
52 1
        return $this->url;
53
    }
54
55 1
    public function getStreet(): string
56
    {
57 1
        return $this->street;
58
    }
59
60 1
    public function getCity(): string
61
    {
62 1
        return $this->city;
63
    }
64
65 1
    public function getZipCode(): string
66
    {
67 1
        return $this->zip;
68
    }
69
70 1
    public function getCountry(): string
71
    {
72 1
        return $this->country;
73
    }
74
75 2
    public function isLive(): bool
76
    {
77 2
        return $this->live;
78
    }
79
80 1
    public function getCarriers(): CarrierCollection
81
    {
82 1
        return $this->carriers;
83
    }
84
85
    /** @inheritDoc */
86 1
    public function __toArray(): array
87
    {
88 1
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('name' => $...>carriers->__toArray()) returns the type array<string,Inspirum\Ar...Value[]|boolean|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...
89 1
            'name' => $this->name,
90 1
            'contactPerson' => $this->contactPerson,
91 1
            'email' => $this->email,
92 1
            'phone' => $this->phone,
93 1
            'url' => $this->url,
94 1
            'street' => $this->street,
95 1
            'city' => $this->city,
96 1
            'zip' => $this->zip,
97 1
            'country' => $this->country,
98 1
            'live' => $this->live,
99 1
            'carriers' => $this->carriers->__toArray(),
100
101 1
        ];
102
    }
103
}
104