Completed
Pull Request — master (#10)
by Gabriel
60:20
created

Address::newPersonalAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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