Skip to content

Commit 7648b11

Browse files
committed
Stop concatenating strings together to get full class name
Closes #194
1 parent 27e564d commit 7648b11

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
2.13.0
5+
-------------------
6+
7+
* The model class names are no longer constructed by concatenating strings.
8+
This change was made to improve support for tools like PHP-Scoper.
9+
Reported by Andrew Mead. GitHub #194.
10+
411
2.12.2 (2021-11-30)
512
-------------------
613

src/Database/Reader.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
use GeoIp2\Exception\AddressNotFoundException;
88
use GeoIp2\Model\AbstractModel;
9+
use GeoIp2\Model\AnonymousIp;
10+
use GeoIp2\Model\Asn;
11+
use GeoIp2\Model\City;
12+
use GeoIp2\Model\ConnectionType;
13+
use GeoIp2\Model\Country;
14+
use GeoIp2\Model\Domain;
15+
use GeoIp2\Model\Enterprise;
16+
use GeoIp2\Model\Isp;
917
use GeoIp2\ProviderInterface;
1018
use MaxMind\Db\Reader as DbReader;
1119
use MaxMind\Db\Reader\InvalidDatabaseException;
@@ -80,10 +88,10 @@ public function __construct(
8088
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
8189
* is corrupt or invalid
8290
*/
83-
public function city(string $ipAddress): \GeoIp2\Model\City
91+
public function city(string $ipAddress): City
8492
{
8593
// @phpstan-ignore-next-line
86-
return $this->modelFor('City', 'City', $ipAddress);
94+
return $this->modelFor(City::class, 'City', $ipAddress);
8795
}
8896

8997
/**
@@ -96,10 +104,10 @@ public function city(string $ipAddress): \GeoIp2\Model\City
96104
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
97105
* is corrupt or invalid
98106
*/
99-
public function country(string $ipAddress): \GeoIp2\Model\Country
107+
public function country(string $ipAddress): Country
100108
{
101109
// @phpstan-ignore-next-line
102-
return $this->modelFor('Country', 'Country', $ipAddress);
110+
return $this->modelFor(Country::class, 'Country', $ipAddress);
103111
}
104112

105113
/**
@@ -112,11 +120,11 @@ public function country(string $ipAddress): \GeoIp2\Model\Country
112120
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
113121
* is corrupt or invalid
114122
*/
115-
public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
123+
public function anonymousIp(string $ipAddress): AnonymousIp
116124
{
117125
// @phpstan-ignore-next-line
118126
return $this->flatModelFor(
119-
'AnonymousIp',
127+
AnonymousIp::class,
120128
'GeoIP2-Anonymous-IP',
121129
$ipAddress
122130
);
@@ -132,11 +140,11 @@ public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
132140
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
133141
* is corrupt or invalid
134142
*/
135-
public function asn(string $ipAddress): \GeoIp2\Model\Asn
143+
public function asn(string $ipAddress): Asn
136144
{
137145
// @phpstan-ignore-next-line
138146
return $this->flatModelFor(
139-
'Asn',
147+
Asn::class,
140148
'GeoLite2-ASN',
141149
$ipAddress
142150
);
@@ -152,11 +160,11 @@ public function asn(string $ipAddress): \GeoIp2\Model\Asn
152160
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
153161
* is corrupt or invalid
154162
*/
155-
public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
163+
public function connectionType(string $ipAddress): ConnectionType
156164
{
157165
// @phpstan-ignore-next-line
158166
return $this->flatModelFor(
159-
'ConnectionType',
167+
ConnectionType::class,
160168
'GeoIP2-Connection-Type',
161169
$ipAddress
162170
);
@@ -172,11 +180,11 @@ public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
172180
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
173181
* is corrupt or invalid
174182
*/
175-
public function domain(string $ipAddress): \GeoIp2\Model\Domain
183+
public function domain(string $ipAddress): Domain
176184
{
177185
// @phpstan-ignore-next-line
178186
return $this->flatModelFor(
179-
'Domain',
187+
Domain::class,
180188
'GeoIP2-Domain',
181189
$ipAddress
182190
);
@@ -192,10 +200,10 @@ public function domain(string $ipAddress): \GeoIp2\Model\Domain
192200
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
193201
* is corrupt or invalid
194202
*/
195-
public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
203+
public function enterprise(string $ipAddress): Enterprise
196204
{
197205
// @phpstan-ignore-next-line
198-
return $this->modelFor('Enterprise', 'Enterprise', $ipAddress);
206+
return $this->modelFor(Enterprise::class, 'Enterprise', $ipAddress);
199207
}
200208

201209
/**
@@ -208,11 +216,11 @@ public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
208216
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
209217
* is corrupt or invalid
210218
*/
211-
public function isp(string $ipAddress): \GeoIp2\Model\Isp
219+
public function isp(string $ipAddress): Isp
212220
{
213221
// @phpstan-ignore-next-line
214222
return $this->flatModelFor(
215-
'Isp',
223+
Isp::class,
216224
'GeoIP2-ISP',
217225
$ipAddress
218226
);
@@ -225,8 +233,6 @@ private function modelFor(string $class, string $type, string $ipAddress): Abstr
225233
$record['traits']['ip_address'] = $ipAddress;
226234
$record['traits']['prefix_len'] = $prefixLen;
227235

228-
$class = 'GeoIp2\\Model\\' . $class;
229-
230236
return new $class($record, $this->locales);
231237
}
232238

@@ -236,15 +242,14 @@ private function flatModelFor(string $class, string $type, string $ipAddress): A
236242

237243
$record['ip_address'] = $ipAddress;
238244
$record['prefix_len'] = $prefixLen;
239-
$class = 'GeoIp2\\Model\\' . $class;
240245

241246
return new $class($record);
242247
}
243248

244249
private function getRecord(string $class, string $type, string $ipAddress): array
245250
{
246251
if (strpos($this->dbType, $type) === false) {
247-
$method = lcfirst($class);
252+
$method = lcfirst((new \ReflectionClass($class))->getShortName());
248253

249254
throw new \BadMethodCallException(
250255
"The $method method cannot be used to open a {$this->dbType} database"

src/WebService/Client.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private function userAgent(): string
139139
public function city(string $ipAddress = 'me'): City
140140
{
141141
// @phpstan-ignore-next-line
142-
return $this->responseFor('city', 'City', $ipAddress);
142+
return $this->responseFor('city', City::class, $ipAddress);
143143
}
144144

145145
/**
@@ -168,7 +168,7 @@ public function city(string $ipAddress = 'me'): City
168168
*/
169169
public function country(string $ipAddress = 'me'): Country
170170
{
171-
return $this->responseFor('country', 'Country', $ipAddress);
171+
return $this->responseFor('country', Country::class, $ipAddress);
172172
}
173173

174174
/**
@@ -199,15 +199,16 @@ public function country(string $ipAddress = 'me'): Country
199199
public function insights(string $ipAddress = 'me'): Insights
200200
{
201201
// @phpstan-ignore-next-line
202-
return $this->responseFor('insights', 'Insights', $ipAddress);
202+
return $this->responseFor('insights', Insights::class, $ipAddress);
203203
}
204204

205205
private function responseFor(string $endpoint, string $class, string $ipAddress): Country
206206
{
207207
$path = implode('/', [self::$basePath, $endpoint, $ipAddress]);
208208

209209
try {
210-
$body = $this->client->get('GeoIP2 ' . $class, $path);
210+
$service = (new \ReflectionClass($class))->getShortName();
211+
$body = $this->client->get('GeoIP2 ' . $service, $path);
211212
} catch (\MaxMind\Exception\IpAddressNotFoundException $ex) {
212213
throw new AddressNotFoundException(
213214
$ex->getMessage(),
@@ -249,8 +250,6 @@ private function responseFor(string $endpoint, string $class, string $ipAddress)
249250
);
250251
}
251252

252-
$class = 'GeoIp2\\Model\\' . $class;
253-
254253
return new $class($body, $this->locales);
255254
}
256255
}

0 commit comments

Comments
 (0)