Passed
Push — main ( c73e00...0a00da )
by
unknown
15:13 queued 14s
created

Address::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 20
rs 9.9666
ccs 10
cts 10
cp 1
cc 1
nc 1
nop 9
crap 1

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 WMDE\Fundraising\AddressChangeContext\Domain\Model;
6
7
use WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressValidationException;
8
9
class Address {
10
11
	/**
12
	 * @var int|null
13
	 * @phpstan-ignore-next-line
14
	 */
15
	private ?int $id;
16
17
	private string $salutation;
18
19
	private string $company;
20
21
	private string $title;
22
23
	private string $firstName = '';
24
25
	private string $lastName = '';
26
27
	private string $address;
28
29
	private string $postcode;
30
31
	private string $city;
32
33
	private string $country = '';
34
35 2
	private function __construct(
36
		string $salutation,
37
		string $company,
38
		string $title,
39
		string $firstName,
40
		string $lastName,
41
		string $address,
42
		string $postcode,
43
		string $city,
44
		string $country
45
	) {
46 2
		$this->salutation = $salutation;
47 2
		$this->company = $company;
48 2
		$this->title = $title;
49 2
		$this->firstName = $firstName;
50 2
		$this->lastName = $lastName;
51 2
		$this->address = $address;
52 2
		$this->postcode = $postcode;
53 2
		$this->city = $city;
54 2
		$this->country = $country;
55
	}
56
57 8
	public static function newPersonalAddress(
58
		string $salutation,
59
		string $title,
60
		string $firstName,
61
		string $lastName,
62
		string $address,
63
		string $postcode,
64
		string $city,
65
		string $country ): self {
66 8
		self::assertNotEmpty( 'Salutation', $salutation );
67 7
		self::assertNotEmpty( 'First Name', $firstName );
68 6
		self::assertNotEmpty( 'Last Name', $lastName );
69 5
		self::assertNotEmpty( 'Address', $address );
70 4
		self::assertNotEmpty( 'Post Code', $postcode );
71 3
		self::assertNotEmpty( 'City', $city );
72 2
		self::assertNotEmpty( 'Country', $country );
73
74 1
		return new self( $salutation, '', $title, $firstName, $lastName, $address, $postcode, $city, $country );
75
	}
76
77 6
	public static function newCompanyAddress(
78
		string $company,
79
		string $address,
80
		string $postcode,
81
		string $city,
82
		string $country ): self {
83 6
		self::assertNotEmpty( 'Company', $company );
84 5
		self::assertNotEmpty( 'Address', $address );
85 4
		self::assertNotEmpty( 'Post Code', $postcode );
86 3
		self::assertNotEmpty( 'City', $city );
87 2
		self::assertNotEmpty( 'Country', $country );
88 1
		return new self( '', $company, '', '', '', $address, $postcode, $city, $country );
89
	}
90
91 14
	private static function assertNotEmpty( string $field, string $value ): void {
92 14
		if ( $value === '' ) {
93 12
			throw new ChangeAddressValidationException( $field );
94
		}
95
	}
96
97 1
	public function getSalutation(): string {
98 1
		return $this->salutation;
99
	}
100
101 1
	public function getCompany(): string {
102 1
		return $this->company;
103
	}
104
105 1
	public function getTitle(): string {
106 1
		return $this->title;
107
	}
108
109 1
	public function getFirstName(): string {
110 1
		return $this->firstName;
111
	}
112
113 1
	public function getLastName(): string {
114 1
		return $this->lastName;
115
	}
116
117 2
	public function getAddress(): string {
118 2
		return $this->address;
119
	}
120
121 2
	public function getPostcode(): string {
122 2
		return $this->postcode;
123
	}
124
125 2
	public function getCity(): string {
126 2
		return $this->city;
127
	}
128
129 2
	public function getCountry(): string {
130 2
		return $this->country;
131
	}
132
133
	public function getId(): ?int {
134
		return $this->id;
135
	}
136
137
}
138