Skip to content

Commit b87dee0

Browse files
authored
Merge pull request #64 from amicus-alex/memcached-add-zero-port
Add possibility to set zero port for memcache and memcached checkers
2 parents f45fe84 + c12eebd commit b87dee0

File tree

6 files changed

+137
-19
lines changed

6 files changed

+137
-19
lines changed

docs/book/diagnostics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ use Laminas\Diagnostics\Check\Memcache;
336336

337337
$checkLocal = new Memcache('127.0.0.1'); // default port
338338
$checkBackup = new Memcache('10.0.30.40', 11212);
339+
$checkSocket = new Memcache('unix:///run/memcached/memcached.sock', 0);
339340
```
340341

341342
## Memcached
@@ -348,6 +349,7 @@ use Laminas\Diagnostics\Check\Memcached;
348349

349350
$checkLocal = new Memcached('127.0.0.1'); // default port
350351
$checkBackup = new Memcached('10.0.30.40', 11212);
352+
$checkSocket = new Memcached('/run/memcached/memcached.sock', 0);
351353
```
352354

353355
## MongoDb

psalm-baseline.xml

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,21 @@
437437
</PropertyNotSetInConstructor>
438438
</file>
439439
<file src="src/Check/Memcache.php">
440-
<DocblockTypeContradiction occurrences="1">
440+
<DocblockTypeContradiction occurrences="2">
441+
<code>$port &lt; 0</code>
441442
<code>is_string($host)</code>
442443
</DocblockTypeContradiction>
443-
<MixedAssignment occurrences="2">
444+
<MixedAssignment occurrences="1">
444445
<code>$memcache</code>
445-
<code>$stats</code>
446446
</MixedAssignment>
447447
<MixedMethodCall occurrences="3">
448448
<code>addServer</code>
449449
<code>connect</code>
450450
<code>getExtendedStats</code>
451451
</MixedMethodCall>
452+
<NoValue occurrences="1">
453+
<code>$port</code>
454+
</NoValue>
452455
<PropertyNotSetInConstructor occurrences="1">
453456
<code>Memcache</code>
454457
</PropertyNotSetInConstructor>
@@ -457,18 +460,21 @@
457460
</RedundantCastGivenDocblockType>
458461
</file>
459462
<file src="src/Check/Memcached.php">
460-
<DocblockTypeContradiction occurrences="1">
463+
<DocblockTypeContradiction occurrences="2">
464+
<code>$port &lt; 0</code>
461465
<code>is_string($host)</code>
462466
</DocblockTypeContradiction>
463-
<MixedAssignment occurrences="2">
467+
<MixedAssignment occurrences="1">
464468
<code>$memcached</code>
465-
<code>$stats</code>
466469
</MixedAssignment>
467470
<MixedMethodCall occurrences="3">
468471
<code>addServer</code>
469472
<code>getLastDisconnectedServer</code>
470473
<code>getStats</code>
471474
</MixedMethodCall>
475+
<NoValue occurrences="1">
476+
<code>$port</code>
477+
</NoValue>
472478
<PropertyNotSetInConstructor occurrences="1">
473479
<code>Memcached</code>
474480
</PropertyNotSetInConstructor>
@@ -602,9 +608,33 @@
602608
</UndefinedDocblockClass>
603609
</file>
604610
<file src="src/Check/Redis.php">
611+
<MixedAssignment occurrences="1">
612+
<code>$client</code>
613+
</MixedAssignment>
614+
<MixedInferredReturnType occurrences="1">
615+
<code>PredisClient|RedisExtensionClient</code>
616+
</MixedInferredReturnType>
617+
<MixedMethodCall occurrences="2">
618+
<code>auth</code>
619+
<code>connect</code>
620+
</MixedMethodCall>
621+
<MixedReturnStatement occurrences="1">
622+
<code>$client</code>
623+
</MixedReturnStatement>
605624
<PropertyNotSetInConstructor occurrences="1">
606625
<code>Redis</code>
607626
</PropertyNotSetInConstructor>
627+
<UndefinedClass occurrences="1">
628+
<code>RedisException</code>
629+
</UndefinedClass>
630+
<UndefinedDocblockClass occurrences="3">
631+
<code>$client</code>
632+
<code>PredisClient|RedisExtensionClient</code>
633+
<code>RedisException</code>
634+
</UndefinedDocblockClass>
635+
<UnnecessaryVarAnnotation occurrences="1">
636+
<code>array</code>
637+
</UnnecessaryVarAnnotation>
608638
</file>
609639
<file src="src/Check/SecurityAdvisory.php">
610640
<DocblockTypeContradiction occurrences="1">
@@ -1039,15 +1069,17 @@
10391069
</PossiblyNullArgument>
10401070
</file>
10411071
<file src="test/MemcacheTest.php">
1042-
<InvalidArgument occurrences="1">
1072+
<InvalidArgument occurrences="2">
1073+
<code>-11211</code>
10431074
<code>['127.0.0.1']</code>
10441075
</InvalidArgument>
10451076
<InvalidCast occurrences="1">
10461077
<code>['127.0.0.1']</code>
10471078
</InvalidCast>
10481079
</file>
10491080
<file src="test/MemcachedTest.php">
1050-
<InvalidArgument occurrences="1">
1081+
<InvalidArgument occurrences="2">
1082+
<code>-11211</code>
10511083
<code>['127.0.0.1']</code>
10521084
</InvalidArgument>
10531085
<InvalidCast occurrences="1">

