Skip to content

Commit 5c09be5

Browse files
committed
Parse: add types
1 parent 5e64f90 commit 5c09be5

File tree

7 files changed

+25
-47
lines changed

7 files changed

+25
-47
lines changed

src/PHPDraft/Parse/BaseHtmlGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ abstract class BaseHtmlGenerator
2222
*
2323
* @var int
2424
*/
25-
public $sorting;
25+
public int $sorting;
2626

2727
/**
2828
* JSON representation of an API Blueprint.
2929
*
30-
* @var stdClass
30+
* @var object
3131
*/
32-
protected $object;
32+
protected object $object;
3333

3434
/**
3535
* Rendered HTML
3636
*
3737
* @var string
3838
*/
39-
protected $html;
39+
protected string $html;
4040

4141
/**
4242
* Constructor.

src/PHPDraft/Parse/BaseParser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,30 @@ abstract class BaseParser
2424
/**
2525
* The API Blueprint output (JSON).
2626
*
27-
* @var object
27+
* @var object|null
2828
*/
29-
public $json;
29+
public ?object $json;
3030

3131
/**
3232
* Temp directory.
3333
*
3434
* @var string
3535
*/
36-
protected $tmp_dir;
36+
protected string $tmp_dir;
3737

3838
/**
3939
* The API Blueprint input.
4040
*
4141
* @var ApibFileParser
4242
*/
43-
protected $apib;
43+
protected ApibFileParser $apib;
4444

4545
/**
4646
* BaseParser constructor.
4747
*
4848
* @param ApibFileParser $apib API Blueprint text
4949
*
50-
* @return \PHPDraft\Parse\BaseParser
50+
* @return self
5151
*/
5252
public function init(ApibFileParser $apib): self
5353
{
@@ -114,7 +114,7 @@ public function parseToJson(): object
114114
*
115115
* @return void
116116
*/
117-
abstract protected function parse();
117+
abstract protected function parse(): void;
118118

119119
/**
120120
* Check if a given parser is available.

src/PHPDraft/Parse/Drafter.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Drafter extends BaseParser
2121
*
2222
* @var string
2323
*/
24-
protected $drafter;
24+
protected string $drafter;
2525

2626
/**
2727
* ApibToJson constructor.
@@ -33,15 +33,19 @@ class Drafter extends BaseParser
3333
public function init(ApibFileParser $apib): BaseParser
3434
{
3535
parent::init($apib);
36-
$this->drafter = self::location();
36+
$loc = self::location();
37+
if ($loc === false) {
38+
throw new \UnexpectedValueException("Could not find drafter location!");
39+
}
40+
$this->drafter = $loc;
3741

3842
return $this;
3943
}
4044

4145
/**
4246
* Return drafter location if found.
4347
*
44-
* @return bool|string
48+
* @return false|string
4549
*/
4650
public static function location()
4751
{
@@ -59,7 +63,12 @@ public static function location()
5963
protected function parse(): void
6064
{
6165
shell_exec("{$this->drafter} {$this->tmp_dir}/index.apib -f json -o {$this->tmp_dir}/index.json 2> /dev/null");
62-
$this->json = json_decode(file_get_contents($this->tmp_dir . '/index.json'));
66+
$content = file_get_contents($this->tmp_dir . '/index.json');
67+
if (!is_string($content)) {
68+
throw new \RuntimeException('Could not read intermediary APIB file!');
69+
}
70+
71+
$this->json = json_decode($content);
6372
}
6473

6574
/**

src/PHPDraft/Parse/Tests/BaseParserTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ public function testSetupCorrectly(): void
7474
$this->assertInstanceOf('\PHPDraft\In\ApibFileParser', $this->get_reflection_property_value('apib'));
7575
}
7676

77-
/**
78-
* Check if the JSON is empty before parsing
79-
*
80-
* @covers \PHPDraft\Parse\BaseParser::parseToJson()
81-
*/
82-
public function testPreRunStringIsEmpty(): void
83-
{
84-
$this->assertEmpty($this->class->json);
85-
}
86-
8777
/**
8878
* Check if parsing the APIB to JSON gives the expected result
8979
*

src/PHPDraft/Parse/Tests/DrafterAPITest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,6 @@ public function testAvailableSuccess(): void
111111
$this->unmock_function('curl_exec');
112112
}
113113

114-
/**
115-
* Check if the JSON is empty before parsing
116-
*
117-
* @covers \PHPDraft\Parse\DrafterAPI::parseToJson()
118-
*/
119-
public function testPreRunStringIsEmpty(): void
120-
{
121-
$this->assertEmpty($this->class->json);
122-
}
123-
124114
/**
125115
* Check if parsing the fails without drafter
126116
*

src/PHPDraft/Parse/Tests/DrafterTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ public function testSetupCorrectly(): void
8383
$this->assertInstanceOf('\PHPDraft\In\ApibFileParser', $this->get_reflection_property_value('apib'));
8484
}
8585

86-
/**
87-
* Check if the JSON is empty before parsing
88-
*
89-
* @covers \PHPDraft\Parse\Drafter::parseToJson()
90-
*/
91-
public function testPreRunStringIsEmpty(): void
92-
{
93-
$this->assertEmpty($this->class->json);
94-
}
95-
9686
/**
9787
* Check if parsing the APIB to JSON gives the expected result
9888
*/

src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ public function tearDown(): void
5757
*/
5858
public function testSetupCorrectly(): void
5959
{
60-
$property = $this->reflection->getProperty('object');
61-
$property->setAccessible(true);
62-
$this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')), $property->getValue($this->class));
60+
$json = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json'));
61+
$this->assertEquals($json, $this->get_reflection_property_value('object'));
6362
}
6463

6564
/**

0 commit comments

Comments
 (0)