Skip to content

Commit d2d2701

Browse files
author
Greg Bowler
authored
Type improvements (#190)
* fix: deprecation notices * stan: type improvements
1 parent 020b3b7 commit d2d2701

13 files changed

+110
-63
lines changed

src/Input.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use Gt\Input\InputData\FileUploadInputData;
1717
use Gt\Input\InputData\QueryStringInputData;
1818

19+
/**
20+
* @implements ArrayAccess<string, ?string>
21+
* @implements Iterator<string, ?string>
22+
*/
1923
class Input implements ArrayAccess, Countable, Iterator {
2024
use InputValueGetter;
2125
use KeyValueArrayAccess;
@@ -27,14 +31,16 @@ class Input implements ArrayAccess, Countable, Iterator {
2731
const DATA_FILES = "files";
2832
const DATA_COMBINED = "combined";
2933

30-
/** @var BodyStream */
31-
protected $bodyStream;
32-
33-
/** @var QueryStringInputData */
34-
protected $queryStringParameters;
35-
/** @var BodyInputData */
36-
protected $bodyParameters;
34+
protected BodyStream $bodyStream;
35+
protected QueryStringInputData $queryStringParameters;
36+
protected BodyInputData $bodyParameters;
3737

38+
/**
39+
* @param array<string, string> $get
40+
* @param array<string, string> $post
41+
* @param array<string, array<string, string>> $files
42+
* @param string $bodyPath
43+
*/
3844
public function __construct(
3945
array $get = [],
4046
array $post = [],
@@ -89,7 +95,6 @@ public function add(string $key, InputDatum $datum, string $method):void {
8995

9096
default:
9197
throw new InvalidInputMethodException($method);
92-
break;
9398
}
9499

95100
$this->parameters = new CombinedInputData(
@@ -217,8 +222,10 @@ public function do(string $match):Trigger {
217222
*
218223
* $matches is an associative array, where the key is a request variable's name and the
219224
* value is the request variable's value to match.
225+
*
226+
* @param array<string, string>|string $matches
220227
*/
221-
public function when(...$matches):Trigger {
228+
public function when(array|string...$matches):Trigger {
222229
$trigger = new Trigger($this);
223230
$trigger->when($matches);
224231
return $trigger;
@@ -251,7 +258,7 @@ public function withAll():Trigger {
251258
return $this->newTrigger("withAll");
252259
}
253260

254-
protected function newTrigger(string $functionName, ...$args):Trigger {
261+
protected function newTrigger(string $functionName, string...$args):Trigger {
255262
$trigger = new Trigger($this);
256263
return $trigger->$functionName(...$args);
257264
}

src/InputData/AbstractInputData.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Iterator;
88
use Gt\Input\InputData\Datum\InputDatum;
99

10+
/**
11+
* @implements ArrayAccess<string, string|InputDatum>
12+
* @implements Iterator<string, string|InputDatum>
13+
*/
1014
abstract class AbstractInputData implements ArrayAccess, Countable, Iterator {
1115
use InputValueGetter;
1216
use KeyValueArrayAccess;
@@ -33,12 +37,12 @@ public function hasValue(string $key):bool {
3337
}
3438

3539
protected function set(string $key, InputDatum $value):void {
36-
$this->parameters[$key] = $value;
40+
$this->parameters[$key] = (string)$value;
3741
}
3842

39-
public function withKeyValue(string $key, InputDatum $value):self {
43+
public function withKeyValue(string $key, InputDatum $value):static {
4044
$clone = clone($this);
41-
$clone->parameters[$key] = $value;
45+
$clone->parameters[$key] = (string)$value;
4246
return $clone;
4347
}
4448
}

src/InputData/Datum/FailedFileUpload.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Gt\Input\InputData\Datum;
33

44
class FailedFileUpload extends FileUpload {
5-
protected $errorCode;
5+
protected int $errorCode;
66

77
public function __construct(
88
string $originalFilename,
@@ -68,4 +68,4 @@ public function getErrorMessage():string {
6868

6969
return $msg;
7070
}
71-
}
71+
}

src/InputData/Datum/FileUpload.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getStream():StreamInterface {
111111
* @throws \RuntimeException on any error during the move operation, or on
112112
* the second or subsequent call to the method.
113113
*/
114-
public function moveTo($targetPath) {
114+
public function moveTo($targetPath):void {
115115
if(!is_uploaded_file($this->tempFilePath)) {
116116
throw new UploadedFileSecurityException($this->tempFilePath);
117117
}
@@ -195,4 +195,4 @@ public function getClientFilename() {
195195
public function getClientMediaType() {
196196
return $this->getMimeType();
197197
}
198-
}
198+
}

src/InputData/Datum/InputDatum.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
namespace Gt\Input\InputData\Datum;
33

44
class InputDatum {
5-
protected $value;
5+
protected mixed $value;
66

7-
public function __construct($value) {
7+
public function __construct(mixed $value) {
88
$this->value = $value;
99
}
1010

1111
public function __toString():string {
1212
return $this->value;
1313
}
14-
}
14+
}

src/InputData/Datum/MultipleInputDatum.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
use Gt\Input\ImmutableObjectModificationException;
66
use Iterator;
77

8+
/**
9+
* @implements ArrayAccess<string|int, mixed>
10+
* @implements Iterator<string|int, mixed>
11+
*/
812
class MultipleInputDatum extends InputDatum implements ArrayAccess, Iterator {
9-
protected $iteratorKey;
13+
protected int $iteratorKey;
1014

11-
public function __construct($value) {
15+
public function __construct(mixed $value) {
1216
parent::__construct($value);
1317

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

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

@@ -64,22 +69,22 @@ public function rewind():void {
6469
}
6570

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

7378
/** @link http://php.net/manual/en/arrayaccess.offsetget.php
74-
* @param string $offset
79+
* @param string|int $offset
7580
*/
7681
public function offsetGet($offset):mixed {
7782
return $this->value[$offset];
7883
}
7984

8085
/**
8186
* @link http://php.net/manual/en/arrayaccess.offsetset.php
82-
* @param string $offset
87+
* @param string|int $offset
8388
* @param string $value
8489
*/
8590
public function offsetSet($offset, $value):void {
@@ -88,6 +93,7 @@ public function offsetSet($offset, $value):void {
8893

8994
/**
9095
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
96+
* @param string|int $offset
9197
*/
9298
public function offsetUnset($offset):void {
9399
throw new ImmutableObjectModificationException();

src/InputData/FileUploadInputData.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Gt\Input\InputData\Datum\FileUpload;
66

77
class FileUploadInputData extends InputData {
8-
8+
/**
9+
* @param array<string, string|array<string, string>> $files
10+
*/
911
public function __construct(array $files) {
1012
$files = $this->normalizeArray($files);
1113
$data = $this->createData($files);
@@ -22,8 +24,11 @@ public function __construct(array $files) {
2224
* + size
2325
* Each key's value is string, unless the request parameter name ends with [], in which case
2426
* each value is another array. This function normalises the array to the latter.
27+
*
28+
* @param array<string, string|array<string, string>> $files
29+
* @return array<string, array<string, array<string>>>
2530
*/
26-
protected function normalizeArray($files):array {
31+
protected function normalizeArray(array $files):array {
2732
foreach($files as $parameterName => $fileDetailArray) {
2833
foreach($fileDetailArray as $key => $value) {
2934
if(!is_array($value)) {
@@ -35,6 +40,10 @@ protected function normalizeArray($files):array {
3540
return $files;
3641
}
3742

43+
/**
44+
* @param array<string, array<string, array<string>>> $files
45+
* @return array<string, array<FileUpload>>
46+
*/
3847
protected function createData(array $files):array {
3948
$datumList = [];
4049

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

52-
if($details["error"][$i] === UPLOAD_ERR_OK) {
53-
$datumList[$inputName] []= new FileUpload(...$params);
61+
if($details["error"][$i] == UPLOAD_ERR_OK) {
62+
array_push(
63+
$datumList[$inputName],
64+
new FileUpload(...$params)
65+
);
5466
}
5567
else {
5668
$params []= (int)$details["error"][$i];
57-
$datumList[$inputName] []= new FailedFileUpload(...$params);
69+
array_push(
70+
$datumList[$inputName],
71+
new FailedFileUpload(...$params)
72+
);
5873
}
5974
}
6075
}
76+
6177
return $datumList;
6278
}
63-
}
79+
}

src/InputData/InputData.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Gt\Input\InputValueGetter;
77

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

@@ -15,7 +16,7 @@ public function __construct(iterable...$sources) {
1516
&& isset($value[0])) {
1617
$value = new MultipleInputDatum($value);
1718
}
18-
else if(!$value instanceof InputDatum) {
19+
else {
1920
$value = new InputDatum($value);
2021
}
2122
$this->add($key, $value);
@@ -53,10 +54,12 @@ public function removeExcept(string...$keys):self {
5354
return $this;
5455
}
5556

57+
/** @return array<string> */
5658
public function getKeys():array {
5759
return array_keys($this->parameters);
5860
}
5961

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

src/InputData/InputDataFactory.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Gt\Input\WithWithoutClashingException;
66

77
class InputDataFactory {
8+
/**
9+
* @param array<string>|array<string, string> $with
10+
* @param array<string>|array<string, string> $without
11+
*/
812
public static function create(Input $input, array $with = [], array $without = []):InputData {
913
$data = $input->getAll();
1014

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

3539
return $data;
3640
}
37-
}
41+
}

src/InputData/KeyValueArrayAccess.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function offsetGet($offset):mixed {
1313
if($this instanceof FileUploadInputData) {
1414
return $this->getFile($offset);
1515
}
16-
elseif($this instanceof Input || $this instanceof InputData) {
16+
elseif(is_a($this, Input::class) || is_a($this, InputData::class)) {
1717
if($this->contains($offset)) {
1818
return $this->get($offset);
1919
}
@@ -22,6 +22,10 @@ public function offsetGet($offset):mixed {
2222
return null;
2323
}
2424

25+
/**
26+
* @param string $offset
27+
* @param string|InputDatum $value
28+
*/
2529
public function offsetSet($offset, $value):void {
2630
if($this->parameters instanceof InputData) {
2731
if(is_string($value)) {

src/InputValueGetter.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
use TypeError;
1414

1515
trait InputValueGetter {
16-
/** @var FileUploadInputData */
17-
protected $fileUploadParameters;
18-
/** @var CombinedInputData */
19-
protected $parameters;
16+
protected FileUploadInputData $fileUploadParameters;
17+
/** @var array<string, string>|CombinedInputData */
18+
protected array|CombinedInputData $parameters;
2019

2120
public function getString(string $key):?string {
2221
return $this->get($key);
@@ -69,9 +68,7 @@ public function getFile(string $key):FileUpload {
6968
}
7069
}
7170

72-
/**
73-
* @return FileUpload[]
74-
*/
71+
/** @return MultipleInputDatum<FileUpload> */
7572
public function getMultipleFile(string $key):MultipleInputDatum {
7673
return $this->get($key);
7774
}
@@ -104,9 +101,7 @@ public function getDateTime(
104101
return $dateTime;
105102
}
106103

107-
/**
108-
* @return DateTime[]
109-
*/
104+
/** @return MultipleInputDatum<DateTime> */
110105
public function getMultipleDateTime(string $key):MultipleInputDatum {
111106
return $this->get($key);
112107
}

src/Trigger/Callback.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
class Callback {
77
/** @var callable */
88
private $callback;
9-
private $args;
9+
/** @var array<string> */
10+
private array $args;
1011

11-
public function __construct(callable $callback, ...$args) {
12+
public function __construct(callable $callback, string...$args) {
1213
$this->callback = $callback;
1314
$this->args = $args;
1415
}
@@ -23,4 +24,4 @@ public function call(InputData $data):void {
2324

2425
call_user_func_array($this->callback, $parameters);
2526
}
26-
}
27+
}

0 commit comments

Comments
 (0)