Skip to content

Commit

Permalink
Merge pull request #64 from amicus-alex/memcached-add-zero-port
Browse files Browse the repository at this point in the history
Add possibility to set zero port for memcache and memcached checkers
  • Loading branch information
Ocramius authored Dec 15, 2022
2 parents f45fe84 + c12eebd commit b87dee0
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 19 deletions.
2 changes: 2 additions & 0 deletions docs/book/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ use Laminas\Diagnostics\Check\Memcache;

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

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

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

## MongoDb
Expand Down
48 changes: 40 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -437,18 +437,21 @@
</PropertyNotSetInConstructor>
</file>
<file src="src/Check/Memcache.php">
<DocblockTypeContradiction occurrences="1">
<DocblockTypeContradiction occurrences="2">
<code>$port &lt; 0</code>
<code>is_string($host)</code>
</DocblockTypeContradiction>
<MixedAssignment occurrences="2">
<MixedAssignment occurrences="1">
<code>$memcache</code>
<code>$stats</code>
</MixedAssignment>
<MixedMethodCall occurrences="3">
<code>addServer</code>
<code>connect</code>
<code>getExtendedStats</code>
</MixedMethodCall>
<NoValue occurrences="1">
<code>$port</code>
</NoValue>
<PropertyNotSetInConstructor occurrences="1">
<code>Memcache</code>
</PropertyNotSetInConstructor>
Expand All @@ -457,18 +460,21 @@
</RedundantCastGivenDocblockType>
</file>
<file src="src/Check/Memcached.php">
<DocblockTypeContradiction occurrences="1">
<DocblockTypeContradiction occurrences="2">
<code>$port &lt; 0</code>
<code>is_string($host)</code>
</DocblockTypeContradiction>
<MixedAssignment occurrences="2">
<MixedAssignment occurrences="1">
<code>$memcached</code>
<code>$stats</code>
</MixedAssignment>
<MixedMethodCall occurrences="3">
<code>addServer</code>
<code>getLastDisconnectedServer</code>
<code>getStats</code>
</MixedMethodCall>
<NoValue occurrences="1">
<code>$port</code>
</NoValue>
<PropertyNotSetInConstructor occurrences="1">
<code>Memcached</code>
</PropertyNotSetInConstructor>
Expand Down Expand Up @@ -602,9 +608,33 @@
</UndefinedDocblockClass>
</file>
<file src="src/Check/Redis.php">
<MixedAssignment occurrences="1">
<code>$client</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>PredisClient|RedisExtensionClient</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="2">
<code>auth</code>
<code>connect</code>
</MixedMethodCall>
<MixedReturnStatement occurrences="1">
<code>$client</code>
</MixedReturnStatement>
<PropertyNotSetInConstructor occurrences="1">
<code>Redis</code>
</PropertyNotSetInConstructor>
<UndefinedClass occurrences="1">
<code>RedisException</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="3">
<code>$client</code>
<code>PredisClient|RedisExtensionClient</code>
<code>RedisException</code>
</UndefinedDocblockClass>
<UnnecessaryVarAnnotation occurrences="1">
<code>array</code>
</UnnecessaryVarAnnotation>
</file>
<file src="src/Check/SecurityAdvisory.php">
<DocblockTypeContradiction occurrences="1">
Expand Down Expand Up @@ -1039,15 +1069,17 @@
</PossiblyNullArgument>
</file>
<file src="test/MemcacheTest.php">
<InvalidArgument occurrences="1">
<InvalidArgument occurrences="2">
<code>-11211</code>
<code>['127.0.0.1']</code>
</InvalidArgument>
<InvalidCast occurrences="1">
<code>['127.0.0.1']</code>
</InvalidCast>
</file>
<file src="test/MemcachedTest.php">
<InvalidArgument occurrences="1">
<InvalidArgument occurrences="2">
<code>-11211</code>
<code>['127.0.0.1']</code>
</InvalidArgument>
<InvalidCast occurrences="1">
Expand Down
11 changes: 7 additions & 4 deletions src/Check/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class Memcache extends AbstractCheck
protected $port;

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

$port = (int) $port;
if ($port < 1) {
if ($port < 0) {
throw new InvalidArgumentException(sprintf(
'Invalid port number %d - expecting a positive integer',
'Invalid port number %d - expecting an unsigned integer',
$port
));
}
Expand Down
13 changes: 8 additions & 5 deletions src/Check/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class Memcached extends AbstractCheck
protected $port;

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

$port = (int) $port;
if ($port < 1) {
if ($port < 0) {
throw new InvalidArgumentException(sprintf(
'Invalid port number %d - expecting a positive integer',
'Invalid port number %d - expecting an unsigned integer',
$port
));
}
Expand Down
41 changes: 40 additions & 1 deletion test/MemcacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,46 @@ public function testHostValidation(): void
public function testPortValidation(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer");
$this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer");
new Memcache('127.0.0.1', -11211);
}

/**
* @dataProvider providerValidConstructorArguments
* @param array<empty, empty>|array{string}|array{string, positive-int|0} $arguments
*/
public function testConstructor(array $arguments): void
{
new Memcache(...$arguments);

$this->expectNotToPerformAssertions();
}

/**
* @return non-empty-array<
* non-empty-string,
* array{array<empty, empty>|array{string}|array{string, positive-int|0}}
* >
*/
public static function providerValidConstructorArguments(): array
{
return [
'no arguments' => [[]],
'only host' => [
['127.0.0.1'],
],
'host and port' => [
[
'127.0.0.1',
11211,
],
],
'unix socket' => [
[
'unix:///run/memcached/memcached.sock',
0,
],
],
];
}
}
41 changes: 40 additions & 1 deletion test/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,46 @@ public function testHostValidation(): void
public function testPortValidation(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer");
$this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer");
new Memcached('127.0.0.1', -11211);
}

/**
* @dataProvider providerValidConstructorArguments
* @param array<empty, empty>|array{string}|array{string, positive-int|0} $arguments
*/
public function testConstructor(array $arguments): void
{
new Memcached(...$arguments);

$this->expectNotToPerformAssertions();
}

/**
* @return non-empty-array<
* non-empty-string,
* array{array<empty, empty>|array{string}|array{string, positive-int|0}}
* >
*/
public static function providerValidConstructorArguments(): array
{
return [
'no arguments' => [[]],
'only host' => [
['127.0.0.1'],
],
'host and port' => [
[
'127.0.0.1',
11211,
],
],
'unix socket' => [
[
'unix:///run/memcached/memcached.sock',
0,
],
],
];
}
}

0 comments on commit b87dee0

Please sign in to comment.