Skip to content

Commit

Permalink
Type improvements (#190)
Browse files Browse the repository at this point in the history
* fix: deprecation notices

* stan: type improvements
  • Loading branch information
g105b authored Feb 26, 2022
1 parent 020b3b7 commit d2d2701
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 63 deletions.
27 changes: 17 additions & 10 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Gt\Input\InputData\FileUploadInputData;
use Gt\Input\InputData\QueryStringInputData;

/**
* @implements ArrayAccess<string, ?string>
* @implements Iterator<string, ?string>
*/
class Input implements ArrayAccess, Countable, Iterator {
use InputValueGetter;
use KeyValueArrayAccess;
Expand All @@ -27,14 +31,16 @@ class Input implements ArrayAccess, Countable, Iterator {
const DATA_FILES = "files";
const DATA_COMBINED = "combined";

/** @var BodyStream */
protected $bodyStream;

/** @var QueryStringInputData */
protected $queryStringParameters;
/** @var BodyInputData */
protected $bodyParameters;
protected BodyStream $bodyStream;
protected QueryStringInputData $queryStringParameters;
protected BodyInputData $bodyParameters;

/**
* @param array<string, string> $get
* @param array<string, string> $post
* @param array<string, array<string, string>> $files
* @param string $bodyPath
*/
public function __construct(
array $get = [],
array $post = [],
Expand Down Expand Up @@ -89,7 +95,6 @@ public function add(string $key, InputDatum $datum, string $method):void {

default:
throw new InvalidInputMethodException($method);
break;
}

$this->parameters = new CombinedInputData(
Expand Down Expand Up @@ -217,8 +222,10 @@ public function do(string $match):Trigger {
*
* $matches is an associative array, where the key is a request variable's name and the
* value is the request variable's value to match.
*
* @param array<string, string>|string $matches
*/
public function when(...$matches):Trigger {
public function when(array|string...$matches):Trigger {
$trigger = new Trigger($this);
$trigger->when($matches);
return $trigger;
Expand Down Expand Up @@ -251,7 +258,7 @@ public function withAll():Trigger {
return $this->newTrigger("withAll");
}

protected function newTrigger(string $functionName, ...$args):Trigger {
protected function newTrigger(string $functionName, string...$args):Trigger {
$trigger = new Trigger($this);
return $trigger->$functionName(...$args);
}
Expand Down
10 changes: 7 additions & 3 deletions src/InputData/AbstractInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Iterator;
use Gt\Input\InputData\Datum\InputDatum;

/**
* @implements ArrayAccess<string, string|InputDatum>
* @implements Iterator<string, string|InputDatum>
*/
abstract class AbstractInputData implements ArrayAccess, Countable, Iterator {
use InputValueGetter;
use KeyValueArrayAccess;
Expand All @@ -33,12 +37,12 @@ public function hasValue(string $key):bool {
}

protected function set(string $key, InputDatum $value):void {
$this->parameters[$key] = $value;
$this->parameters[$key] = (string)$value;
}

public function withKeyValue(string $key, InputDatum $value):self {
public function withKeyValue(string $key, InputDatum $value):static {
$clone = clone($this);
$clone->parameters[$key] = $value;
$clone->parameters[$key] = (string)$value;
return $clone;
}
}
4 changes: 2 additions & 2 deletions src/InputData/Datum/FailedFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Gt\Input\InputData\Datum;

class FailedFileUpload extends FileUpload {
protected $errorCode;
protected int $errorCode;

public function __construct(
string $originalFilename,
Expand Down Expand Up @@ -68,4 +68,4 @@ public function getErrorMessage():string {

return $msg;
}
}
}
4 changes: 2 additions & 2 deletions src/InputData/Datum/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function getStream():StreamInterface {
* @throws \RuntimeException on any error during the move operation, or on
* the second or subsequent call to the method.
*/
public function moveTo($targetPath) {
public function moveTo($targetPath):void {
if(!is_uploaded_file($this->tempFilePath)) {
throw new UploadedFileSecurityException($this->tempFilePath);
}
Expand Down Expand Up @@ -195,4 +195,4 @@ public function getClientFilename() {
public function getClientMediaType() {
return $this->getMimeType();
}
}
}
6 changes: 3 additions & 3 deletions src/InputData/Datum/InputDatum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
namespace Gt\Input\InputData\Datum;

class InputDatum {
protected $value;
protected mixed $value;

public function __construct($value) {
public function __construct(mixed $value) {
$this->value = $value;
}

public function __toString():string {
return $this->value;
}
}
}
16 changes: 11 additions & 5 deletions src/InputData/Datum/MultipleInputDatum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
use Gt\Input\ImmutableObjectModificationException;
use Iterator;

/**
* @implements ArrayAccess<string|int, mixed>
* @implements Iterator<string|int, mixed>
*/
class MultipleInputDatum extends InputDatum implements ArrayAccess, Iterator {
protected $iteratorKey;
protected int $iteratorKey;

public function __construct($value) {
public function __construct(mixed $value) {
parent::__construct($value);

$this->iteratorKey = 0;
Expand All @@ -18,6 +22,7 @@ public function __toString():string {
return implode(", ", $this->value);
}

/** @return array<string, string> */
public function toArray():array {
$array = [];

Expand Down Expand Up @@ -64,22 +69,22 @@ public function rewind():void {
}

/** @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param string $offset
* @param string|int $offset
*/
public function offsetExists($offset):bool {
return isset($this->value[$offset]);
}

/** @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param string $offset
* @param string|int $offset
*/
public function offsetGet($offset):mixed {
return $this->value[$offset];
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param string $offset
* @param string|int $offset
* @param string $value
*/
public function offsetSet($offset, $value):void {
Expand All @@ -88,6 +93,7 @@ public function offsetSet($offset, $value):void {

/**
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param string|int $offset
*/
public function offsetUnset($offset):void {
throw new ImmutableObjectModificationException();
Expand Down
28 changes: 22 additions & 6 deletions src/InputData/FileUploadInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Gt\Input\InputData\Datum\FileUpload;

class FileUploadInputData extends InputData {

/**
* @param array<string, string|array<string, string>> $files
*/
public function __construct(array $files) {
$files = $this->normalizeArray($files);
$data = $this->createData($files);
Expand All @@ -22,8 +24,11 @@ public function __construct(array $files) {
* + size
* Each key's value is string, unless the request parameter name ends with [], in which case
* each value is another array. This function normalises the array to the latter.
*
* @param array<string, string|array<string, string>> $files
* @return array<string, array<string, array<string>>>
*/
protected function normalizeArray($files):array {
protected function normalizeArray(array $files):array {
foreach($files as $parameterName => $fileDetailArray) {
foreach($fileDetailArray as $key => $value) {
if(!is_array($value)) {
Expand All @@ -35,6 +40,10 @@ protected function normalizeArray($files):array {
return $files;
}

/**
* @param array<string, array<string, array<string>>> $files
* @return array<string, array<FileUpload>>
*/
protected function createData(array $files):array {
$datumList = [];

Expand All @@ -49,15 +58,22 @@ protected function createData(array $files):array {
$details["tmp_name"][$i],
];

if($details["error"][$i] === UPLOAD_ERR_OK) {
$datumList[$inputName] []= new FileUpload(...$params);
if($details["error"][$i] == UPLOAD_ERR_OK) {
array_push(
$datumList[$inputName],
new FileUpload(...$params)
);
}
else {
$params []= (int)$details["error"][$i];
$datumList[$inputName] []= new FailedFileUpload(...$params);
array_push(
$datumList[$inputName],
new FailedFileUpload(...$params)
);
}
}
}

return $datumList;
}
}
}
5 changes: 4 additions & 1 deletion src/InputData/InputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gt\Input\InputValueGetter;

class InputData extends AbstractInputData {
/** @param iterable<string,string>|iterable<string, array<string>>|iterable<InputData>...$sources */
public function __construct(iterable...$sources) {
$this->parameters = [];

Expand All @@ -15,7 +16,7 @@ public function __construct(iterable...$sources) {
&& isset($value[0])) {
$value = new MultipleInputDatum($value);
}
else if(!$value instanceof InputDatum) {
else {
$value = new InputDatum($value);
}
$this->add($key, $value);
Expand Down Expand Up @@ -53,10 +54,12 @@ public function removeExcept(string...$keys):self {
return $this;
}

/** @return array<string> */
public function getKeys():array {
return array_keys($this->parameters);
}

/** @return array<string, string|array<string>> */
public function asArray():array {
$array = [];

Expand Down
6 changes: 5 additions & 1 deletion src/InputData/InputDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Gt\Input\WithWithoutClashingException;

class InputDataFactory {
/**
* @param array<string>|array<string, string> $with
* @param array<string>|array<string, string> $without
*/
public static function create(Input $input, array $with = [], array $without = []):InputData {
$data = $input->getAll();

Expand Down Expand Up @@ -34,4 +38,4 @@ public static function create(Input $input, array $with = [], array $without = [

return $data;
}
}
}
6 changes: 5 additions & 1 deletion src/InputData/KeyValueArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function offsetGet($offset):mixed {
if($this instanceof FileUploadInputData) {
return $this->getFile($offset);
}
elseif($this instanceof Input || $this instanceof InputData) {
elseif(is_a($this, Input::class) || is_a($this, InputData::class)) {
if($this->contains($offset)) {
return $this->get($offset);
}
Expand All @@ -22,6 +22,10 @@ public function offsetGet($offset):mixed {
return null;
}

/**
* @param string $offset
* @param string|InputDatum $value
*/
public function offsetSet($offset, $value):void {
if($this->parameters instanceof InputData) {
if(is_string($value)) {
Expand Down
15 changes: 5 additions & 10 deletions src/InputValueGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
use TypeError;

trait InputValueGetter {
/** @var FileUploadInputData */
protected $fileUploadParameters;
/** @var CombinedInputData */
protected $parameters;
protected FileUploadInputData $fileUploadParameters;
/** @var array<string, string>|CombinedInputData */
protected array|CombinedInputData $parameters;

public function getString(string $key):?string {
return $this->get($key);
Expand Down Expand Up @@ -69,9 +68,7 @@ public function getFile(string $key):FileUpload {
}
}

/**
* @return FileUpload[]
*/
/** @return MultipleInputDatum<FileUpload> */
public function getMultipleFile(string $key):MultipleInputDatum {
return $this->get($key);
}
Expand Down Expand Up @@ -104,9 +101,7 @@ public function getDateTime(
return $dateTime;
}

/**
* @return DateTime[]
*/
/** @return MultipleInputDatum<DateTime> */
public function getMultipleDateTime(string $key):MultipleInputDatum {
return $this->get($key);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Trigger/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
class Callback {
/** @var callable */
private $callback;
private $args;
/** @var array<string> */
private array $args;

public function __construct(callable $callback, ...$args) {
public function __construct(callable $callback, string...$args) {
$this->callback = $callback;
$this->args = $args;
}
Expand All @@ -23,4 +24,4 @@ public function call(InputData $data):void {

call_user_func_array($this->callback, $parameters);
}
}
}
Loading

0 comments on commit d2d2701

Please sign in to comment.