Skip to content

Commit

Permalink
Added typed parameters and typed return values
Browse files Browse the repository at this point in the history
  • Loading branch information
dude920228 committed Nov 5, 2018
1 parent 2e93338 commit b65c9e0
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 83 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.3",
"php": ">=7.2",
"psr/container": "^1.0",
"ext-sockets": "*",
"symfony/console": "^4.1",
Expand Down
12 changes: 6 additions & 6 deletions src/CacheClient/CacheClient.php
Expand Up @@ -16,12 +16,12 @@ class CacheClient implements CacheClientInterface
*/
private $ioHandler;

public function __construct($ioHandler)
public function __construct(CacheIOHandler $ioHandler)
{
$this->ioHandler = $ioHandler;
}

public function set($key, $package)
public function set(string $key, $package): void
{
$socket = $this->ioHandler->createClientSocket();
$data = ['action' => 'set', 'key' => $key, 'message' => $package];
Expand All @@ -30,7 +30,7 @@ public function set($key, $package)
$this->ioHandler->closeSocket($socket);
}

public function get($key)
public function get(string $key)
{
$data = ['action' => 'get', 'key' => $key];
$socket = $this->ioHandler->createClientSocket();
Expand All @@ -42,7 +42,7 @@ public function get($key)
return unserialize($recv);
}

public function delete($key)
public function delete(string $key): void
{
$data = ['action' => 'delete', 'key' => $key];
$socket = $this->ioHandler->createClientSocket();
Expand All @@ -51,15 +51,15 @@ public function delete($key)
$this->ioHandler->closeSocket($socket);
}

public function quitServer()
public function quitServer(): void
{
$data = ['action' => 'quit'];
$socket = $this->ioHandler->createClientSocket();
$dataString = serialize($data);
$this->ioHandler->writeToSocket($socket, $dataString);
}

