Skip to content

Commit

Permalink
Merge pull request #10 from QoboLtd/improve-type-safety
Browse files Browse the repository at this point in the history
Improve type safety
  • Loading branch information
nicosp authored Nov 20, 2024
2 parents 4a468b5 + 362725a commit 967c066
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 146 deletions.
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Parameter \#1 \$codepoint of function chr expects int, string given\.$#'
identifier: argument.type
count: 1
path: src/Parsers/Search/OneX.php
6 changes: 5 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
parameters:
level: 3
level: 5
paths:
- src
- tests
treatPhpDocTypesAsCertain: false

includes:
- phpstan-baseline.neon
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getHeader(string $name): ?string
{
$headers = $this->response->getHeader($name);

if ($headers) {
if ($headers !== []) {
return implode('; ', $headers);
} else {
return null;
Expand Down
11 changes: 5 additions & 6 deletions src/Models/Bulletin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class Bulletin implements \Stringable

public function __construct(array $details = [])
{
if ($details && is_array($details)) {
$this->details = array_change_key_case($details, CASE_UPPER);
}
$this->details = array_change_key_case($details, CASE_UPPER);
}

public function getBody(): ?string
Expand All @@ -22,9 +20,10 @@ public function getBody(): ?string
}

/**
* @return $this
* @param ?string $body Body
* @return self
*/
public function setBody($body): static
public function setBody(?string $body): self
{
$this->body = $body;

Expand Down Expand Up @@ -70,6 +69,6 @@ public function getMetadataTimestamp()

public function __toString(): string
{
return (string) $this->body;
return $this->body ?? '';
}
}
8 changes: 4 additions & 4 deletions src/Models/Search/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ class Record implements \ArrayAccess, \Stringable
protected ?string $restricted_value = '****';
protected array $values = [];

public function get(string $field): mixed
public function get(string|int $field): mixed
{
return $this->values[$field] ?? null;
}

/**
* @param $value
*/
public function set(string $field, $value)
public function set(string|int $field, $value)
{
$this->values[$field] = $value;
}

public function remove(string $field)
public function remove(string|int $field)
{
unset($this->values[$field]);
}

public function isRestricted(string $field): bool
public function isRestricted(string|int $field): bool
{
$val = $this->get($field);

Expand Down
41 changes: 16 additions & 25 deletions src/Models/Search/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function setHeaders(array $headers): static
}

/**
* @param null $keyed_by
* @param mixed $keyed_by
*/
public function addRecord(Record $record, $keyed_by = null)
public function addRecord(Record $record, mixed $keyed_by = null): void
{
// register this Results object as the record's parent automatically
$record->setParent($this);
Expand All @@ -71,7 +71,7 @@ public function addRecord(Record $record, $keyed_by = null)
*
* @param $field
*/
public function keyResultsBy($field)
public function keyResultsBy($field): void
{
$results = clone $this->results;
$this->results = new Collection();
Expand All @@ -91,19 +91,19 @@ public function find($key_id): ?Record
}

/**
* @return null
* @return mixed
*/
public function getError()
{
return $this->error;
}

/**
* @param null $error
* @param mixed $error
*
* @return $this
* @return self
*/
public function setError($error): static
public function setError(mixed $error): self
{
$this->error = $error;

Expand All @@ -116,14 +116,10 @@ public function getReturnedResultsCount(): int
}

/**
* @return $this
* @return self
*/
public function setReturnedResultsCount(int $returned_results_count): static
public function setReturnedResultsCount(int $returned_results_count): self
{
if (is_int($returned_results_count) == false) {
throw new \InvalidArgumentException('$returned_results_count should be an integer');
}

$this->returned_results_count = $returned_results_count;

return $this;
Expand All @@ -135,14 +131,10 @@ public function getTotalResultsCount(): int
}

/**
* @return $this
* @return self
*/
public function setTotalResultsCount(int $total_results_count): static
public function setTotalResultsCount(int $total_results_count): self
{
if (is_int($total_results_count) == false) {
throw new \InvalidArgumentException('$total_results_count should be an integer');
}

$this->total_results_count = $total_results_count;

return $this;
Expand Down Expand Up @@ -187,17 +179,16 @@ public function getSession(): ?Session
}

/**
* @return $this
* @return self
*/
public function setSession(Session $session): static
public function setSession(Session $session): self
{
$this->session = $session;

return $this;
}

/**
* @return null
*
* @throws \PHRETS\Exceptions\CapabilityUnavailable
*/
Expand All @@ -211,11 +202,11 @@ public function getMetadata()
}

/**
* @param null $metadata
* @param mixed $metadata
*
* @return $this
* @return self
*/
public function setMetadata($metadata): static
public function setMetadata(mixed $metadata): self
{
$this->metadata = $metadata;

Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class BaseObject extends Base
{
public function parse(Session $rets, Response $response): Collection
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class Lookup extends Base
{
public function parse(Session $rets, Response $response)
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/LookupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class LookupType extends Base
{
public function parse(Session $rets, Response $response): Collection
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class Resource extends Base
{
public function parse(Session $rets, Response $response): Collection
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/ResourceClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class ResourceClass extends Base
{
public function parse(Session $rets, Response $response): Collection
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use PHRETS\Http\Response;
use PHRETS\Models\Metadata\System as SystemModel;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class System extends Base
{
public function parse(Session $rets, Response $response): SystemModel
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$base = $xml->METADATA->{'METADATA-SYSTEM'};
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Support\Collection;
use PHRETS\Http\Response;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class Table extends Base
{
public function parse(Session $rets, Response $response, $keyed_by): Collection
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(\PHRETS\Strategies\Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$collection = new Collection();
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/GetObject/Single.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Single
{
public function parse(Response $response): BaseObject
{
$body = (string)$response->getBody();
$obj = new BaseObject();
$obj->setContent(($response->getBody()) ? $response->getBody()->__toString() : null);
$obj->setContent($body !== '' ? $body : null);
$obj->setContentDescription($response->getHeader('Content-Description'));
$obj->setContentSubDescription($response->getHeader('Content-Sub-Description'));
$obj->setContentId($response->getHeader('Content-ID'));
Expand Down
21 changes: 21 additions & 0 deletions src/Parsers/ParserType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace PHRETS\Parsers;

enum ParserType : string
{
case LOGIN = 'parser.login';
case OBJECT_SINGLE = 'parser.object.single';
case OBJECT_MULTIPLE = 'parser.object.multiple';
case SEARCH = 'parser.search';
case SEARCH_RECURSIVE = 'parser.search.recursive';
case METADATA_SYSTEM = 'parser.metadata.system';
case METADATA_RESOURCE = 'parser.metadata.resource';
case METADATA_CLASS = 'parser.metadata.class';
case METADATA_TABLE = 'parser.metadata.table';
case METADATA_OBJECT = 'parser.metadata.object';
case METADATA_LOOKUP = 'parser.metadata.lookup';
case METADATA_LOOKUPTYPE = 'parser.metadata.lookuptype';
case UPDATE = 'parser.update';
case OBJECT_POST = 'parser.object.post';
case XML = 'parser.xml';
}
6 changes: 3 additions & 3 deletions src/Parsers/Search/OneX.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use PHRETS\Http\Response;
use PHRETS\Models\Search\Record;
use PHRETS\Models\Search\Results;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;
use PHRETS\Strategies\Strategy;

class OneX
{
public function parse(Session $rets, Response $response, $parameters): Results
{
/** @var \PHRETS\Parsers\XML $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide(Strategy::PARSER_XML);
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::XML);
$xml = $parser->parse($response);

$rs = new Results();
Expand Down Expand Up @@ -62,7 +62,7 @@ protected function getDelimiter(Session $rets, $xml, $parameters): string
// assume tab delimited since it wasn't given
$rets->debug('Assuming TAB delimiter since none specified in response');

return chr('09');
return chr(9);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/Search/RecursiveOneX.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHRETS\Exceptions\AutomaticPaginationError;
use PHRETS\Http\Response;
use PHRETS\Models\Search\Results;
use PHRETS\Parsers\ParserType;
use PHRETS\Session;

class RecursiveOneX
Expand All @@ -18,7 +19,7 @@ public function parse(Session $rets, Response $response, $parameters): Results
// we're given the first response automatically, so parse this and start the recursion

/** @var \PHRETS\Parsers\Search\OneX $parser */
$parser = $rets->getConfiguration()->getStrategy()->provide('parser.search');
$parser = $rets->getConfiguration()->getStrategy()->provide(ParserType::SEARCH);
$rs = $parser->parse($rets, $response, $parameters);

while ($this->continuePaginating($rets, $parameters, $rs)) {
Expand Down
Loading

0 comments on commit 967c066

Please sign in to comment.