Skip to content

Commit

Permalink
Parse: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Jan 16, 2022
1 parent 5e64f90 commit 5c09be5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/PHPDraft/Parse/BaseHtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ abstract class BaseHtmlGenerator
*
* @var int
*/
public $sorting;
public int $sorting;

/**
* JSON representation of an API Blueprint.
*
* @var stdClass
* @var object
*/
protected $object;
protected object $object;

/**
* Rendered HTML
*
* @var string
*/
protected $html;
protected string $html;

/**
* Constructor.
Expand Down
12 changes: 6 additions & 6 deletions src/PHPDraft/Parse/BaseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ abstract class BaseParser
/**
* The API Blueprint output (JSON).
*
* @var object
* @var object|null
*/
public $json;
public ?object $json;

/**
* Temp directory.
*
* @var string
*/
protected $tmp_dir;
protected string $tmp_dir;

/**
* The API Blueprint input.
*
* @var ApibFileParser
*/
protected $apib;
protected ApibFileParser $apib;

/**
* BaseParser constructor.
*
* @param ApibFileParser $apib API Blueprint text
*
* @return \PHPDraft\Parse\BaseParser
* @return self
*/
public function init(ApibFileParser $apib): self
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public function parseToJson(): object
*
* @return void
*/
abstract protected function parse();
abstract protected function parse(): void;

/**
* Check if a given parser is available.
Expand Down
17 changes: 13 additions & 4 deletions src/PHPDraft/Parse/Drafter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Drafter extends BaseParser
*
* @var string
*/
protected $drafter;
protected string $drafter;

/**
* ApibToJson constructor.
Expand All @@ -33,15 +33,19 @@ class Drafter extends BaseParser
public function init(ApibFileParser $apib): BaseParser
{
parent::init($apib);
$this->drafter = self::location();
$loc = self::location();
if ($loc === false) {
throw new \UnexpectedValueException("Could not find drafter location!");
}
$this->drafter = $loc;

return $this;
}

/**
* Return drafter location if found.
*
* @return bool|string
* @return false|string
*/
public static function location()
{
Expand All @@ -59,7 +63,12 @@ public static function location()
protected function parse(): void
{
shell_exec("{$this->drafter} {$this->tmp_dir}/index.apib -f json -o {$this->tmp_dir}/index.json 2> /dev/null");
$this->json = json_decode(file_get_contents($this->tmp_dir . '/index.json'));
$content = file_get_contents($this->tmp_dir . '/index.json');
if (!is_string($content)) {
throw new \RuntimeException('Could not read intermediary APIB file!');
}

$this->json = json_decode($content);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/PHPDraft/Parse/Tests/BaseParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ public function testSetupCorrectly(): void
$this->assertInstanceOf('\PHPDraft\In\ApibFileParser', $this->get_reflection_property_value('apib'));
}

/**
* Check if the JSON is empty before parsing
*
* @covers \PHPDraft\Parse\BaseParser::parseToJson()
*/
public function testPreRunStringIsEmpty(): void
{
$this->assertEmpty($this->class->json);
}

/**
* Check if parsing the APIB to JSON gives the expected result
*
Expand Down
10 changes: 0 additions & 10 deletions src/PHPDraft/Parse/Tests/DrafterAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ public function testAvailableSuccess(): void
$this->unmock_function('curl_exec');
}

/**
* Check if the JSON is empty before parsing
*
* @covers \PHPDraft\Parse\DrafterAPI::parseToJson()
*/
public function testPreRunStringIsEmpty(): void
{
$this->assertEmpty($this->class->json);
}

/**
* Check if parsing the fails without drafter
*
Expand Down
10 changes: 0 additions & 10 deletions src/PHPDraft/Parse/Tests/DrafterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ public function testSetupCorrectly(): void
$this->assertInstanceOf('\PHPDraft\In\ApibFileParser', $this->get_reflection_property_value('apib'));
}

/**
* Check if the JSON is empty before parsing
*
* @covers \PHPDraft\Parse\Drafter::parseToJson()
*/
public function testPreRunStringIsEmpty(): void
{
$this->assertEmpty($this->class->json);
}

/**
* Check if parsing the APIB to JSON gives the expected result
*/
Expand Down
5 changes: 2 additions & 3 deletions src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public function tearDown(): void
*/
public function testSetupCorrectly(): void
{
$property = $this->reflection->getProperty('object');
$property->setAccessible(true);
$this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')), $property->getValue($this->class));
$json = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json'));
$this->assertEquals($json, $this->get_reflection_property_value('object'));
}

/**
Expand Down

0 comments on commit 5c09be5

Please sign in to comment.