public function getEntries()
public function getEntries(): array
{
$data = ['action' => 'getEntries'];
$socket = $this->ioHandler->createClientSocket();
Expand Down
6 changes: 3 additions & 3 deletions src/CacheClient/CacheClientInterface.php
Expand Up @@ -7,9 +7,9 @@
*/
interface CacheClientInterface
{
public function set($key, $message);
public function set(string $key, $message);

public function get($key);
public function get(string $key);

public function delete($key);
public function delete(string $key);
}
6 changes: 3 additions & 3 deletions src/CacheEventListener/CacheEventListenerInterface.php
Expand Up @@ -7,9 +7,9 @@
*/
interface CacheEventListenerInterface
{
public function onSet($key, $entry);
public function onSet(string $key, $entry);

public function onGet($key, $entry);
public function onGet(string $key, $entry);

public function onDelete($key, $entry);
public function onDelete(string $key, $entry);
}
36 changes: 18 additions & 18 deletions src/CacheServer/ActionHandler.php
Expand Up @@ -10,10 +10,10 @@
class ActionHandler
{
public function __invoke(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
): ?bool {
$action = $data['action'];
$functionName = 'handle'.ucfirst($action);
if (!method_exists($this, $functionName)) {
Expand All @@ -28,10 +28,10 @@ public function __invoke(
}

private function handleSet(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
):bool {
$package = $data['message'];
if ($server->getEventListener()) {
$package = $server->getEventListener()->onSet($data['key'], $package);
Expand All @@ -42,10 +42,10 @@ private function handleSet(
}

private function handleGet(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
): bool {
$key = $data['key'];
$package = $server->getBucket()->get($key);
if ($package === false) {
Expand All @@ -61,10 +61,10 @@ private function handleGet(
}

private function handleDelete(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
): bool {
$key = $data['key'];
$package = $server->getBucket()->get($key);
if ($package === false) {
Expand All @@ -79,10 +79,10 @@ private function handleDelete(
}

private function handleGetEntries(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
): bool {
$entries = $server->getBucket()->getEntries();
$entriesFormatted = [];
foreach ($entries as $key => $value) {
Expand All @@ -95,10 +95,10 @@ private function handleGetEntries(
}

private function handleQuit(
$server,
$data,
CacheServer $server,
array $data,
$connection
) {
): void {
$server->close();
}
}
24 changes: 12 additions & 12 deletions src/CacheServer/CacheServer.php
Expand Up @@ -2,6 +2,7 @@

namespace PhpCache\CacheServer;

use PhpCache\CacheEventListener\CacheEventListenerInterface;
use PhpCache\IO\CacheIOHandler;
use PhpCache\Storage\Bucket;
use PhpCache\Storage\Maintainer;
Expand Down Expand Up @@ -41,7 +42,11 @@ class CacheServer implements CacheServerInterface
private $clients;

public function __construct(
$ioHandler, $bucket, $actionHandler, $maintainer, $cacheEventListener = false
CacheIOHandler $ioHandler,
Bucket $bucket,
ActionHandler $actionHandler,
Maintainer $maintainer,
CacheEventListenerInterface $cacheEventListener = null
) {
$this->running = true;
$this->ioHandler = $ioHandler;
Expand All @@ -52,7 +57,7 @@ public function __construct(
$this->cacheEventListener = $cacheEventListener;
}

public function run()
public function run(): void
{
$this->socket = $this->ioHandler->createServerSocket();
while ($this->running) {
Expand All @@ -79,22 +84,17 @@ public function run()
}
}

public function getBucket()
public function getBucket(): Bucket
{
return $this->bucket;
}

public function getIOHandler()
public function getIOHandler(): CacheIOHandler
{
return $this->ioHandler;
}

public function getCacheEventListener()
{
return $this->cacheEventListener;
}

public function getMaintainer()
public function getMaintainer(): Maintainer
{
return $this->maintainer;
}
Expand All @@ -104,12 +104,12 @@ public function getSocket()
return $this->socket;
}

public function getEventListener()
public function getEventListener(): CacheEventListenerInterface
{
return $this->cacheEventListener;
}

public function close()
public function close(): void
{
$this->maintainer->backup($this->bucket);
$this->running = false;
Expand Down
4 changes: 2 additions & 2 deletions src/CacheServer/CacheServerInterface.php
Expand Up @@ -7,7 +7,7 @@
*/
interface CacheServerInterface
{
public function run();
public function run(): void;

public function close();
public function close(): void;
}
4 changes: 2 additions & 2 deletions src/ServiceManager/ConfigAggregator.php
Expand Up @@ -20,12 +20,12 @@ public function __construct()
$this->mergedConfig = [];
}

public function addConfig($config)
public function addConfig(array $config): void
{
$this->configs[] = $config;
}

public function getMergedConfig()
public function getMergedConfig(): array
{
foreach ($this->configs as $config) {
$this->mergedConfig = array_merge_recursive($this->mergedConfig, $config);
Expand Down
8 changes: 4 additions & 4 deletions src/ServiceManager/ServiceManager.php
Expand Up @@ -17,13 +17,13 @@ class ServiceManager implements ContainerInterface
private $factories;
private $invokables;

public function __construct($config)
public function __construct(array $config)
{
$this->config = $config;
$this->configure();
}

private function configure()
private function configure(): void
{
if (isset($this->config['services']['aliases'])) {
$this->aliases = $this->config['services']['aliases'];
Expand Down Expand Up @@ -64,7 +64,7 @@ public function get($id)
);
}

private function getServiceType($id)
private function getServiceType(string $id): string
{
if (array_key_exists($id, $this->aliases)) {
return 'alias';
Expand All @@ -73,7 +73,7 @@ private function getServiceType($id)
return array_key_exists($id, $this->factories) ? 'factory' : 'invokable';
}

public function getConfig()
public function getConfig(): array
{
return $this->config['config'];
}
Expand Down
19 changes: 7 additions & 12 deletions src/Storage/Bucket.php
Expand Up @@ -12,13 +12,13 @@ class Bucket implements StorageInterface
private $entries;
private $backupDir;

public function __construct($backupDir)
public function __construct(string $backupDir)
{
$this->backupDir = $backupDir;
$this->entries = [];
}

public function get($key)
public function get(string $key): string
{
if (!array_key_exists($key, $this->entries) && !$this->existsInBackup($key)) {
return false;
Expand All @@ -32,7 +32,7 @@ public function get($key)
return gzuncompress($this->entries[$key]['content']);
}

private function existsInBackup($key)
private function existsInBackup($key): bool
{
if (file_exists($this->backupDir.'/'.$key.'.dat')) {
return true;
Expand All @@ -41,7 +41,7 @@ private function existsInBackup($key)
return false;
}

private function getFromBackup($key)
private function getFromBackup($key): string
{
$contents = '';
$handle = fopen($this->backupDir.'/'.$key.'.dat', 'r+');
Expand All @@ -54,7 +54,7 @@ private function getFromBackup($key)
return $contents;
}

public function store($key, $entry, $time = null)
public function store(string $key, string $entry, $time = null): bool
{
$compressed = gzcompress($entry, 9);
$this->entries[$key]['content'] = $compressed;
Expand All @@ -66,12 +66,12 @@ public function store($key, $entry, $time = null)
return true;
}

public function getEntries()
public function getEntries(): array
{
return $this->entries;
}

public function delete($key)
public function delete($key): bool
{
if (array_key_exists($key, $this->entries)) {
unset($this->entries[$key]);
Expand All @@ -81,9 +81,4 @@ public function delete($key)

return false;
}

public function getKeys()
{
return array_keys($this->entries);
}
}

0 comments on commit b65c9e0

Please sign in to comment.