src/Check/Memcache.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ class Memcache extends AbstractCheck
2828
protected $port;
2929

3030
/**
31-
* @param string $host
32-
* @param int $port
31+
* @param string $host The hostname of the memcache server. This parameter may
32+
* also specify other transports like unix:///run/memcached/memcached.sock
33+
* to use UNIX domain sockets, in this case port must also be set to 0.
34+
* @param 0|positive-int $port The port where memcached is listening for connections.
35+
* Set this parameter to 0 when using UNIX domain sockets.
3336
* @throws InvalidArgumentException
3437
*/
3538
public function __construct($host = '127.0.0.1', $port = 11211)
@@ -42,9 +45,9 @@ public function __construct($host = '127.0.0.1', $port = 11211)
4245
}
4346

4447
$port = (int) $port;
45-
if ($port < 1) {
48+
if ($port < 0) {
4649
throw new InvalidArgumentException(sprintf(
47-
'Invalid port number %d - expecting a positive integer',
50+
'Invalid port number %d - expecting an unsigned integer',
4851
$port
4952
));
5053
}

src/Check/Memcached.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ class Memcached extends AbstractCheck
2727
protected $port;
2828

2929
/**
30-
* @param string $host
31-
* @param int $port
30+
* @param string $host The hostname of the memcache server. This parameter may
31+
* also specify other transports like /path/to/memcached.sock
32+
* to use UNIX domain sockets, in this case port must also be set to 0.
33+
* @param 0|positive-int $port The port where memcached is listening for connections.
34+
* Set this parameter to 0 when using UNIX domain sockets.
3235
* @throws InvalidArgumentException If host is not a string value.
33-
* @throws InvalidArgumentException If port is less than 1.
36+
* @throws InvalidArgumentException If port is less than 0.
3437
*/
3538
public function __construct($host = '127.0.0.1', $port = 11211)
3639
{
@@ -42,9 +45,9 @@ public function __construct($host = '127.0.0.1', $port = 11211)
4245
}
4346

4447
$port = (int) $port;
45-
if ($port < 1) {
48+
if ($port < 0) {
4649
throw new InvalidArgumentException(sprintf(
47-
'Invalid port number %d - expecting a positive integer',
50+
'Invalid port number %d - expecting an unsigned integer',
4851
$port
4952
));
5053
}

test/MemcacheTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,46 @@ public function testHostValidation(): void
1919
public function testPortValidation(): void
2020
{
2121
$this->expectException(InvalidArgumentException::class);
22-
$this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer");
22+
$this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer");
2323
new Memcache('127.0.0.1', -11211);
2424
}
25+
26+
/**
27+
* @dataProvider providerValidConstructorArguments
28+
* @param array<empty, empty>|array{string}|array{string, positive-int|0} $arguments
29+
*/
30+
public function testConstructor(array $arguments): void
31+
{
32+
new Memcache(...$arguments);
33+
34+
$this->expectNotToPerformAssertions();
35+
}
36+
37+
/**
38+
* @return non-empty-array<
39+
* non-empty-string,
40+
* array{array<empty, empty>|array{string}|array{string, positive-int|0}}
41+
* >
42+
*/
43+
public static function providerValidConstructorArguments(): array
44+
{
45+
return [
46+
'no arguments' => [[]],
47+
'only host' => [
48+
['127.0.0.1'],
49+
],
50+
'host and port' => [
51+
[
52+
'127.0.0.1',
53+
11211,
54+
],
55+
],
56+
'unix socket' => [
57+
[
58+
'unix:///run/memcached/memcached.sock',
59+
0,
60+
],
61+
],
62+
];
63+
}
2564
}

test/MemcachedTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,46 @@ public function testHostValidation(): void
1919
public function testPortValidation(): void
2020
{
2121
$this->expectException(InvalidArgumentException::class);
22-
$this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer");
22+
$this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer");
2323
new Memcached('127.0.0.1', -11211);
2424
}
25+
26+
/**
27+
* @dataProvider providerValidConstructorArguments
28+
* @param array<empty, empty>|array{string}|array{string, positive-int|0} $arguments
29+
*/
30+
public function testConstructor(array $arguments): void
31+
{
32+
new Memcached(...$arguments);
33+
34+
$this->expectNotToPerformAssertions();
35+
}
36+
37+
/**
38+
* @return non-empty-array<
39+
* non-empty-string,
40+
* array{array<empty, empty>|array{string}|array{string, positive-int|0}}
41+
* >
42+
*/
43+
public static function providerValidConstructorArguments(): array
44+
{
45+
return [
46+
'no arguments' => [[]],
47+
'only host' => [
48+
['127.0.0.1'],
49+
],
50+
'host and port' => [
51+
[
52+
'127.0.0.1',
53+
11211,
54+
],
55+
],
56+
'unix socket' => [
57+
[
58+
'unix:///run/memcached/memcached.sock',
59+
0,
60+
],
61+
],
62+
];
63+
}
2564
}

0 commit comments

Comments
 (0)