Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup phpstan #38

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
on: [push, pull_request]

jobs:
run:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.3', '7.4', '8.0']
php-versions: ['8.0', '8.1', '8.2']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
Expand All @@ -31,3 +31,5 @@ jobs:
with:
files: ./coverage.xml
verbose: true
- name: Run phpstan
run: ./vendor/bin/phpstan --level=max --xdebug analyse src
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ vendor/*
.idea/*
.phpunit.result.cache
coverage.xml
.DS_Store
.DS_Store
.vscode/*
var/*
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"search engine"
],
"require": {
"php": "^7.3|^8.0"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^9.0"
"phpunit/phpunit": "^7.0|^9.0",
"phpstan/phpstan": "^1.9"
},
"autoload": {
"psr-4": {
Expand Down
63 changes: 61 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 42 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
use Psonic\Contracts\Client as ClientInterface;
use Psonic\Contracts\Command as CommandInterface;
use Psonic\Contracts\Response as ResponseInterface;
use RuntimeException;


class Client implements ClientInterface
{
/** @var resource $resource */
private $resource;

/** @var string */
private $host;
/** @var int */
private $port;
/** @var int|null */
private $errorNo;
/** @var string */
private $errorMessage;
/** @var int */
private $maxTimeout;

/**
Expand All @@ -39,11 +47,17 @@ public function __construct($host = 'localhost', $port = 1491, $timeout = 30)
*/
public function send(CommandInterface $command): ResponseInterface
{
if (!$this->resource) {
throw new ConnectionException();
if(!is_resource($this->resource)) {
//Fixme: In php8+ a try catch on fwrite throws a TypeError and catches the case of an empty $this->>resource variable
throw new ConnectionException("Not connected to sonic. Empty stream given ". var_export($this->resource, true));
}

$result = fwrite($this->resource, $command);

if($result === false) {
throw new RuntimeException("Unable to write to stream");
}

fwrite($this->resource, $command);
return $this->read();
}

Expand All @@ -53,28 +67,46 @@ public function send(CommandInterface $command): ResponseInterface
*/
public function read(): ResponseInterface
{
$message = explode("\r\n", fgets($this->resource))[0];
$string = fgets($this->resource);

if($string === false) {
throw new \RuntimeException("Unable to read from stream");
}

if(empty($string)) {
throw new \RuntimeException("Read empty string from stream");
}

$message = explode("\r\n", $string)[0];

return new SonicResponse($message);
}

/**
* @throws ConnectionException
* connects to the socket
*/
public function connect()
public function connect(): void
{
if (!$this->resource = stream_socket_client("tcp://{$this->host}:{$this->port}", $this->errorNo, $this->errorMessage, $this->maxTimeout)) {
throw new ConnectionException();
$resource = stream_socket_client("tcp://{$this->host}:{$this->port}", $this->errorNo, $this->errorMessage, $this->maxTimeout);
if (!$resource) {
throw new ConnectionException("Unable to connect to sonic search engine with given host: $this->host and port: $this->port}. Error code $this->errorNo with $this->errorMessage was produced");
}
$this->resource = $resource;
}

/**
* Disconnects from a socket
*/
public function disconnect()
public function disconnect(): void
{
stream_socket_shutdown($this->resource, STREAM_SHUT_WR);
$this->resource = null;
$result = stream_socket_shutdown($this->resource, STREAM_SHUT_WR);

if(!$result) {
throw new \RuntimeException("Unable to shut down stream socket connection");
}

fclose($this->resource);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

abstract class Command implements CommandInterface
{
private $command;
private $parameters;
private string $command;
/**
* @var array<mixed>
*/
private array $parameters;

public function __construct($command, $parameters = [])
/**
* @param array<mixed> $parameters
*/
public function __construct(string $command, array $parameters = [])
{
$this->command = $command;
$this->parameters = $parameters;
Expand All @@ -18,8 +24,9 @@ public function __construct($command, $parameters = [])
/**
* Wrap the string in quotes, and normalize whitespace. Also remove double quotes.
*/
protected function wrapInQuotes($string)
protected function wrapInQuotes(string $string):string
{
/** @var string $string */
$string = preg_replace('/[\r\n\t"]/', ' ', $string);
$string = '"' . str_replace('"', '\"', $string) . '"';
return $string;
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/Control/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class InfoCommand extends Command
{
private $command = 'INFO';
private $parameters = [];
private string $command = 'INFO';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* Info Command constructor.
Expand All @@ -16,4 +17,4 @@ public function __construct()
{
parent::__construct($this->command, $this->parameters);
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Control/StartControlChannelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class StartControlChannelCommand extends Command
{
private $command = 'START';
private $parameters = [];
private string $command = 'START';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* StartControlChannelCommand constructor.
Expand All @@ -22,4 +23,4 @@ public function __construct($password)

parent::__construct($this->command, $this->parameters);
}
}
}
8 changes: 4 additions & 4 deletions src/Commands/Control/TriggerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

final class TriggerCommand extends Command
{
private $command = 'TRIGGER';
private $parameters = [];
private string $command = 'TRIGGER';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* TriggerCommand constructor.
* @param string $action
*/
public function __construct(string $action)
{
Expand All @@ -21,4 +21,4 @@ public function __construct(string $action)

parent::__construct($this->command, $this->parameters);
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Ingest/CountCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class CountCommand extends Command
{
private $command = 'COUNT';
private $parameters = [];
private string $command = 'COUNT';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* Counts the number of objects
Expand All @@ -26,4 +27,4 @@ public function __construct(string $collection, string $bucket = null, string $o

parent::__construct($this->command, $this->parameters);
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Ingest/FlushBucketCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class FlushBucketCommand extends Command
{
private $command = 'FLUSHB';
private $parameters = [];
private string $command = 'FLUSHB';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* Flushes a given bucket in a collection
Expand All @@ -24,4 +25,4 @@ public function __construct(string $collection, string $bucket)

parent::__construct($this->command, $this->parameters);
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Ingest/FlushCollectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class FlushCollectionCommand extends Command
{
private $command = 'FLUSHC';
private $parameters = [];
private string $command = 'FLUSHC';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* Flushes a given collection
Expand All @@ -22,4 +23,4 @@ public function __construct(string $collection)

parent::__construct($this->command, $this->parameters);
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Ingest/FlushObjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

final class FlushObjectCommand extends Command
{
private $command = 'FLUSHO';
private $parameters = [];
private string $command = 'FLUSHO';
/** @var array<mixed> $parameters */
private array $parameters = [];

/**
* Flushes the text from an object
Expand All @@ -26,4 +27,4 @@ public function __construct(string $collection, string $bucket, string $object)

parent::__construct($this->command, $this->parameters);
}
}
}
Loading