diff --git a/src/Input.php b/src/Input.php index 266fa7f..6fe3b04 100644 --- a/src/Input.php +++ b/src/Input.php @@ -16,6 +16,10 @@ use Gt\Input\InputData\FileUploadInputData; use Gt\Input\InputData\QueryStringInputData; +/** + * @implements ArrayAccess + * @implements Iterator + */ class Input implements ArrayAccess, Countable, Iterator { use InputValueGetter; use KeyValueArrayAccess; @@ -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 $get + * @param array $post + * @param array> $files + * @param string $bodyPath + */ public function __construct( array $get = [], array $post = [], @@ -89,7 +95,6 @@ public function add(string $key, InputDatum $datum, string $method):void { default: throw new InvalidInputMethodException($method); - break; } $this->parameters = new CombinedInputData( @@ -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 $matches */ - public function when(...$matches):Trigger { + public function when(array|string...$matches):Trigger { $trigger = new Trigger($this); $trigger->when($matches); return $trigger; @@ -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); } diff --git a/src/InputData/AbstractInputData.php b/src/InputData/AbstractInputData.php index 6e31ecf..fd6e610 100644 --- a/src/InputData/AbstractInputData.php +++ b/src/InputData/AbstractInputData.php @@ -7,6 +7,10 @@ use Iterator; use Gt\Input\InputData\Datum\InputDatum; +/** + * @implements ArrayAccess + * @implements Iterator + */ abstract class AbstractInputData implements ArrayAccess, Countable, Iterator { use InputValueGetter; use KeyValueArrayAccess; @@ -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; } } diff --git a/src/InputData/Datum/FailedFileUpload.php b/src/InputData/Datum/FailedFileUpload.php index a3dfe1d..213b025 100644 --- a/src/InputData/Datum/FailedFileUpload.php +++ b/src/InputData/Datum/FailedFileUpload.php @@ -2,7 +2,7 @@ namespace Gt\Input\InputData\Datum; class FailedFileUpload extends FileUpload { - protected $errorCode; + protected int $errorCode; public function __construct( string $originalFilename, @@ -68,4 +68,4 @@ public function getErrorMessage():string { return $msg; } -} \ No newline at end of file +} diff --git a/src/InputData/Datum/FileUpload.php b/src/InputData/Datum/FileUpload.php index 4816643..25aeacd 100644 --- a/src/InputData/Datum/FileUpload.php +++ b/src/InputData/Datum/FileUpload.php @@ -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); } @@ -195,4 +195,4 @@ public function getClientFilename() { public function getClientMediaType() { return $this->getMimeType(); } -} \ No newline at end of file +} diff --git a/src/InputData/Datum/InputDatum.php b/src/InputData/Datum/InputDatum.php index b0d44e0..18d7d38 100644 --- a/src/InputData/Datum/InputDatum.php +++ b/src/InputData/Datum/InputDatum.php @@ -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; } -} \ No newline at end of file +} diff --git a/src/InputData/Datum/MultipleInputDatum.php b/src/InputData/Datum/MultipleInputDatum.php index 0205605..c9cb638 100644 --- a/src/InputData/Datum/MultipleInputDatum.php +++ b/src/InputData/Datum/MultipleInputDatum.php @@ -5,10 +5,14 @@ use Gt\Input\ImmutableObjectModificationException; use Iterator; +/** + * @implements ArrayAccess + * @implements Iterator + */ 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; @@ -18,6 +22,7 @@ public function __toString():string { return implode(", ", $this->value); } + /** @return array */ public function toArray():array { $array = []; @@ -64,14 +69,14 @@ 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]; @@ -79,7 +84,7 @@ public function offsetGet($offset):mixed { /** * @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 { @@ -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(); diff --git a/src/InputData/FileUploadInputData.php b/src/InputData/FileUploadInputData.php index 87736d9..84f39ac 100644 --- a/src/InputData/FileUploadInputData.php +++ b/src/InputData/FileUploadInputData.php @@ -5,7 +5,9 @@ use Gt\Input\InputData\Datum\FileUpload; class FileUploadInputData extends InputData { - + /** + * @param array> $files + */ public function __construct(array $files) { $files = $this->normalizeArray($files); $data = $this->createData($files); @@ -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> $files + * @return array>> */ - 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)) { @@ -35,6 +40,10 @@ protected function normalizeArray($files):array { return $files; } + /** + * @param array>> $files + * @return array> + */ protected function createData(array $files):array { $datumList = []; @@ -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; } -} \ No newline at end of file +} diff --git a/src/InputData/InputData.php b/src/InputData/InputData.php index 7ad7135..b33017d 100644 --- a/src/InputData/InputData.php +++ b/src/InputData/InputData.php @@ -6,6 +6,7 @@ use Gt\Input\InputValueGetter; class InputData extends AbstractInputData { + /** @param iterable|iterable>|iterable...$sources */ public function __construct(iterable...$sources) { $this->parameters = []; @@ -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); @@ -53,10 +54,12 @@ public function removeExcept(string...$keys):self { return $this; } + /** @return array */ public function getKeys():array { return array_keys($this->parameters); } + /** @return array> */ public function asArray():array { $array = []; diff --git a/src/InputData/InputDataFactory.php b/src/InputData/InputDataFactory.php index 6d8720f..c337d02 100644 --- a/src/InputData/InputDataFactory.php +++ b/src/InputData/InputDataFactory.php @@ -5,6 +5,10 @@ use Gt\Input\WithWithoutClashingException; class InputDataFactory { + /** + * @param array|array $with + * @param array|array $without + */ public static function create(Input $input, array $with = [], array $without = []):InputData { $data = $input->getAll(); @@ -34,4 +38,4 @@ public static function create(Input $input, array $with = [], array $without = [ return $data; } -} \ No newline at end of file +} diff --git a/src/InputData/KeyValueArrayAccess.php b/src/InputData/KeyValueArrayAccess.php index 78c7e8c..7102f83 100644 --- a/src/InputData/KeyValueArrayAccess.php +++ b/src/InputData/KeyValueArrayAccess.php @@ -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); } @@ -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)) { diff --git a/src/InputValueGetter.php b/src/InputValueGetter.php index a6fe3f2..c399505 100644 --- a/src/InputValueGetter.php +++ b/src/InputValueGetter.php @@ -13,10 +13,9 @@ use TypeError; trait InputValueGetter { - /** @var FileUploadInputData */ - protected $fileUploadParameters; - /** @var CombinedInputData */ - protected $parameters; + protected FileUploadInputData $fileUploadParameters; + /** @var array|CombinedInputData */ + protected array|CombinedInputData $parameters; public function getString(string $key):?string { return $this->get($key); @@ -69,9 +68,7 @@ public function getFile(string $key):FileUpload { } } - /** - * @return FileUpload[] - */ + /** @return MultipleInputDatum */ public function getMultipleFile(string $key):MultipleInputDatum { return $this->get($key); } @@ -104,9 +101,7 @@ public function getDateTime( return $dateTime; } - /** - * @return DateTime[] - */ + /** @return MultipleInputDatum */ public function getMultipleDateTime(string $key):MultipleInputDatum { return $this->get($key); } diff --git a/src/Trigger/Callback.php b/src/Trigger/Callback.php index 5ff3e7b..1d95134 100644 --- a/src/Trigger/Callback.php +++ b/src/Trigger/Callback.php @@ -6,9 +6,10 @@ class Callback { /** @var callable */ private $callback; - private $args; + /** @var array */ + private array $args; - public function __construct(callable $callback, ...$args) { + public function __construct(callable $callback, string...$args) { $this->callback = $callback; $this->args = $args; } @@ -23,4 +24,4 @@ public function call(InputData $data):void { call_user_func_array($this->callback, $parameters); } -} \ No newline at end of file +} diff --git a/src/Trigger/Trigger.php b/src/Trigger/Trigger.php index 466e8bd..3112270 100644 --- a/src/Trigger/Trigger.php +++ b/src/Trigger/Trigger.php @@ -6,17 +6,19 @@ use Gt\Input\InputData\InputDataFactory; class Trigger { - /** @var Input */ - protected $input; - - protected $matches; - protected $keyMatches; - protected $with; - protected $without; - /** @var Callback[] */ - protected $callbacks; - /** @var ?bool */ - protected $hasFired; + protected Input $input; + + /** @var array> */ + protected array $matches; + /** @var array */ + protected array $keyMatches; + /** @var array */ + protected array $with; + /** @var array */ + protected array $without; + /** @var array */ + protected array $callbacks; + protected ?bool $hasFired; public function __construct(Input $input) { $this->input = $input; @@ -29,7 +31,8 @@ public function __construct(Input $input) { $this->hasFired = null; } - public function when(...$matches):self { + /** @param string|array...$matches */ + public function when(string|array...$matches):self { $matches = $this->flattenArray($matches); foreach($matches as $key => $match) { @@ -85,13 +88,13 @@ protected function setKeyTrigger(string $key):self { return $this; } - public function call(callable $callback, ...$args):self { + public function call(callable $callback, string...$args):self { $this->callbacks []= new Callback($callback, ...$args); $this->hasFired = $this->fire(); return $this; } - public function or(callable $callback, ...$args):self { + public function or(callable $callback, string...$args):self { if(is_null($this->hasFired)) { throw new CallOrOutOfSequenceException(); } @@ -134,7 +137,7 @@ public function fire():bool { return $fired; } - protected function callCallbacks() { + protected function callCallbacks():void { $fields = InputDataFactory::create( $this->input, $this->with, @@ -147,6 +150,10 @@ protected function callCallbacks() { } } + /** + * @param array>> $array + * @return array + */ protected function flattenArray(array $array):array { $result = []; @@ -179,4 +186,4 @@ protected function flattenArray(array $array):array { return $result; } -} \ No newline at end of file +}