Completed
Pull Request — master (#1)
by Tim
65:22
created

Address::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 21
rs 9.9332
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 WMDE\Fundraising\AddressChange\Domain\Model;
6
7
use WMDE\Fundraising\AddressChange\UseCases\ChangeAddress\ChangeAddressValidationException;
8
9
class Address {
10
11
	private const TYPE_PERSONAL = 'personal';
12
13
	private const TYPE_COMPANY = 'company';
14
15
	private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
16
17
	private $salutation;
18
19
	private $company;
20
21
	private $title;
22
23
	private $firstName = '';
24
25
	private $lastName = '';
26
27
	private $address;
28
29
	private $postcode;
30
31
	private $city;
32
33
	private $country = '';
34
35
	private $addressType;
36
37
	private function __construct(
38
		string $salutation,
39
		string $company,
40
		string $title,
41
		string $firstName,
42
		string $lastName,
43
		string $address,
44
		string $postcode,
45
		string $city,
46
		string $country,
47
		string $addressType ) {
48
		$this->salutation = $salutation;
49
		$this->company = $company;
50
		$this->title = $title;
51
		$this->firstName = $firstName;
52
		$this->lastName = $lastName;
53
		$this->address = $address;
54
		$this->postcode = $postcode;
55
		$this->city = $city;
56
		$this->country = $country;
57
		$this->addressType = $addressType;
58
	}
59
60
	static public function newPersonalAddress(
61
		string $salutation,
62
		string $title,
63
		string $firstName,
64
		string $lastName,
65
		string $address,
66
		string $postcode,
67
		string $city,
68
		string $country ): self {
69
		self::assertNotEmpty( 'Salutation', $salutation );
70
		self::assertNotEmpty( 'First Name', $firstName );
71
		self::assertNotEmpty( 'Last Name', $lastName );
72
		self::assertNotEmpty( 'Address', $address );
73
		self::assertNotEmpty( 'Post Code', $postcode );
74
		self::assertNotEmpty( 'City', $city );
75
		self::assertNotEmpty( 'Country', $country );
76
77
		return new self( $salutation, '', $title, $firstName, $lastName, $address, $postcode, $city, $country, self::TYPE_PERSONAL );
78
	}
79
80
	static public function newCompanyAddress(
81
		string $company,
82
		string $address,
83
		string $postcode,
84
		string $city,
85
		string $country ): self {
86
		self::assertNotEmpty( 'Company', $company );
87
		self::assertNotEmpty( 'Address', $address );
88
		self::assertNotEmpty( 'Post Code', $postcode );
89
		self::assertNotEmpty( 'City', $city );
90
		self::assertNotEmpty( 'Country', $country );
91
		return new self( '', $company, '', '', '', $address, $postcode, $city, $country, self::TYPE_COMPANY );
92
	}
93
94
	static private function assertNotEmpty( string $field, string $value ): void {
95
		if ( $value === '' ) {
96
			throw new ChangeAddressValidationException( $field );
97
		}
98
	}
99
100
	public function isPersonalAddress(): bool {
101
		return $this->addressType === self::TYPE_PERSONAL;
102
	}
103
104
	public function isCompanyAddress(): bool {
105
		return $this->addressType === self::TYPE_COMPANY;
106
	}
107
108
	public function getSalutation(): string {
109
		return $this->salutation;
110
	}
111
112
	public function getCompany(): string {
113
		return $this->company;
114
	}
115
116
	public function getTitle(): string {
117
		return $this->title;
118
	}
119
120
	public function getFirstName(): string {
121
		return $this->firstName;
122
	}
123
124
	public function getLastName(): string {
125
		return $this->lastName;
126
	}
127
128
	public function getAddress(): string {
129
		return $this->address;
130
	}
131
132
	public function getPostcode(): string {
133
		return $this->postcode;
134
	}
135
136
	public function getCity(): string {
137
		return $this->city;
138
	}
139
140
	public function getCountry(): string {
141
		return $this->country;
142
	}
143
}
144