Skip to content

Commit 7feed60

Browse files
refactor(*): Apply code smell advices
1 parent 030a0fe commit 7feed60

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

src/ApiCache.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ApiCache
7676
/**
7777
* @var array
7878
*/
79-
private $cacheKey = [];
79+
private $cacheKeys = [];
8080

8181
/**
8282
* @var array|null
@@ -151,11 +151,11 @@ public function configure(
151151
private function getScopes(): ?array
152152
{
153153
if (null === $this->scopes) {
154-
$scopes = [Constants::SCOPE_IP, Constants::SCOPE_RANGE];
154+
$finalScopes = [Constants::SCOPE_IP, Constants::SCOPE_RANGE];
155155
if (!empty($this->geolocConfig['enabled'])) {
156-
$scopes[] = Constants::SCOPE_COUNTRY;
156+
$finalScopes[] = Constants::SCOPE_COUNTRY;
157157
}
158-
$this->scopes = $scopes;
158+
$this->scopes = $finalScopes;
159159
}
160160

161161
return $this->scopes;
@@ -368,21 +368,21 @@ private function saveRemediationsForCacheKey(array $decisions, string $cacheKey)
368368
*/
369369
private function getCacheKey(string $scope, string $value): string
370370
{
371-
if (!isset($this->cacheKey[$scope][$value])) {
371+
if (!isset($this->cacheKeys[$scope][$value])) {
372372
switch ($scope) {
373373
case Constants::SCOPE_RANGE:
374374
case Constants::SCOPE_IP:
375-
$this->cacheKey[$scope][$value] = Constants::SCOPE_IP.':'.$value;
375+
$this->cacheKeys[$scope][$value] = Constants::SCOPE_IP.':'.$value;
376376
break;
377377
case Constants::SCOPE_COUNTRY:
378-
$this->cacheKey[$scope][$value] = Constants::SCOPE_COUNTRY.':'.$value;
378+
$this->cacheKeys[$scope][$value] = Constants::SCOPE_COUNTRY.':'.$value;
379379
break;
380380
default:
381-
throw new Exception('Unknown scope:'.$scope);
381+
throw new BouncerException('Unknown scope:'.$scope);
382382
}
383383
}
384384

385-
return $this->cacheKey[$scope][$value];
385+
return $this->cacheKeys[$scope][$value];
386386
}
387387

388388
/**

src/Geolocation.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function getMaxMindCountry(string $ip, string $databaseType, string $dat
5252
$record = $reader->city($ip);
5353
break;
5454
default:
55-
throw new Exception("Unknown MaxMind database type:$databaseType");
55+
throw new BouncerException("Unknown MaxMind database type:$databaseType");
5656
}
5757
$result['country'] = $record->country->isoCode;
5858
} catch (AddressNotFoundException $e) {
@@ -85,14 +85,11 @@ public function getCountryResult(array $geolocConfig, string $ip): array
8585

8686
return $result;
8787
}
88-
switch ($geolocConfig['type']) {
89-
case Constants::GEOLOCATION_TYPE_MAXMIND:
90-
$configPath = $geolocConfig[Constants::GEOLOCATION_TYPE_MAXMIND];
91-
$result =
92-
$this->getMaxMindCountry($ip, $configPath['database_type'], $configPath['database_path']);
93-
break;
94-
default:
95-
throw new Exception('Unknown Geolocation type:'.$geolocConfig['type']);
88+
if (Constants::GEOLOCATION_TYPE_MAXMIND === $geolocConfig['type']) {
89+
$configPath = $geolocConfig[Constants::GEOLOCATION_TYPE_MAXMIND];
90+
$result = $this->getMaxMindCountry($ip, $configPath['database_type'], $configPath['database_path']);
91+
} else {
92+
throw new BouncerException('Unknown Geolocation type:'.$geolocConfig['type']);
9693
}
9794

9895
if ($saveInSession) {

tests/WatcherClient.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class WatcherClient
1111
public const WATCHER_LOGIN = 'PhpUnitTestMachine';
1212
public const WATCHER_PASSWORD = 'PhpUnitTestMachinePassword';
1313

14+
public const HOURS24 = '+24 hours';
15+
1416
/** @var LoggerInterface */
1517
private $logger;
1618

@@ -53,7 +55,7 @@ public function setInitialState(): void
5355
$this->deleteAllDecisions();
5456
$now = new DateTime();
5557
$this->addDecision($now, '12h', '+12 hours', TestHelpers::BAD_IP, 'captcha');
56-
$this->addDecision($now, '24h', '+24 hours', TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE, 'ban');
58+
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE, 'ban');
5759
$this->addDecision($now, '24h', '+24 hours', TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
5860
}
5961

@@ -65,9 +67,9 @@ public function setSecondState(): void
6567
$now = new DateTime();
6668
$this->addDecision($now, '36h', '+36 hours', TestHelpers::NEWLY_BAD_IP, 'ban');
6769
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE, 'captcha');
68-
$this->addDecision($now, '24h', '+24 hours', TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
69-
$this->addDecision($now, '24h', '+24 hours', TestHelpers::IP_JAPAN, 'ban');
70-
$this->addDecision($now, '24h', '+24 hours', TestHelpers::IP_FRANCE, 'ban');
70+
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
71+
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_JAPAN, 'ban');
72+
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_FRANCE, 'ban');
7173
}
7274

7375
/**

tests/end-to-end/utils/watcherClient.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const ip2long = (argIpParam) => {
5252
);
5353
argIP = argIP.match(pattern);
5454
if (!argIP) {
55-
return false;
55+
throw new Error(`${argIpParam} is not a valid IP`);
5656
}
5757
argIP[0] = 0;
5858
for (i = 1; i < 5; i += 1) {
@@ -68,7 +68,9 @@ const ip2long = (argIpParam) => {
6868
argIP[3] >= argIP[7] ||
6969
argIP[4] >= argIP[8]
7070
) {
71-
return false;
71+
throw new Error(
72+
`Something went wrong with ${argIpParam} ip2long process`,
73+
);
7274
}
7375
return (
7476
argIP[1] * (argIP[0] === 1 || 16777216) +

0 commit comments

Comments
 (0)