Skip to content

Commit 6cf188e

Browse files
authored
Merge pull request #16 from seregazhuk/final-classes-with-type-hints
Move to final classes with type-hints
2 parents 3cbbeef + 0ec58f9 commit 6cf188e

20 files changed

+91
-293
lines changed

src/Client.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(Connection $connection, Parser $parser)
7272
$this->setConnectionHandlers();
7373
}
7474

75-
protected function setConnectionHandlers()
75+
protected function setConnectionHandlers(): void
7676
{
7777
$this->connection->on('data', function ($chunk) {
7878
$parsed = $this->parser->parseRawResponse($chunk);
@@ -95,7 +95,7 @@ protected function setConnectionHandlers()
9595
* @param array $args
9696
* @return Promise|PromiseInterface
9797
*/
98-
public function __call($name, $args)
98+
public function __call(string $name, array $args)
9999
{
100100
$request = new Request($name);
101101

@@ -118,7 +118,7 @@ public function __call($name, $args)
118118
* @param string[] $responses
119119
* @throws Exception
120120
*/
121-
public function resolveRequests(array $responses)
121+
public function resolveRequests(array $responses): void
122122
{
123123
if ($this->pool->isEmpty()) {
124124
throw new Exception('Received unexpected response, no matching request found');
@@ -143,7 +143,7 @@ public function resolveRequests(array $responses)
143143
/**
144144
* Closes the connection when all requests are resolved
145145
*/
146-
public function end()
146+
public function end(): void
147147
{
148148
$this->isEnding = true;
149149

@@ -155,7 +155,7 @@ public function end()
155155
/**
156156
* Forces closing the connection and rejects all pending requests
157157
*/
158-
public function close()
158+
public function close(): void
159159
{
160160
if ($this->isClosed) {
161161
return;

src/Connection/CommandsPool.php

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

33
namespace seregazhuk\React\Memcached\Connection;
44

5-
class CommandsPool
5+
final class CommandsPool
66
{
77
/**
88
* @var string[]
99
*/
1010
private $commands = [];
1111

12-
/**
13-
* @param string $command
14-
*/
15-
public function add($command)
12+
public function add(string $command): void
1613
{
1714
$this->commands[] = $command;
1815
}
1916

20-
public function clear()
17+
public function clear(): void
2118
{
2219
$this->commands = [];
2320
}
2421

25-
/**
26-
* @return string
27-
*/
28-
public function shift()
22+
public function shift(): string
2923
{
3024
return array_shift($this->commands);
3125
}

src/Connection/Connection.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,29 @@
88
use React\Socket\ConnectorInterface;
99
use React\Stream\DuplexStreamInterface;
1010

11-
class Connection extends EventEmitter
11+
final class Connection extends EventEmitter
1212
{
1313
/**
1414
* @var DuplexStreamInterface
1515
*/
1616
private $stream;
1717

18-
/**
19-
* @var string
20-
*/
2118
private $address;
2219

23-
/**
24-
* @var ConnectorInterface
25-
*/
2620
private $connector;
2721

28-
/**
29-
* @var bool
30-
*/
3122
private $isConnecting = false;
3223

33-
/**
34-
* @var CommandsPool
35-
*/
3624
private $commandsPool;
3725

38-
/**
39-
* @param string $address
40-
* @param ConnectorInterface $connector
41-
*/
42-
public function __construct($address, ConnectorInterface $connector)
26+
public function __construct(string $address, ConnectorInterface $connector)
4327
{
4428
$this->address = $address;
4529
$this->connector = $connector;
4630
$this->commandsPool = new CommandsPool();
4731
}
4832

49-
/**
50-
* @return PromiseInterface
51-
*/
52-
public function connect()
33+
public function connect(): PromiseInterface
5334
{
5435
$this->isConnecting = true;
5536

@@ -61,10 +42,7 @@ public function connect()
6142
);
6243
}
6344

64-
/**
65-
* @param ConnectionInterface $stream
66-
*/
67-
public function onConnected(ConnectionInterface $stream)
45+
public function onConnected(ConnectionInterface $stream): void
6846
{
6947
$this->stream = $stream;
7048
$this->isConnecting = false;
@@ -80,13 +58,13 @@ public function onConnected(ConnectionInterface $stream)
8058
}
8159
}
8260

83-
public function onFailed()
61+
public function onFailed(): void
8462
{
8563
$this->cancelConnecting();
8664
$this->emit('failed');
8765
}
8866

89-
public function close()
67+
public function close(): void
9068
{
9169
if ($this->stream) {
9270
$this->stream->close();
@@ -96,10 +74,7 @@ public function close()
9674
$this->emit('close');
9775
}
9876

99-
/**
100-
* @param string $command
101-
*/
102-
public function write($command)
77+
public function write(string $command): void
10378
{
10479
if ($this->stream && $this->stream->isWritable()) {
10580
$this->stream->write($command);
@@ -112,7 +87,7 @@ public function write($command)
11287
}
11388
}
11489

115-
private function cancelConnecting()
90+
private function cancelConnecting(): void
11691
{
11792
$this->isConnecting = false;
11893
$this->commandsPool->clear();

src/Exception/ConnectionClosedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Throwable;
66

7-
class ConnectionClosedException extends Exception
7+
final class ConnectionClosedException extends Exception
88
{
99
public function __construct($message = '', $code = 0, Throwable $previous = null)
1010
{

src/Exception/Exception.php

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

33
namespace seregazhuk\React\Memcached\Exception;
44

5-
class Exception extends \Exception
5+
use Exception as BaseException;
6+
7+
class Exception extends BaseException
68
{
79
}

src/Exception/FailedCommandException.php

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

33
namespace seregazhuk\React\Memcached\Exception;
44

5-
class FailedCommandException extends CommandException
5+
final class FailedCommandException extends CommandException
66
{
77
}

src/Exception/WrongCommandException.php

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

33
namespace seregazhuk\React\Memcached\Exception;
44

5-
class WrongCommandException extends CommandException
5+
final class WrongCommandException extends CommandException
66
{
77
}

src/Factory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99

1010
class Factory
1111
{
12-
/**
13-
* Creates a memcached client
14-
* @param LoopInterface $loop
15-
* @param string $address
16-
* @return Client
17-
*/
18-
public static function createClient(LoopInterface $loop, $address = 'localhost:11211')
12+
public static function createClient(LoopInterface $loop, string $address = 'localhost:11211'): Client
1913
{
2014
$connection = new Connection($address, new Connector($loop));
2115

src/Protocol/Parser.php

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
use seregazhuk\React\Memcached\Protocol\Response\VersionResponse;
1818
use seregazhuk\React\Memcached\Protocol\Response\WriteResponse;
1919

20-
class Parser
20+
final class Parser
2121
{
22-
const RESPONSE_END = 'END';
23-
const RESPONSE_STORED = 'STORED';
24-
const RESPONSE_NOT_STORED = 'NOT_STORED';
25-
const RESPONSE_DELETED = 'DELETED';
26-
const RESPONSE_NOT_FOUND = 'NOT_FOUND';
27-
const RESPONSE_OK = 'OK';
28-
const RESPONSE_EXISTS = 'EXISTS';
29-
const RESPONSE_ERROR = 'ERROR';
30-
const RESPONSE_RESET = 'RESET';
31-
const RESPONSE_VERSION = 'VERSION';
32-
const RESPONSE_VALUE = 'VALUE';
33-
const RESPONSE_TOUCHED = 'TOUCHED';
34-
35-
const RESPONSE_ENDS = [
22+
public const RESPONSE_STORED = 'STORED';
23+
public const RESPONSE_DELETED = 'DELETED';
24+
public const RESPONSE_NOT_FOUND = 'NOT_FOUND';
25+
public const RESPONSE_OK = 'OK';
26+
public const RESPONSE_VERSION = 'VERSION';
27+
28+
private const RESPONSE_NOT_STORED = 'NOT_STORED';
29+
private const RESPONSE_END = 'END';
30+
private const RESPONSE_EXISTS = 'EXISTS';
31+
public const RESPONSE_TOUCHED = 'TOUCHED';
32+
private const RESPONSE_ERROR = 'ERROR';
33+
private const RESPONSE_RESET = 'RESET';
34+
35+
private const RESPONSE_ENDS = [
3636
self::RESPONSE_END,
3737
self::RESPONSE_DELETED,
3838
self::RESPONSE_NOT_FOUND,
@@ -45,60 +45,49 @@ class Parser
4545
self::RESPONSE_TOUCHED,
4646
];
4747

48-
const COMMAND_SET = 'set';
48+
private const COMMAND_SET = 'set';
4949

50-
const COMMAND_SEPARATOR = "\r\n";
50+
public const COMMAND_SEPARATOR = "\r\n";
5151

52-
const STORAGE_COMMANDS = [
52+
private const STORAGE_COMMANDS = [
5353
self::COMMAND_SET,
5454
self::COMMAND_ADD,
5555
self::COMMAND_REPLACE,
5656
];
5757

58-
const COMMAND_GET = 'get';
59-
60-
const RETRIEVAL_COMMANDS = [
61-
self::COMMAND_GET,
62-
];
63-
64-
const WRITE_RESPONSE_ENDS = [
65-
self::RESPONSE_STORED,
66-
self::RESPONSE_NOT_STORED,
67-
self::RESPONSE_EXISTS,
68-
self::RESPONSE_NOT_FOUND,
69-
];
70-
71-
const COMMAND_VERSION = 'version';
72-
const COMMAND_STATS = 'stats';
73-
const COMMAND_TOUCH = 'touch';
74-
const COMMAND_DELETE = 'delete';
75-
const COMMAND_INCREMENT = 'incr';
76-
const COMMAND_DECREMENT = 'decr';
77-
const COMMAND_ADD = 'add';
78-
const COMMAND_REPLACE = 'replace';
79-
const COMMAND_VERBOSITY = 'verbosity';
80-
const COMMAND_FLUSH_ALL = 'flushAll';
81-
82-
const COMMANDS = [
83-
self::COMMAND_ADD,
84-
self::COMMAND_DECREMENT,
85-
self::COMMAND_DELETE,
86-
self::COMMAND_FLUSH_ALL,
87-
self::COMMAND_GET,
88-
self::COMMAND_INCREMENT,
89-
self::COMMAND_REPLACE,
90-
self::COMMAND_SET,
91-
self::COMMAND_STATS,
92-
self::COMMAND_TOUCH,
93-
self::COMMAND_VERBOSITY,
94-
self::COMMAND_VERSION,
95-
];
58+
private const COMMAND_GET = 'get';
59+
60+
private const COMMAND_VERSION = 'version';
61+
private const COMMAND_STATS = 'stats';
62+
private const COMMAND_TOUCH = 'touch';
63+
private const COMMAND_DELETE = 'delete';
64+
private const COMMAND_INCREMENT = 'incr';
65+
private const COMMAND_DECREMENT = 'decr';
66+
private const COMMAND_ADD = 'add';
67+
private const COMMAND_REPLACE = 'replace';
68+
private const COMMAND_VERBOSITY = 'verbosity';
69+
private const COMMAND_FLUSH_ALL = 'flushAll';
70+
71+
private const COMMANDS = [
72+
self::COMMAND_ADD,
73+
self::COMMAND_DECREMENT,
74+
self::COMMAND_DELETE,
75+
self::COMMAND_FLUSH_ALL,
76+
self::COMMAND_GET,
77+
self::COMMAND_INCREMENT,
78+
self::COMMAND_REPLACE,
79+
self::COMMAND_SET,
80+
self::COMMAND_STATS,
81+
self::COMMAND_TOUCH,
82+
self::COMMAND_VERBOSITY,
83+
self::COMMAND_VERSION,
84+
];
9685

9786
/**
9887
* @param string $data
9988
* @return string[]
10089
*/
101-
public function parseRawResponse($data = '')
90+
public function parseRawResponse(string $data = ''): array
10291
{
10392
$data = substr($data, 0, strlen($data) - strlen(self::COMMAND_SEPARATOR));
10493
$lines = explode(self::COMMAND_SEPARATOR, $data);

src/Protocol/Request/Request.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ abstract class Request
99
*/
1010
protected $command;
1111

12-
/**
13-
* @return string
14-
*/
15-
public function command()
12+
public function command(): string
1613
{
1714
return $this->command;
1815
}

0 commit comments

Comments
 (0)