Skip to content

Commit d56f538

Browse files
committed
General: add types
1 parent 0beb6e0 commit d56f538

29 files changed

+99
-124
lines changed

src/PHPDraft/In/ApibFileParser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
namespace PHPDraft\In;
1212

1313
use PHPDraft\Parse\ExecutionException;
14+
use Stringable;
1415

1516
/**
1617
* Class ApibFileParser.
1718
*/
18-
class ApibFileParser
19+
class ApibFileParser implements Stringable
1920
{
2021
/**
2122
* Complete API Blueprint.
@@ -56,7 +57,7 @@ public function __construct(string $filename = 'index.apib')
5657
*
5758
* @throws ExecutionException
5859
*
59-
* @return $this self reference.
60+
* @return self self reference.
6061
*/
6162
public function parse(): self
6263
{

src/PHPDraft/Model/Elements/BasicStructureElement.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace PHPDraft\Model\Elements;
1010

11+
use Stringable;
12+
1113
abstract class BasicStructureElement implements StructureElement
1214
{
1315
/**
@@ -39,7 +41,7 @@ abstract class BasicStructureElement implements StructureElement
3941
*
4042
* @var mixed
4143
*/
42-
public $value = null;
44+
public mixed $value = null;
4345
/**
4446
* Object status (required|optional).
4547
*
@@ -71,7 +73,7 @@ abstract class BasicStructureElement implements StructureElement
7173
* @param object|null $object An object to parse
7274
* @param string[] $dependencies Dependencies of this object
7375
*
74-
* @return StructureElement self reference
76+
* @return self self reference
7577
*/
7678
abstract public function parse(?object $object, array &$dependencies): StructureElement;
7779

@@ -181,6 +183,7 @@ public function string_value(bool $flat = false)
181183
if (is_subclass_of($this->value, BasicStructureElement::class) && $flat === true) {
182184
return is_array($this->value->value) ? array_keys($this->value->value)[0] : $this->value->value;
183185
}
186+
184187
return $this->value;
185188
}
186189

@@ -193,19 +196,10 @@ public function string_value(bool $flat = false)
193196
*/
194197
public function get_class(string $element): BasicStructureElement
195198
{
196-
switch ($element) {
197-
default:
198-
case 'object':
199-
$struct = $this->new_instance();
200-
break;
201-
case 'array':
202-
$struct = new ArrayStructureElement();
203-
break;
204-
case 'enum':
205-
$struct = new EnumStructureElement();
206-
break;
207-
}
208-
209-
return $struct;
199+
return match ($element) {
200+
default => $this->new_instance(),
201+
'array' => new ArrayStructureElement(),
202+
'enum' => new EnumStructureElement(),
203+
};
210204
}
211205
}

src/PHPDraft/Model/Elements/ElementStructureElement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPDraft\Model\Elements;
44

5+
use Stringable;
6+
57
class ElementStructureElement implements StructureElement
68
{
79
/**
@@ -23,7 +25,7 @@ class ElementStructureElement implements StructureElement
2325
*
2426
* @var mixed
2527
*/
26-
public $value = null;
28+
public mixed $value = null;
2729

2830
/**
2931
* Parse a JSON object to a structure.

src/PHPDraft/Model/Elements/EnumStructureElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EnumStructureElement extends BasicStructureElement
2222
* @param object|null $object APIB Item to parse
2323
* @param string[] $dependencies List of dependencies build
2424
*
25-
* @return $this
25+
* @return self
2626
*/
2727
public function parse(?object $object, array &$dependencies): StructureElement
2828
{

src/PHPDraft/Model/Elements/RequestBodyElement.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ public function print_request(?string $type = 'application/x-www-form-urlencoded
3535
}
3636
}
3737

38-
switch ($type) {
39-
case 'application/x-www-form-urlencoded':
40-
$return .= join('&', $list);
41-
break;
42-
default:
43-
$return .= join(PHP_EOL, $list);
44-
break;
45-
}
38+
$return .= match ($type) {
39+
'application/x-www-form-urlencoded' => join('&', $list),
40+
default => join(PHP_EOL, $list),
41+
};
4642

4743
$return .= '</code>';
4844

src/PHPDraft/Model/Elements/StructureElement.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
namespace PHPDraft\Model\Elements;
1414

15-
interface StructureElement
15+
use Stringable;
16+
17+
interface StructureElement extends Stringable
1618
{
1719
/**
1820
* Default data types.
@@ -31,13 +33,6 @@ interface StructureElement
3133
*/
3234
public function parse(?object $object, array &$dependencies): self;
3335

34-
/**
35-
* Print a string representation.
36-
*
37-
* @return string
38-
*/
39-
public function __toString(): string;
40-
4136

4237
/**
4338
* Get a string representation of the value.

src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSetupCorrectly(): void
5454
* @param mixed $value Value to set to the class
5555
* @param mixed $string_value Expected string representation
5656
*/
57-
public function testStringValue($value, $string_value): void
57+
public function testStringValue(mixed $value, mixed $string_value): void
5858
{
5959
$this->set_reflection_property_value('value', $value);
6060

@@ -65,6 +65,11 @@ public function testStringValue($value, $string_value): void
6565
$this->assertSame($string_value, $return);
6666
}
6767

68+
/**
69+
* Provide string values
70+
*
71+
* @return array
72+
*/
6873
public function stringValueProvider(): array
6974
{
7075
$return = [];

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class HTTPRequest implements Comparable
5858
*
5959
* @var mixed
6060
*/
61-
public $body = null;
61+
public mixed $body = null;
6262

6363
/**
6464
* Schema of the body of the request.
@@ -71,7 +71,7 @@ class HTTPRequest implements Comparable
7171
*
7272
* @var RequestBodyElement[]|RequestBodyElement
7373
*/
74-
public $struct = [];
74+
public mixed $struct = [];
7575
/**
7676
* Identifier for the request.
7777
*
@@ -95,7 +95,7 @@ public function __construct(Transition &$parent)
9595
*
9696
* @param object $object JSON object
9797
*
98-
* @return $this self-reference
98+
* @return self self-reference
9999
*/
100100
public function parse(object $object): self
101101
{
@@ -190,11 +190,9 @@ public function get_curl_command(string $base_url, array $additional = []): stri
190190
$type = $this->headers['Content-Type'] ?? null;
191191

192192
$options[] = '-X' . $this->method;
193-
if (is_null($this->body) || $this->body === []) {
194-
//NO-OP
195-
} elseif (is_string($this->body)) {
193+
if (is_string($this->body)) {
196194
$options[] = '--data-binary ' . escapeshellarg($this->body);
197-
} elseif (is_array($this->body)) {
195+
} elseif (is_array($this->body) && $this->body !== []) {
198196
$options[] = '--data-binary ' . escapeshellarg(join('', $this->body));
199197
} elseif (is_subclass_of($this->struct, StructureElement::class)) {
200198
foreach ($this->struct->value as $body) {

src/PHPDraft/Model/HTTPResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct(Transition $parent)
7676
*
7777
* @param object $object JSON object
7878
*
79-
* @return $this self-reference
79+
* @return self self-reference
8080
*/
8181
public function parse(object $object): self
8282
{

src/PHPDraft/Model/Resource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(Category &$parent)
4545
*
4646
* @param object $object JSON object
4747
*
48-
* @return $this self-reference
48+
* @return self self-reference
4949
*/
5050
public function parse(object $object): self
5151
{

0 commit comments

Comments
 (0)