Skip to content

Commit dac6940

Browse files
style(*): Update PHPdoc and comments
1 parent 7b78ed4 commit dac6940

File tree

8 files changed

+52
-24
lines changed

8 files changed

+52
-24
lines changed

src/AbstractBounce.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Monolog\Formatter\LineFormatter;
1212
use Monolog\Handler\RotatingFileHandler;
1313
use Monolog\Logger;
14+
use Psr\Cache\InvalidArgumentException;
1415
use Psr\Log\LoggerInterface;
1516

1617
/**
@@ -53,7 +54,7 @@ protected function getArraySettings(string $name): array
5354
/**
5455
* Run a bounce.
5556
*
56-
* @throws Exception
57+
* @throws Exception|InvalidArgumentException
5758
*/
5859
public function run(
5960
): void {
@@ -97,7 +98,7 @@ protected function initLoggerHelper($logDirectoryPath, $loggerName): void
9798
}
9899

99100
/**
100-
* @throws Exception
101+
* @throws Exception|InvalidArgumentException
101102
*/
102103
protected function bounceCurrentIp()
103104
{

src/ApiClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
class ApiClient
2020
{
2121
/** @var LoggerInterface */
22-
private $logger = null;
22+
private $logger;
2323

2424
/**
2525
* @var RestClient
2626
*/
27-
private $restClient = null;
27+
private $restClient;
2828

2929
public function __construct(LoggerInterface $logger)
3030
{

src/Bouncer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use IPLib\ParseStringFlag;
1212
use Monolog\Handler\NullHandler;
1313
use Monolog\Logger;
14+
use Psr\Cache\InvalidArgumentException;
1415
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1617
use Symfony\Component\Config\Definition\Processor;
@@ -54,7 +55,7 @@ public function __construct(AbstractAdapter $cacheAdapter = null, LoggerInterfac
5455
*
5556
* @param array $config An array with all configuration parameters
5657
*
57-
* @throws \Psr\Cache\InvalidArgumentException
58+
* @throws InvalidArgumentException
5859
*/
5960
public function configure(array $config): void
6061
{
@@ -110,7 +111,7 @@ private function capRemediationLevel(string $remediation): string
110111
*
111112
* @return string the remediation to apply (ex: 'ban', 'captcha', 'bypass')
112113
*
113-
* @throws \Psr\Cache\InvalidArgumentException
114+
* @throws InvalidArgumentException
114115
*/
115116
public function getRemediationForIp(string $ip): string
116117
{
@@ -172,7 +173,7 @@ public static function getCaptchaHtmlTemplate(bool $error, string $captchaImageS
172173
*
173174
* @return array "count": number of decisions added, "errors": decisions not added
174175
*
175-
* @throws \Psr\Cache\InvalidArgumentException
176+
* @throws InvalidArgumentException
176177
*/
177178
public function warmBlocklistCacheUp(): array
178179
{
@@ -185,7 +186,7 @@ public function warmBlocklistCacheUp(): array
185186
*
186187
* @return array Number of deleted and new decisions, and errors when processing decisions
187188
*
188-
* @throws \Psr\Cache\InvalidArgumentException
189+
* @throws InvalidArgumentException
189190
*/
190191
public function refreshBlocklistCache(): array
191192
{
@@ -197,7 +198,7 @@ public function refreshBlocklistCache(): array
197198
*
198199
* @return bool If the cache has been successfully cleared or not
199200
*
200-
* @throws \Psr\Cache\InvalidArgumentException
201+
* @throws InvalidArgumentException
201202
*/
202203
public function clearCache(): bool
203204
{
@@ -266,7 +267,7 @@ public function checkCaptcha(string $expected, string $try, string $ip): bool
266267
*
267268
* @return void If the connection was successful or not
268269
*
269-
* @throws BouncerException|\Psr\Cache\InvalidArgumentException if the connection was not successful
270+
* @throws BouncerException|InvalidArgumentException if the connection was not successful
270271
* */
271272
public function testConnection()
272273
{

src/BouncerException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CrowdSecBouncer;
44

5+
use RuntimeException;
6+
57
/**
68
* Exception interface for all exceptions thrown by CrowdSec Bouncer.
79
*
@@ -12,6 +14,6 @@
1214
* @copyright Copyright (c) 2020+ CrowdSec
1315
* @license MIT License
1416
*/
15-
class BouncerException extends \RuntimeException
17+
class BouncerException extends RuntimeException
1618
{
1719
}

src/IBounce.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace CrowdSecBouncer;
44

5-
use Monolog\Logger;
6-
75
/**
86
* The interface to implement when bouncing.
97
*
@@ -52,7 +50,7 @@ public function getCaptchaWallOptions(): array;
5250
public function getBanWallOptions(): array;
5351

5452
/**
55-
* @return [[string, string], ...] Returns IP ranges to trust as proxies as an array of comparables ip bounds
53+
* @return array [[string, string], ...] Returns IP ranges to trust as proxies as an array of comparables ip bounds
5654
*/
5755
public function getTrustForwardedIpBoundsList(): array;
5856

@@ -67,7 +65,7 @@ public function getSessionVariable(string $name);
6765
public function setSessionVariable(string $name, $value): void;
6866

6967
/**
70-
* Unset a session variable, throw an error if this does not exists.
68+
* Unset a session variable, throw an error if this does not exist.
7169
*
7270
* @return void;
7371
*/

src/RestClient.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RestClient
2828
private $baseUri = null;
2929

3030
/** @var LoggerInterface */
31-
private $logger = null;
31+
private $logger;
3232

3333
public function __construct(LoggerInterface $logger)
3434
{
@@ -52,7 +52,7 @@ public function configure(string $baseUri, array $headers, int $timeout): void
5252
}
5353

5454
/**
55-
* Convert an key-value array of headers to the official HTTP header string.
55+
* Convert a key-value array of headers to the official HTTP header string.
5656
*/
5757
private function convertHeadersToString(array $headers): string
5858
{
@@ -67,7 +67,7 @@ private function convertHeadersToString(array $headers): string
6767
/**
6868
* Send an HTTP request using the file_get_contents and parse its JSON result if any.
6969
*
70-
* @throws BouncerException when the reponse status is not 2xx
70+
* @throws BouncerException when the response status is not 2xx
7171
*/
7272
public function request(
7373
string $endpoint,
@@ -115,8 +115,7 @@ public function request(
115115
if ($status < 200 || $status >= 300) {
116116
throw new BouncerException("unexpected response status from $this->baseUri$endpoint: $status\n".$response);
117117
}
118-
$data = json_decode($response, true);
119118

120-
return $data;
119+
return json_decode($response, true);
121120
}
122121
}

src/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function setSessionVariable(string $name, $value): void
3737
}
3838

3939
/**
40-
* Unset a session variable, throw an error if this does not exists.
40+
* Unset a session variable, throw an error if this does not exist.
4141
*
4242
* @return void;
4343
*/

src/StandAloneBounce.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace CrowdSecBouncer;
44

5+
use ErrorException;
6+
use Exception;
57
use Symfony\Component\Cache\Adapter\AbstractAdapter;
68
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
79
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
810
use Symfony\Component\Cache\Adapter\RedisAdapter;
11+
use Symfony\Component\Cache\Exception\CacheException;
912
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1013

1114
/**
@@ -23,6 +26,11 @@ class StandAloneBounce extends AbstractBounce implements IBounce
2326
/** @var AbstractAdapter */
2427
protected $cacheAdapter;
2528

29+
/**
30+
* @var string
31+
*/
32+
protected $session_name;
33+
2634
public function init(array $crowdSecStandaloneBouncerConfig)
2735
{
2836
if (\PHP_SESSION_NONE === session_status()) {
@@ -33,6 +41,10 @@ public function init(array $crowdSecStandaloneBouncerConfig)
3341
$this->initLogger();
3442
}
3543

44+
/**
45+
* @throws CacheException
46+
* @throws ErrorException
47+
*/
3648
private function getCacheAdapterInstance(): AbstractAdapter
3749
{
3850
// Singleton for this function
@@ -69,14 +81,18 @@ private function getCacheAdapterInstance(): AbstractAdapter
6981

7082
break;
7183
default:
72-
throw new BouncerException('Unknow selected cache technology.');
84+
throw new BouncerException('Unknown selected cache technology.');
7385
}
7486

7587
return $this->cacheAdapter;
7688
}
7789

7890
/**
7991
* @return Bouncer get the bouncer instance
92+
*
93+
* @throws CacheException
94+
* @throws ErrorException
95+
* @throws \Psr\Cache\InvalidArgumentException
8096
*/
8197
public function getBouncerInstance(): Bouncer
8298
{
@@ -271,7 +287,7 @@ public function setSessionVariable(string $name, $value): void
271287
}
272288

273289
/**
274-
* Unset a session variable, throw an error if this does not exists.
290+
* Unset a session variable, throw an error if this does not exist.
275291
*
276292
* @return void;
277293
*/
@@ -294,6 +310,9 @@ public function getPostedVariable(string $name): ?string
294310

295311
/**
296312
* If the current IP should be bounced or not, matching custom business rules.
313+
*
314+
* @throws CacheException
315+
* @throws ErrorException
297316
*/
298317
public function shouldBounceCurrentIp(): bool
299318
{
@@ -340,6 +359,10 @@ public function sendResponse(?string $body, int $statusCode = 200): void
340359
exit();
341360
}
342361

362+
/**
363+
* @throws \Psr\Cache\InvalidArgumentException
364+
* @throws Exception
365+
*/
343366
public function safelyBounce(): void
344367
{
345368
// If there is any technical problem while bouncing, don't block the user. Bypass bouncing and log the error.
@@ -349,7 +372,7 @@ public function safelyBounce(): void
349372
});
350373
$this->run();
351374
restore_error_handler();
352-
} catch (\Exception $e) {
375+
} catch (Exception $e) {
353376
$this->logger->error('', [
354377
'type' => 'EXCEPTION_WHILE_BOUNCING',
355378
'message' => $e->getMessage(),
@@ -368,6 +391,10 @@ public function safelyBounce(): void
368391
}
369392
}
370393

394+
/**
395+
* @throws ErrorException
396+
* @throws CacheException
397+
*/
371398
public function isConfigValid(): bool
372399
{
373400
$issues = ['errors' => [], 'warnings' => []];

0 commit comments

Comments
 (0)