Completed
Branch BUG-9871-email-validation (e62b1a)
by
unknown
350:41 queued 333:27
created

GenericAddress::city()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace EventEspresso\core\domain\entities;
3
4
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
5
	exit( 'No direct script access allowed' );
6
}
7
8
9
10
/**
11
 * Class GenericAddress
12
 *
13
 * @package       Event Espresso
14
 * @subpackage    core\entities\
15
 * @author        Brent Christensen
16
 * @since         4.8
17
 */
18
class GenericAddress implements \EEI_Address {
19
20
	private $_address     = '';
21
22
	private $_address2    = '';
23
24
	private $_city        = '';
25
26
	private $_state_ID    = '';
27
28
	private $_state_obj   = '';
29
30
	private $_zip         = '';
31
32
	private $_country_ID  = '';
33
34
	private $_country_obj = '';
35
36
37
38
	/**
39
	 * @param string              $address
40
	 * @param string              $address2
41
	 * @param string              $city
42
	 * @param \EE_State | string   $state
43
	 * @param string              $zip
44
	 * @param \EE_Country | string $country
45
	 * @return GenericAddress
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
	 */
47
	public function __construct( $address, $address2, $city, $state, $zip, $country ) {
48
		$this->_address = $address;
49
		$this->_address2 = $address2;
50
		$this->_city = $city;
51
		if ( $state instanceof \EE_State ) {
52
			$this->_state_obj = $state;
0 ignored issues
show
Documentation Bug introduced by
It seems like $state of type object<EE_State> is incompatible with the declared type string of property $_state_obj.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
		} else {
54
			$this->_state_ID = $state;
55
			$this->_state_obj = $this->_get_state_obj();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_get_state_obj() of type object<EE_State> is incompatible with the declared type string of property $_state_obj.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
		}
57
		$this->_zip = $zip;
58
		if ( $country instanceof \EE_Country ) {
59
			$this->_country_obj = $country;
0 ignored issues
show
Documentation Bug introduced by
It seems like $country of type object<EE_Country> is incompatible with the declared type string of property $_country_obj.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
		} else {
61
			$this->_country_ID = $country;
62
			$this->_country_obj = $this->_get_country_obj();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_get_country_obj() of type object<EE_Country> is incompatible with the declared type string of property $_country_obj.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
		}
64
	}
65
66
67
68
	/**
69
	 * @return string
70
	 */
71
	public function address() {
72
		return $this->_address;
73
	}
74
75
76
77
	/**
78
	 * @return string
79
	 */
80
	public function address2() {
81
		return $this->_address2;
82
	}
83
84
85
86
	/**
87
	 * @return string
88
	 */
89
	public function city() {
90
		return $this->_city;
91
	}
92
93
94
95
	/**
96
	 * @return \EE_State
97
	 */
98
	private function _get_state_obj() {
99
		return $this->_state_obj instanceof \EE_State
100
			? $this->_state_obj
101
			: \EE_Registry::instance()->load_model( 'State' )->get_one_by_ID( $this->_state_ID );
102
	}
103
104
105
106
	/**
107
	 * @return string
108
	 */
109
	public function state_ID() {
110
		return $this->_state_ID;
111
	}
112
113
114
115
	/**
116
	 * @return string
117
	 */
118
	public function state_abbrev() {
119
		return $this->state_obj() instanceof \EE_State
120
			? $this->state_obj()->abbrev()
121
			: '';
122
	}
123
124
125
126
	/**
127
	 * @return string
128
	 */
129
	public function state_name() {
130
		return $this->state_obj() instanceof \EE_State
131
			? $this->state_obj()->name()
132
			: '';
133
	}
134
135
136
137
	/**
138
	 * @return \EE_State
139
	 */
140
	public function state_obj() {
141
		return $this->_state_obj;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_state_obj; (string) is incompatible with the return type declared by the interface EEI_Address::state_obj of type EE_State.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
142
	}
143
144
145
146
	/**
147
	 * @return string
148
	 */
149
	public function state() {
150
		if ( apply_filters( 'FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj() ) ) {
151
			return $this->state_obj()->abbrev();
0 ignored issues
show
Bug introduced by
The method abbrev cannot be called on $this->state_obj() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
152
		} else {
153
			return $this->state_name();
154
		}
155
	}
156
157
158
159
	/**
160
	 * @return \EE_Country
161
	 */
162
	private function _get_country_obj() {
163
		return $this->_country_obj instanceof \EE_Country
164
			? $this->_country_obj
165
			: \EE_Registry::instance()->load_model( 'Country' )->get_one_by_ID( $this->_country_ID );
166
	}
167
168
169
170
	/**
171
	 * @return string
172
	 */
173
	public function country_ID() {
174
		return $this->_country_ID;
175
	}
176
177
178
179
	/**
180
	 * @return string
181
	 */
182
	public function country_name() {
183
		return $this->country_obj() instanceof \EE_Country
184
			? $this->country_obj()->name()
185
			: '';
186
	}
187
188
189
190
	/**
191
	 * @return \EE_Country
192
	 */
193
	public function country_obj() {
194
		return $this->_country_obj;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_country_obj; (string) is incompatible with the return type declared by the interface EEI_Address::country_obj of type EE_Country.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
195
	}
196
197
198
199
	/**
200
	 * @return string
201
	 */
202
	public function country() {
203
		if ( apply_filters( 'FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj() ) ) {
204
			return $this->country_ID();
205
		} else {
206
			return $this->country_name();
207
		}
208
	}
209
210
211
212
	/**
213
	 * @return string
214
	 */
215
	public function zip() {
216
		return $this->_zip;
217
	}
218
219
}
220
// End of file GenericAddress.php
221
// Location: core/entities/GenericAddress.php