From 51e2aab2edfbabbc49dc4b4943dab2d732b692df Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 16 Jan 2022 16:53:14 +0100 Subject: [PATCH 1/5] In: add types --- src/PHPDraft/In/ApibFileParser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PHPDraft/In/ApibFileParser.php b/src/PHPDraft/In/ApibFileParser.php index d10fb110..86684336 100644 --- a/src/PHPDraft/In/ApibFileParser.php +++ b/src/PHPDraft/In/ApibFileParser.php @@ -22,21 +22,21 @@ class ApibFileParser * * @var string */ - protected $full_apib; + protected string $full_apib; /** * Location of the API Blueprint to parse. * * @var string */ - protected $location; + protected string $location; /** * Filename to parse. * * @var string */ - private $filename; + private string $filename; /** * FileParser constructor. From ea7d55be0948a8175842418dfbff1aed659d82c8 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 16 Jan 2022 16:53:26 +0100 Subject: [PATCH 2/5] Model: add types --- src/PHPDraft/Model/Category.php | 19 ++------ src/PHPDraft/Model/Comparable.php | 4 +- .../Model/Elements/ArrayStructureElement.php | 5 +-- .../Model/Elements/BasicStructureElement.php | 22 +++++----- .../Elements/ElementStructureElement.php | 20 +++++---- .../Model/Elements/EnumStructureElement.php | 22 ++++------ .../Model/Elements/ObjectStructureElement.php | 18 ++++---- .../Model/Elements/RequestBodyElement.php | 4 +- .../Model/Elements/StructureElement.php | 2 +- .../Tests/ArrayStructureElementTest.php | 5 +++ .../Tests/BasicStructureElementTest.php | 3 +- .../Tests/ElementStructureElementTest.php | 7 ++- .../Tests/EnumStructureElementTest.php | 12 +++++ .../Tests/ObjectStructureElementTest.php | 8 ++++ .../Elements/Tests/RequestBodyElementTest.php | 22 ++++++++-- src/PHPDraft/Model/HTTPRequest.php | 37 ++++++++-------- src/PHPDraft/Model/HTTPResponse.php | 44 +++++++++---------- src/PHPDraft/Model/HierarchyElement.php | 14 +++--- src/PHPDraft/Model/Resource.php | 11 +++-- src/PHPDraft/Model/Tests/HTTPRequestTest.php | 2 + src/PHPDraft/Model/Tests/HTTPResponseTest.php | 10 ++--- .../Model/Tests/HierarchyElementChildTest.php | 16 ------- src/PHPDraft/Model/Tests/ResourceTest.php | 11 +++-- src/PHPDraft/Model/Tests/TransitionTest.php | 31 ++++--------- src/PHPDraft/Model/Transition.php | 35 +++++++-------- 25 files changed, 191 insertions(+), 193 deletions(-) diff --git a/src/PHPDraft/Model/Category.php b/src/PHPDraft/Model/Category.php index 08ec4e52..7355798b 100644 --- a/src/PHPDraft/Model/Category.php +++ b/src/PHPDraft/Model/Category.php @@ -12,9 +12,7 @@ namespace PHPDraft\Model; -use PHPDraft\Model\Elements\BasicStructureElement; use PHPDraft\Model\Elements\ObjectStructureElement; -use stdClass; /** * Class Category. @@ -26,28 +24,19 @@ class Category extends HierarchyElement * * @var ObjectStructureElement[] */ - public $structures = []; - - /** - * Category type. - * - * @var ?string - */ - public $type = null; + public array $structures = []; /** * Fill class values based on JSON object. * - * @param stdClass $object JSON object + * @param object $object JSON object * - * @return $this self-reference + * @return self self-reference */ - public function parse(stdClass $object) + public function parse(object $object): self { parent::parse($object); - $this->type = $object->meta->classes->content ?? null; - foreach ($object->content as $item) { switch ($item->element) { case 'resource': diff --git a/src/PHPDraft/Model/Comparable.php b/src/PHPDraft/Model/Comparable.php index d8d31985..c65083b6 100644 --- a/src/PHPDraft/Model/Comparable.php +++ b/src/PHPDraft/Model/Comparable.php @@ -16,9 +16,9 @@ interface Comparable /** * Check if item is the same as other item. * - * @param self $b Object to compare to + * @param object $b Object to compare to * * @return bool */ - public function is_equal_to($b): bool; + public function is_equal_to(object $b): bool; } diff --git a/src/PHPDraft/Model/Elements/ArrayStructureElement.php b/src/PHPDraft/Model/Elements/ArrayStructureElement.php index b0dc760a..b50eb37b 100644 --- a/src/PHPDraft/Model/Elements/ArrayStructureElement.php +++ b/src/PHPDraft/Model/Elements/ArrayStructureElement.php @@ -23,7 +23,7 @@ class ArrayStructureElement extends BasicStructureElement * Parse an array object. * * @param object|null $object APIB Item to parse - * @param array $dependencies List of dependencies build + * @param string[] $dependencies List of dependencies build * * @return self Self reference */ @@ -60,8 +60,7 @@ public function __toString(): string if (is_string($this->value)) { $type = $this->get_element_as_html($this->element); $desc = ''; - if ($this->description !== NULL) - { + if ($this->description !== null) { $desc = MarkdownExtra::defaultTransform($this->description); } diff --git a/src/PHPDraft/Model/Elements/BasicStructureElement.php b/src/PHPDraft/Model/Elements/BasicStructureElement.php index 036d8afb..fd02f2e6 100644 --- a/src/PHPDraft/Model/Elements/BasicStructureElement.php +++ b/src/PHPDraft/Model/Elements/BasicStructureElement.php @@ -15,25 +15,25 @@ abstract class BasicStructureElement implements StructureElement * * @var ElementStructureElement|null */ - public $key; + public ?ElementStructureElement $key = null; /** * Object JSON type. * * @var string|null */ - public $type; + public ?string $type; /** * Object description. * * @var string|null */ - public $description; + public ?string $description = null; /** * Type of element. * * @var string|null */ - public $element = null; + public ?string $element = null; /** * Object value. * @@ -45,31 +45,31 @@ abstract class BasicStructureElement implements StructureElement * * @var string|null */ - public $status = ''; + public ?string $status = ''; /** * Parent structure. * * @var string|null */ - public $ref; + public ?string $ref; /** * Is variable. * * @var bool */ - public $is_variable; + public bool $is_variable = false; /** * List of object dependencies. * * @var string[]|null */ - public $deps; + public ?array $deps; /** * Parse a JSON object to a structure. * * @param object|null $object An object to parse - * @param array $dependencies Dependencies of this object + * @param string[] $dependencies Dependencies of this object * * @return StructureElement self reference */ @@ -92,8 +92,8 @@ abstract protected function new_instance(): StructureElement; /** * Parse common fields to give context. * - * @param object $object APIB object - * @param array $dependencies Object dependencies + * @param object $object APIB object + * @param string[] $dependencies Object dependencies * * @return void */ diff --git a/src/PHPDraft/Model/Elements/ElementStructureElement.php b/src/PHPDraft/Model/Elements/ElementStructureElement.php index 2ff72632..7dce2e26 100644 --- a/src/PHPDraft/Model/Elements/ElementStructureElement.php +++ b/src/PHPDraft/Model/Elements/ElementStructureElement.php @@ -7,16 +7,16 @@ class ElementStructureElement implements StructureElement /** * Object JSON type. * - * @var string|null + * @var string */ - public $type; + public string $type; /** * Object description. * * @var string|null */ - public $description; + public ?string $description = null; /** * Object value. @@ -26,11 +26,14 @@ class ElementStructureElement implements StructureElement public $value = null; /** - * @param object|null $object - * @param array $dependencies - * @return StructureElement + * Parse a JSON object to a structure. + * + * @param object|null $object An object to parse + * @param string[] $dependencies Dependencies of this object + * + * @return self self reference */ - public function parse(?object $object, array &$dependencies): StructureElement + public function parse(?object $object, array &$dependencies): self { if (!in_array($object->element, self::DEFAULTS)) { $dependencies[] = $object->element; @@ -77,8 +80,7 @@ public function string_value(bool $flat = false) */ protected function get_element_as_html(?string $element): string { - if ($element === null) - { + if ($element === null) { return 'null'; } diff --git a/src/PHPDraft/Model/Elements/EnumStructureElement.php b/src/PHPDraft/Model/Elements/EnumStructureElement.php index 469433b2..f0621c28 100644 --- a/src/PHPDraft/Model/Elements/EnumStructureElement.php +++ b/src/PHPDraft/Model/Elements/EnumStructureElement.php @@ -20,7 +20,7 @@ class EnumStructureElement extends BasicStructureElement * Parse an array object. * * @param object|null $object APIB Item to parse - * @param array $dependencies List of dependencies build + * @param string[] $dependencies List of dependencies build * * @return $this */ @@ -79,23 +79,19 @@ public function parse(?object $object, array &$dependencies): StructureElement */ public function __toString(): string { - if (is_string($this->value)) { - $type = $this->get_element_as_html($this->element); - $desc = ''; - if ($this->description !== NULL) - { - $desc = MarkdownExtra::defaultTransform($this->description); + if (is_iterable($this->value)) { + $return = ''; + foreach ($this->value as $item) { + $return .= $item->__toString(); } - return "{$this->key->value}{$type}{$desc}"; + return ''; } - $return = ''; - foreach ($this->value as $item) { - $return .= $item->__toString(); - } + $type = $this->get_element_as_html($this->element); + $desc = $this->description === null ? '' : MarkdownExtra::defaultTransform($this->description); - return ''; + return "{$this->key->value}{$type}{$desc}"; } /** diff --git a/src/PHPDraft/Model/Elements/ObjectStructureElement.php b/src/PHPDraft/Model/Elements/ObjectStructureElement.php index 70b15de2..cbf062b8 100644 --- a/src/PHPDraft/Model/Elements/ObjectStructureElement.php +++ b/src/PHPDraft/Model/Elements/ObjectStructureElement.php @@ -13,7 +13,6 @@ namespace PHPDraft\Model\Elements; use Michelf\MarkdownExtra; -use stdClass; /** * Class ObjectStructureElement. @@ -22,10 +21,10 @@ class ObjectStructureElement extends BasicStructureElement { /** * Object representation before parsing - * @var stdClass|null + * @var object|null * @phpstan-ignore-next-line */ - private $object; + private ?object $object; /** * Unset object function. @@ -40,7 +39,7 @@ public function __clearForTest(): void * Parse a JSON object to a data structure. * * @param object|null $object An object to parse - * @param array $dependencies Dependencies of this object + * @param string[] $dependencies Dependencies of this object * * @return ObjectStructureElement self reference */ @@ -84,8 +83,8 @@ public function parse(?object $object, array &$dependencies): StructureElement /** * Parse $this->value as a structure based on given content. * - * @param object $object APIB content - * @param array $dependencies Object dependencies + * @param object $object APIB content + * @param string[] $dependencies Object dependencies * * @return void */ @@ -118,8 +117,8 @@ protected function new_instance(): StructureElement /** * Parse content formed as an array. * - * @param object $object APIB content - * @param array $dependencies Object dependencies + * @param object $object APIB content + * @param string[] $dependencies Object dependencies * * @return void */ @@ -211,8 +210,7 @@ protected function construct_string_return(string $value): string $variable = ''; } $desc = ''; - if ($this->description !== NULL) - { + if ($this->description !== null) { $desc = MarkdownExtra::defaultTransform($this->description); } diff --git a/src/PHPDraft/Model/Elements/RequestBodyElement.php b/src/PHPDraft/Model/Elements/RequestBodyElement.php index 7dfb059b..f287aa6a 100644 --- a/src/PHPDraft/Model/Elements/RequestBodyElement.php +++ b/src/PHPDraft/Model/Elements/RequestBodyElement.php @@ -53,10 +53,10 @@ public function print_request(?string $type = 'application/x-www-form-urlencoded switch ($type) { case 'application/x-www-form-urlencoded': - return $this->key . '=' . $value . ''; + return "{$this->key->value}=$value"; default: $object = []; - $object[$this->key] = $value; + $object[$this->key->value] = $value; return json_encode($object); } diff --git a/src/PHPDraft/Model/Elements/StructureElement.php b/src/PHPDraft/Model/Elements/StructureElement.php index f06d4847..156b40ce 100644 --- a/src/PHPDraft/Model/Elements/StructureElement.php +++ b/src/PHPDraft/Model/Elements/StructureElement.php @@ -25,7 +25,7 @@ interface StructureElement * Parse a JSON object to a structure. * * @param object|null $object An object to parse - * @param array $dependencies Dependencies of this object + * @param string[] $dependencies Dependencies of this object * * @return self self reference */ diff --git a/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php index 39f69aaa..c81d6ed7 100644 --- a/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php @@ -70,6 +70,7 @@ public function parseObjectProvider(): array $base1->type = null; $base1->is_variable = false; $base1->description = null; + $base1->ref = null; $base1->deps = []; $base2 = new ArrayStructureElement(); @@ -87,6 +88,7 @@ public function parseObjectProvider(): array $base2->is_variable = false; $base2->description = null; $base2->deps = ['Some simple array']; + $base2->ref = null; $base3 = new ArrayStructureElement(); $base3->key = new ElementStructureElement(); @@ -105,6 +107,7 @@ public function parseObjectProvider(): array $base3->is_variable = false; $base3->description = "List of car identifiers to retrieve"; $base3->deps = []; + $base3->ref = null; $return['generic value type'] = [ '{ @@ -190,6 +193,7 @@ public function testToStringWithArray(): void $val1 = new ElementStructureElement(); $val1->type = 'string'; $val1->value = 'stuff'; + $val1->description = null; $val2 = new ElementStructureElement(); $val2->type = 'int'; $val2->value = 'class'; @@ -207,6 +211,7 @@ public function testToStringWithComplexArray(): void $val1 = new ElementStructureElement(); $val1->type = 'Bike'; $val1->value = 'type'; + $val1->description = null; $val2 = new ElementStructureElement(); $val2->type = 'car'; $val2->value = 'stuff'; diff --git a/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php index 5c563624..fe635a7a 100644 --- a/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php @@ -119,8 +119,7 @@ public function testParseCommonDeps(): void public function testParseCommon($value, BasicStructureElement $expected_value): void { $dep = []; - $method = $this->reflection->getMethod('parse_common'); - $method->setAccessible(true); + $method = $this->get_accessible_reflection_method('parse_common'); $method->invokeArgs($this->class, [$value, &$dep]); $this->assertEquals($expected_value->key, $this->class->key); diff --git a/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php index bc1492a2..8b02450e 100644 --- a/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php @@ -50,7 +50,9 @@ public function testParse(): void */ public function testStringValue(): void { - $this->assertSame('
  • null
  • ', $this->class->string_value()); + $this->set_reflection_property_value('type', 'string'); + $this->set_reflection_property_value('description', null); + $this->assertSame('
  • string
  • ', $this->class->string_value()); } /** @@ -59,6 +61,7 @@ public function testStringValue(): void public function testToString(): void { $this->set_reflection_property_value('type', 'string'); + $this->set_reflection_property_value('description', null); $this->assertSame('
  • string
  • ', $this->class->__toString()); } @@ -69,6 +72,7 @@ public function testToString(): void public function testToStringCustomType(): void { $this->set_reflection_property_value('type', 'Cow'); + $this->set_reflection_property_value('description', null); $this->assertSame('
  • Cow
  • ', $this->class->__toString()); } @@ -91,6 +95,7 @@ public function testToStringValue(): void { $this->set_reflection_property_value('type', 'Cow'); $this->set_reflection_property_value('value', 'stuff'); + $this->set_reflection_property_value('description', null); $this->assertSame('
  • Cow - stuff
  • ', $this->class->__toString()); } diff --git a/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php index 0c47deb5..1a9bb325 100644 --- a/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php @@ -55,12 +55,16 @@ public function testNewInstance(): void */ public function testToStringWithArray(): void { + $this->set_reflection_property_value('description', null); + $value1 = new ElementStructureElement(); $value1->value = 'hello'; $value1->type = 'string'; + $value1->description = null; $value2 = new ElementStructureElement(); $value2->value = 'test'; $value2->type = 'int'; + $value2->description = null; $this->class->value = [$value1, $value2]; $return = $this->class->__toString(); $this->assertSame('', $return); @@ -75,6 +79,7 @@ public function testToStringWithString(): void $this->class->key = new ElementStructureElement(); $this->class->key->type = 'string'; $this->class->key->value = 'key'; + $this->class->key->description = null; $this->class->element = 'string'; $return = $this->class->__toString(); $this->assertSame('keystring', $return); @@ -99,12 +104,16 @@ public function testToStringWithStringComplex(): void */ public function testToStringWithComplexArray(): void { + $this->set_reflection_property_value('description', null); + $value1 = new ElementStructureElement(); $value1->value = 'hello'; $value1->type = 'bike'; + $value1->description = null; $value2 = new ElementStructureElement(); $value2->value = 'test'; $value2->type = 'Car'; + $value2->description = null; $this->class->value = [$value1, $value2]; $return = $this->class->__toString(); $this->assertSame('', $return); @@ -150,6 +159,7 @@ public function parseObjectProvider(): array $base1->type = 'Some simple enum'; $base1->is_variable = false; $base1->description = null; + $base1->ref = null; $base1->deps = ['Some simple enum']; $base2 = new EnumStructureElement(); @@ -161,6 +171,7 @@ public function parseObjectProvider(): array $base2->element = 'enum'; $base2->type = 'string'; $base2->description = null; + $base2->ref = null; $base2->is_variable = false; $base2->deps = []; @@ -173,6 +184,7 @@ public function parseObjectProvider(): array $base3->element = 'member'; $base3->type = 'number'; $base3->description = "List of car identifiers to retrieve"; + $base3->ref = null; $base3->is_variable = false; $base3->deps = []; diff --git a/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php index 1e70657a..8feec1e0 100644 --- a/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php @@ -86,6 +86,8 @@ public function parseObjectProvider(): array $base1->type = 'string'; $base1->is_variable = false; $base1->description = "desc1"; + $base1->ref = null; + $base1->__clearForTest(); $base2 = new ObjectStructureElement(); $base2->key = new ElementStructureElement(); @@ -97,6 +99,8 @@ public function parseObjectProvider(): array $base2->type = 'string'; $base2->is_variable = false; $base2->description = "desc2"; + $base2->ref = null; + $base2->__clearForTest(); $base3 = clone $base2; $base3->value = 'test1 (string) | test2 (int) | test3 (Cow)'; @@ -339,6 +343,8 @@ public function testValueStructureObjectContentParseContent(): void */ public function testToStringBasic(): void { + $this->set_reflection_property_value('description', null); + $return = $this->class->__toString(); $this->assertSame('{ }', $return); } @@ -372,6 +378,7 @@ public function testToStringNullValue(): void $this->class->key = new ElementStructureElement(); $this->class->key->value = 'hello'; $this->class->type = 'mixed'; + $this->class->is_variable = false; $return = $this->class->__toString(); $this->assertSame('hellomixed ', $return); } @@ -415,6 +422,7 @@ public function testToStringArrayValue(): void */ public function testToStringEnumValue(): void { + $this->class->type = null; $this->class->key = new ElementStructureElement(); $this->class->key->value = 'hello'; $this->class->value = new EnumStructureElement(); diff --git a/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php b/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php index 94c16840..42a69018 100644 --- a/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php @@ -58,7 +58,9 @@ public function testNewInstance(): void */ public function testPrintBasic(): void { - $this->class->key = 'key'; + $key = new ElementStructureElement(); + $key->value = 'key'; + $this->class->key = $key; $return = $this->class->print_request(); $this->assertSame('key=?', $return); } @@ -68,7 +70,9 @@ public function testPrintBasic(): void */ public function testPrintBasicArray(): void { - $this->class->key = 'key'; + $key = new ElementStructureElement(); + $key->value = 'key'; + $this->class->key = $key; $this->class->value = 'value'; $c1 = clone $this->class; $c2 = clone $this->class; @@ -82,7 +86,9 @@ public function testPrintBasicArray(): void */ public function testPrintJson(): void { - $this->class->key = 'key'; + $key = new ElementStructureElement(); + $key->value = 'key'; + $this->class->key = $key; $return = $this->class->print_request('application/json'); $this->assertSame('{"key":"?"}', $return); } @@ -92,7 +98,9 @@ public function testPrintJson(): void */ public function testPrintJsonArray(): void { - $this->class->key = 'key'; + $key = new ElementStructureElement(); + $key->value = 'key'; + $this->class->key = $key; $this->class->value = 'value'; $c1 = clone $this->class; $c2 = clone $this->class; @@ -134,23 +142,29 @@ public function parseObjectProvider(): array $base1->key = new ElementStructureElement(); $base1->key->type = 'string'; $base1->key->value = 'name'; + $base1->key->description = null; $base1->value = 'P10'; $base1->status = 'optional'; $base1->element = 'member'; $base1->type = 'string'; $base1->is_variable = false; $base1->description = "desc1"; + $base1->ref = null; + $base1->__clearForTest(); $base2 = new RequestBodyElement(); $base2->key = new ElementStructureElement(); $base2->key->type = 'string'; $base2->key->value = 'Auth2'; + $base2->key->description = null; $base2->value = 'something'; $base2->status = 'required'; $base2->element = 'member'; $base2->type = 'string'; $base2->is_variable = false; $base2->description = "desc2"; + $base2->ref = null; + $base2->__clearForTest(); $base3 = clone $base2; $base3->value = 'test1 (string) | test2 (string) | test3 (string)'; diff --git a/src/PHPDraft/Model/HTTPRequest.php b/src/PHPDraft/Model/HTTPRequest.php index 2acce7bd..da01f485 100644 --- a/src/PHPDraft/Model/HTTPRequest.php +++ b/src/PHPDraft/Model/HTTPRequest.php @@ -15,44 +15,43 @@ use PHPDraft\Model\Elements\RequestBodyElement; use PHPDraft\Model\Elements\StructureElement; use QL\UriTemplate\Exception; -use stdClass; class HTTPRequest implements Comparable { /** * HTTP Headers. * - * @var array + * @var array */ - public $headers = []; + public array $headers = []; /** * The HTTP Method. * * @var string */ - public $method; + public string $method; /** * Title of the request. * - * @var string + * @var string|null */ - public $title; + public ?string $title; /** * Description of the request. * * @var string */ - public $description; + public string $description; /** * Parent class. * * @var Transition */ - public $parent; + public Transition $parent; /** * Body of the request. @@ -64,9 +63,9 @@ class HTTPRequest implements Comparable /** * Schema of the body of the request. * - * @var mixed + * @var string|null */ - public $body_schema = null; + public ?string $body_schema = null; /** * Structure of the request. * @@ -78,7 +77,7 @@ class HTTPRequest implements Comparable * * @var string */ - protected $id; + protected string $id; /** * HTTPRequest constructor. @@ -157,9 +156,9 @@ public function parse(object $object): self /** * Parse the objects into a request body. * - * @param stdClass $objects JSON objects + * @param object $objects JSON objects */ - private function parse_structure(stdClass $objects): void + private function parse_structure(object $objects): void { $deps = []; $structure = new RequestBodyElement(); @@ -177,8 +176,8 @@ public function get_id(): string /** * Generate a cURL command for the HTTP request. * - * @param string $base_url URL to the base server - * @param array $additional Extra options to pass to cURL + * @param string $base_url URL to the base server + * @param array $additional Extra options to pass to cURL * * @throws Exception * @@ -218,11 +217,11 @@ public function get_curl_command(string $base_url, array $additional = []): stri /** * Check if item is the same as other item. * - * @param self $b Object to compare to + * @param object $b Object to compare to * * @return bool */ - public function is_equal_to($b): bool + public function is_equal_to(object $b): bool { if (!($b instanceof self)) { return false; @@ -236,10 +235,10 @@ public function is_equal_to($b): bool /** * Convert class to string identifier */ - public function __toString() + public function __toString(): string { $headers = json_encode($this->headers); $body = json_encode($this->body); - return "{$this->method}_{$body}_{$headers}"; + return sprintf("%s_%s_%s", $this->method, $body, $headers); } } diff --git a/src/PHPDraft/Model/HTTPResponse.php b/src/PHPDraft/Model/HTTPResponse.php index 9451f9a2..669d48f8 100644 --- a/src/PHPDraft/Model/HTTPResponse.php +++ b/src/PHPDraft/Model/HTTPResponse.php @@ -13,7 +13,6 @@ namespace PHPDraft\Model; use PHPDraft\Model\Elements\ObjectStructureElement; -use stdClass; class HTTPResponse implements Comparable { @@ -22,49 +21,49 @@ class HTTPResponse implements Comparable * * @var int */ - public $statuscode; + public int $statuscode; /** * Description of the object. * - * @var string + * @var string|null */ - public $description; + public ?string $description = null; /** * Identifier for the request. * * @var string */ - protected $id; + protected string $id; /** * Response headers. * - * @var array + * @var array */ - public $headers = []; + public array $headers = []; /** * Response bodies. * - * @var array + * @var array */ - public $content = []; + public array $content = []; /** * Response structure. * * @var ObjectStructureElement[] */ - public $structure = []; + public array $structure = []; /** * Parent entity. * * @var Transition */ - protected $parent; + protected Transition $parent; public function __construct(Transition $parent) { @@ -75,11 +74,11 @@ public function __construct(Transition $parent) /** * Fill class values based on JSON object. * - * @param stdClass $object JSON object + * @param object $object JSON object * * @return $this self-reference */ - public function parse(stdClass $object): self + public function parse(object $object): self { if (isset($object->attributes->statusCode->content)) { $this->statuscode = intval($object->attributes->statusCode->content); @@ -105,11 +104,11 @@ public function get_id(): string /** * Parse request headers. * - * @param stdClass $object An object to parse for headers + * @param object $object An object to parse for headers * * @return void */ - protected function parse_headers(stdClass $object): void + protected function parse_headers(object $object): void { foreach ($object->content as $value) { if (isset($value->content)) { @@ -121,11 +120,11 @@ protected function parse_headers(stdClass $object): void /** * Parse request content. * - * @param stdClass $value An object to parse for content + * @param object $value An object to parse for content * * @return void */ - protected function parse_content(stdClass $value): void + protected function parse_content(object $value): void { if ($value->element === 'copy') { $this->description = $value->content; @@ -145,18 +144,17 @@ protected function parse_content(stdClass $value): void foreach ($value->content->content as $object) { $this->parse_structure($object); } - return; } } /** * Parse structure of the content. * - * @param stdClass $object Objects containing the structure + * @param object $object Objects containing the structure * * @return void */ - protected function parse_structure(stdClass $object): void + protected function parse_structure(object $object): void { $deps = []; $struct = new ObjectStructureElement(); @@ -174,11 +172,11 @@ protected function parse_structure(stdClass $object): void /** * Check if item is the same as other item. * - * @param self $b Object to compare to + * @param object $b Object to compare to * * @return bool */ - public function is_equal_to($b): bool + public function is_equal_to(object $b): bool { if (!($b instanceof self)) { return false; @@ -190,7 +188,7 @@ public function is_equal_to($b): bool /** * Convert class to string identifier */ - public function __toString() + public function __toString(): string { return "{$this->statuscode}_{$this->description}"; } diff --git a/src/PHPDraft/Model/HierarchyElement.php b/src/PHPDraft/Model/HierarchyElement.php index 14d5b058..8be8b166 100644 --- a/src/PHPDraft/Model/HierarchyElement.php +++ b/src/PHPDraft/Model/HierarchyElement.php @@ -12,8 +12,6 @@ namespace PHPDraft\Model; -use stdClass; - /** * Class HierarchyElement. */ @@ -24,37 +22,37 @@ abstract class HierarchyElement * * @var string */ - public $title; + public string $title; /** * Description of the element. * * @var string */ - public $description; + public string $description; /** * Child elements. * * @var HierarchyElement[] */ - public $children = []; + public array $children = []; /** * Parent Element. * * @var HierarchyElement|null */ - protected $parent = null; + protected ?HierarchyElement $parent = null; /** * Parse a JSON object to an element. * - * @param stdClass $object an object to parse + * @param object $object an object to parse * * @return void */ - public function parse(stdClass $object) + public function parse(object $object) { if (isset($object->meta) && isset($object->meta->title)) { $this->title = $object->meta->title->content ?? $object->meta->title; diff --git a/src/PHPDraft/Model/Resource.php b/src/PHPDraft/Model/Resource.php index d62335a2..b5aaade6 100644 --- a/src/PHPDraft/Model/Resource.php +++ b/src/PHPDraft/Model/Resource.php @@ -13,23 +13,22 @@ namespace PHPDraft\Model; use PHPDraft\Model\Elements\ObjectStructureElement; -use stdClass; class Resource extends HierarchyElement { /** * Location relative to the base URL. * - * @var string + * @var string|null */ - public $href; + public ?string $href = null; /** * URL variables. * * @var ObjectStructureElement[] */ - public $url_variables = []; + public array $url_variables = []; /** * Resource constructor. @@ -44,11 +43,11 @@ public function __construct(Category &$parent) /** * Fill class values based on JSON object. * - * @param stdClass $object JSON object + * @param object $object JSON object * * @return $this self-reference */ - public function parse(stdClass $object): self + public function parse(object $object): self { parent::parse($object); diff --git a/src/PHPDraft/Model/Tests/HTTPRequestTest.php b/src/PHPDraft/Model/Tests/HTTPRequestTest.php index f8116147..24535032 100644 --- a/src/PHPDraft/Model/Tests/HTTPRequestTest.php +++ b/src/PHPDraft/Model/Tests/HTTPRequestTest.php @@ -135,6 +135,7 @@ public function testEqualOnBoth(): void public function testGetCurlCommandNoKey(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('method', ''); $return = $this->class->get_curl_command('https://ur.l'); @@ -147,6 +148,7 @@ public function testGetCurlCommandNoKey(): void public function testGetCurlCommandWithHeaders(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('method', ''); $headers = ['header' => 'value']; $this->set_reflection_property_value('headers', $headers); diff --git a/src/PHPDraft/Model/Tests/HTTPResponseTest.php b/src/PHPDraft/Model/Tests/HTTPResponseTest.php index 82c2d3c4..59c24e55 100644 --- a/src/PHPDraft/Model/Tests/HTTPResponseTest.php +++ b/src/PHPDraft/Model/Tests/HTTPResponseTest.php @@ -42,11 +42,10 @@ class HTTPResponseTest extends LunrBaseTest public function setUp(): void { $this->parent_transition = $this->createMock('\PHPDraft\Model\Transition'); - $this->parent = $this->getMockBuilder('\PHPDraft\Model\HierarchyElement') + $this->parent = $this->getMockBuilder('\PHPDraft\Model\Transition') + ->disableOriginalConstructor() ->getMock(); - $this->mock_function('microtime', function () { - return '1000'; - }); + $this->mock_function('microtime', fn() => '1000'); $this->class = new HTTPResponse($this->parent_transition); $this->unmock_function('microtime'); $this->reflection = new ReflectionClass('\PHPDraft\Model\HTTPResponse'); @@ -114,13 +113,14 @@ public function testParseIsCalledExtraHeaders(): void public function testParseIsCalledWOAttributes(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('statuscode', 200); $obj = '{"content":[]}'; $this->class->parse(json_decode($obj)); $this->assertSame($this->parent, $this->get_reflection_property_value('parent')); - $this->assertNull($this->get_reflection_property_value('statuscode')); + $this->assertSame($this->get_reflection_property_value('statuscode'), 200); } /** diff --git a/src/PHPDraft/Model/Tests/HierarchyElementChildTest.php b/src/PHPDraft/Model/Tests/HierarchyElementChildTest.php index 23c9d5d2..68f362d9 100644 --- a/src/PHPDraft/Model/Tests/HierarchyElementChildTest.php +++ b/src/PHPDraft/Model/Tests/HierarchyElementChildTest.php @@ -32,22 +32,6 @@ public function setUp(): void ->getMock(); } - /** - * Test if the value the class is initialized with is correct - */ - public function testTitleSetup(): void - { - $this->assertSame(null, $this->class->title); - } - - /** - * Test if the value the class is initialized with is correct - */ - public function testDescriptionSetup(): void - { - $this->assertSame(null, $this->class->description); - } - /** * Test if the value the class is initialized with is correct */ diff --git a/src/PHPDraft/Model/Tests/ResourceTest.php b/src/PHPDraft/Model/Tests/ResourceTest.php index 760d748f..2c4be02c 100644 --- a/src/PHPDraft/Model/Tests/ResourceTest.php +++ b/src/PHPDraft/Model/Tests/ResourceTest.php @@ -35,6 +35,8 @@ public function setUp(): void { $this->parent = $this->getMockBuilder('\PHPDraft\Model\Category') ->getMock(); + + $this->parent->href = null; $this->class = new Resource($this->parent); $this->reflection = new ReflectionClass('PHPDraft\Model\Resource'); } @@ -74,18 +76,19 @@ public function testParseIsCalled(): void /** * Test basic parse functions + * + * @covers \PHPDraft\Model\Resource::parse */ public function testParseIsCalledNoHREF(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('href', null); $obj = '{"content":[]}'; $this->class->parse(json_decode($obj)); - $href_property = $this->reflection->getProperty('href'); - $href_property->setAccessible(true); - $this->assertNull($href_property->getValue($this->class)); + $this->assertNull($this->get_reflection_property_value('href')); } /** @@ -94,6 +97,7 @@ public function testParseIsCalledNoHREF(): void public function testParseIsCalledIsCopy(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('href', null); $obj = '{"content":[{"element":"copy", "content":""},{"element":"hello", "content":""}, {"element":"hello", "content":""}]}'; @@ -108,6 +112,7 @@ public function testParseIsCalledIsCopy(): void public function testParseIsCalledIsNotCopy(): void { $this->set_reflection_property_value('parent', $this->parent); + $this->set_reflection_property_value('href', null); $this->assertEmpty($this->get_reflection_property_value('children')); $obj = '{"content":[{"element":"hello", "content":""}]}'; diff --git a/src/PHPDraft/Model/Tests/TransitionTest.php b/src/PHPDraft/Model/Tests/TransitionTest.php index ef77ff9a..7be196f0 100644 --- a/src/PHPDraft/Model/Tests/TransitionTest.php +++ b/src/PHPDraft/Model/Tests/TransitionTest.php @@ -307,11 +307,8 @@ public function testBuildURLOverlap(): void $parent->href = '/url'; - $this->set_reflection_property_value('parent', $this->parent); - - $req_property = $this->reflection->getProperty('href'); - $req_property->setAccessible(true); - $req_property->setValue($this->class, '/url/level'); + $this->set_reflection_property_value('parent', $parent); + $this->set_reflection_property_value('href', '/url/level'); $return = $this->class->build_url(); @@ -329,15 +326,10 @@ public function testBuildURLClean(): void $parent->href = '/url'; - $this->set_reflection_property_value('parent', $this->parent); - - $req_property = $this->reflection->getProperty('href'); - $req_property->setAccessible(true); - $req_property->setValue($this->class, '/url/level'); + $this->set_reflection_property_value('parent', $parent); + $this->set_reflection_property_value('href', '/url/level'); - $this->mock_function('strip_tags', function () { - return "STRIPPED"; - }); + $this->mock_function('strip_tags', fn () => "STRIPPED"); $return = $this->class->build_url('', true); @@ -357,8 +349,6 @@ public function testBuildURLVars(): void $parent->href = '/url'; - $this->set_reflection_property_value('parent', $this->parent); - $var1 = $this->getMockBuilder('\PHPDraft\Model\Elements\ObjectStructureElement') ->disableOriginalConstructor() ->getMock(); @@ -372,15 +362,12 @@ public function testBuildURLVars(): void $var1->expects($this->once()) ->method('string_value') ->will($this->returnValue('STRING')); - $var1->key = $key1; - $property = $this->reflection->getProperty('url_variables'); - $property->setAccessible(true); - $property->setValue($this->class, [$var1]); + $var1->key = $key1; - $req_property = $this->reflection->getProperty('href'); - $req_property->setAccessible(true); - $req_property->setValue($this->class, '/url/level'); + $this->set_reflection_property_value('parent', $parent); + $this->set_reflection_property_value('url_variables', [$var1]); + $this->set_reflection_property_value('href', '/url/level'); $return = $this->class->build_url(); diff --git a/src/PHPDraft/Model/Transition.php b/src/PHPDraft/Model/Transition.php index 892e159b..82f78ed9 100644 --- a/src/PHPDraft/Model/Transition.php +++ b/src/PHPDraft/Model/Transition.php @@ -16,7 +16,6 @@ use PHPDraft\Model\Elements\ObjectStructureElement; use PHPDraft\Model\Elements\StructureElement; use QL\UriTemplate\UriTemplate; -use stdClass; class Transition extends HierarchyElement { @@ -25,56 +24,56 @@ class Transition extends HierarchyElement * * @var string */ - public $method; + public string $method; /** * URI. * - * @var string + * @var string|null */ - public $href; + public ?string $href = null; /** * URL variables. * * @var StructureElement[] */ - public $url_variables = []; + public array $url_variables = []; /** * Data variables. * * @var StructureElement|null */ - public $data_variables = null; + public ?StructureElement $data_variables = null; /** * The request. * * @var HTTPRequest[] */ - public $requests = []; + public array $requests = []; /** * The responses. * * @var HTTPResponse[] */ - public $responses = []; + public array $responses = []; /** * Structures used (if any). * * @var StructureElement[] */ - public $structures = []; + public array $structures = []; /** * Transition constructor. * - * @param \PHPDraft\Model\Resource $parent A reference to the parent object + * @param Resource $parent A reference to the parent object */ - public function __construct(\PHPDraft\Model\Resource &$parent) + public function __construct(Resource &$parent) { $this->parent = $parent; } @@ -82,16 +81,16 @@ public function __construct(\PHPDraft\Model\Resource &$parent) /** * Fill class values based on JSON object. * - * @param stdClass $object JSON object + * @param object $object JSON object * * @return $this self-reference */ - public function parse(stdClass $object): self + public function parse(object $object): self { parent::parse($object); - $this->href = (isset($object->attributes->href)) ? $object->attributes->href : $this->parent->href; - $this->href = $this->href->content ?? $this->href; + $href = (isset($object->attributes->href)) ? $object->attributes->href : $this->parent->href; + $this->href = $href->content ?? $href; if (isset($object->attributes->hrefVariables)) { $deps = []; @@ -221,7 +220,7 @@ private function overlap_urls(string $str1, string $str2) * @param string $str1 First part * @param string $str2 Second part * - * @return array|bool + * @return array|bool */ private function find_overlap(string $str1, string $str2) { @@ -260,8 +259,8 @@ public function get_method(int $request = 0): string /** * Generate a cURL request to run the transition. * - * @param string $base_url base URL of the server - * @param array $additional additional arguments to pass + * @param string $base_url base URL of the server + * @param array $additional additional arguments to pass * @param int $key number of the request to generate for * * @return string A cURL CLI command From bfff575eb31f1b571bc1cc34ef886b9ec5e3fb1d Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 16 Jan 2022 16:53:35 +0100 Subject: [PATCH 3/5] Out: add types --- src/PHPDraft/Out/BaseTemplateRenderer.php | 20 ++++----- src/PHPDraft/Out/TemplateRenderer.php | 12 +++--- src/PHPDraft/Out/TwigFactory.php | 49 +++++++---------------- 3 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/PHPDraft/Out/BaseTemplateRenderer.php b/src/PHPDraft/Out/BaseTemplateRenderer.php index 63bd3854..5524d6b0 100644 --- a/src/PHPDraft/Out/BaseTemplateRenderer.php +++ b/src/PHPDraft/Out/BaseTemplateRenderer.php @@ -22,47 +22,47 @@ abstract class BaseTemplateRenderer * * @var int */ - public $sorting; + public int $sorting; /** * CSS Files to load. * * @var string[] */ - public $css = []; + public array $css = []; /** * JS Files to load. * * @var string[] */ - public $js = []; + public array $js = []; /** * JSON object of the API blueprint. * - * @var mixed + * @var array */ - protected $categories = []; + protected array $categories = []; /** * The template file to load. * * @var string */ - protected $template; + protected string $template; /** * The image to use as a logo. * * @var string|null */ - protected $image = null; + protected ?string $image = null; /** * The base data of the API. * - * @var array + * @var array */ - protected $base_data; + protected array $base_data; /** * Structures used in all data. * * @var ObjectStructureElement[] */ - protected $base_structures = []; + protected array $base_structures = []; } diff --git a/src/PHPDraft/Out/TemplateRenderer.php b/src/PHPDraft/Out/TemplateRenderer.php index ade95b1b..e30ec1ed 100644 --- a/src/PHPDraft/Out/TemplateRenderer.php +++ b/src/PHPDraft/Out/TemplateRenderer.php @@ -49,7 +49,7 @@ public function __construct(string $template, ?string $image) /** * Pre-parse objects needed and print HTML. * - * @param mixed $object JSON to parse from + * @param object $object JSON to parse from * * @return string * @@ -59,7 +59,7 @@ public function __construct(string $template, ?string $image) * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ - public function get($object): string + public function get(object $object): string { $include = $this->find_include_file($this->template, 'twig', false); if ($include === null) { @@ -86,9 +86,11 @@ public function get($object): string $twig = TwigFactory::get($loader); $template = $twig->load('main.twig'); - $extras = array_filter($this->base_data, function ($value) { - return !in_array($value, ['HOST', 'TITLE', 'ALT_HOST', 'FORMAT', 'DESC', 'COLOR_1', 'COLOR_2']); - }, ARRAY_FILTER_USE_KEY); + $extras = array_filter( + $this->base_data, + fn($value) => !in_array($value, ['HOST', 'TITLE', 'ALT_HOST', 'FORMAT', 'DESC', 'COLOR_1', 'COLOR_2']), + ARRAY_FILTER_USE_KEY + ); $extras['host'] = $this->base_data['HOST'] ?? null; return $template->render([ diff --git a/src/PHPDraft/Out/TwigFactory.php b/src/PHPDraft/Out/TwigFactory.php index ac0d92a9..18a87efd 100644 --- a/src/PHPDraft/Out/TwigFactory.php +++ b/src/PHPDraft/Out/TwigFactory.php @@ -25,55 +25,34 @@ public static function get(LoaderInterface $loader): Environment { $twig = new Environment($loader); - $twig->addFilter(new TwigFilter('minify_css', function ($string) { + $twig->addFilter(new TwigFilter('method_icon', fn(string $string) => TemplateRenderer::get_method_icon($string))); + $twig->addFilter(new TwigFilter('strip_link_spaces', fn(string $string) => TemplateRenderer::strip_link_spaces($string))); + $twig->addFilter(new TwigFilter('response_status', fn(string $string) => TemplateRenderer::get_response_status((int) $string))); + $twig->addFilter(new TwigFilter('status_reason', fn(string $string) => (new Httpstatus())->getReasonPhrase($string))); + $twig->addFilter(new TwigFilter('minify_css', function (string $string) { $minify = new Css(); $minify->add($string); return $minify->minify(); })); - $twig->addFilter(new TwigFilter('minify_js', function ($string) { + $twig->addFilter(new TwigFilter('minify_js', function (string $string) { $minify = new JS(); $minify->add($string); return $minify->minify(); })); - $twig->addFilter(new TwigFilter('method_icon', function ($string) { - return TemplateRenderer::get_method_icon($string); - })); - $twig->addFilter(new TwigFilter('strip_link_spaces', function ($string) { - return TemplateRenderer::strip_link_spaces($string); - })); - $twig->addFilter(new TwigFilter('response_status', function ($string) { - return TemplateRenderer::get_response_status($string); - })); - $twig->addFilter(new TwigFilter('status_reason', function ($string) { - return (new Httpstatus())->getReasonPhrase($string); - ; - })); - $twig->addTest(new TwigTest('enum_type', function ($object) { - return $object instanceof EnumStructureElement; - })); - $twig->addTest(new TwigTest('object_type', function ($object) { - return $object instanceof ObjectStructureElement; - })); - $twig->addTest(new TwigTest('array_type', function ($object) { - return $object instanceof ArrayStructureElement; - })); - $twig->addTest(new TwigTest('bool', function ($object) { - return is_bool($object); - })); - $twig->addTest(new TwigTest('string', function ($object) { - return is_string($object); - })); - $twig->addTest(new TwigTest('inheriting', function (BasicStructureElement $object) { + $twig->addTest(new TwigTest('enum_type', fn(object $object) => $object instanceof EnumStructureElement)); + $twig->addTest(new TwigTest('object_type', fn(object $object) => $object instanceof ObjectStructureElement)); + $twig->addTest(new TwigTest('array_type', fn(object $object) => $object instanceof ArrayStructureElement)); + $twig->addTest(new TwigTest('bool', fn($object) => is_bool($object))); + $twig->addTest(new TwigTest('string', fn($object) => is_string($object))); + $twig->addTest(new TwigTest('variable_type', fn(BasicStructureElement $object) => $object->is_variable)); + $twig->addTest(new TwigTest('inheriting', function (BasicStructureElement $object): bool { $options = array_merge(StructureElement::DEFAULTS, ['member', 'select', 'option', 'ref', 'T', 'hrefVariables']); return !(is_null($object->element) || in_array($object->element, $options)); })); - $twig->addTest(new TwigTest('variable_type', function (BasicStructureElement $object) { - return $object->is_variable; - })); $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface { - public function load(string $class) + public function load(string $class): ?object { if (MarkdownRuntime::class === $class) { return new MarkdownRuntime(new DefaultMarkdown()); From 20bb73e9e9833f0b256d3f59dea8be41a19d4f64 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 16 Jan 2022 16:53:43 +0100 Subject: [PATCH 4/5] Parse: add types --- src/PHPDraft/Parse/BaseHtmlGenerator.php | 8 ++++---- src/PHPDraft/Parse/BaseParser.php | 12 ++++++------ src/PHPDraft/Parse/Drafter.php | 17 +++++++++++++---- src/PHPDraft/Parse/Tests/BaseParserTest.php | 10 ---------- src/PHPDraft/Parse/Tests/DrafterAPITest.php | 10 ---------- src/PHPDraft/Parse/Tests/DrafterTest.php | 10 ---------- src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php | 5 ++--- 7 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/PHPDraft/Parse/BaseHtmlGenerator.php b/src/PHPDraft/Parse/BaseHtmlGenerator.php index 862901f5..afd23494 100644 --- a/src/PHPDraft/Parse/BaseHtmlGenerator.php +++ b/src/PHPDraft/Parse/BaseHtmlGenerator.php @@ -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. diff --git a/src/PHPDraft/Parse/BaseParser.php b/src/PHPDraft/Parse/BaseParser.php index 0cf98857..840b572a 100644 --- a/src/PHPDraft/Parse/BaseParser.php +++ b/src/PHPDraft/Parse/BaseParser.php @@ -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 { @@ -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. diff --git a/src/PHPDraft/Parse/Drafter.php b/src/PHPDraft/Parse/Drafter.php index 1e8b404a..b6fe2696 100644 --- a/src/PHPDraft/Parse/Drafter.php +++ b/src/PHPDraft/Parse/Drafter.php @@ -21,7 +21,7 @@ class Drafter extends BaseParser * * @var string */ - protected $drafter; + protected string $drafter; /** * ApibToJson constructor. @@ -33,7 +33,11 @@ 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; } @@ -41,7 +45,7 @@ public function init(ApibFileParser $apib): BaseParser /** * Return drafter location if found. * - * @return bool|string + * @return false|string */ public static function location() { @@ -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); } /** diff --git a/src/PHPDraft/Parse/Tests/BaseParserTest.php b/src/PHPDraft/Parse/Tests/BaseParserTest.php index 305063e4..19a97d6f 100644 --- a/src/PHPDraft/Parse/Tests/BaseParserTest.php +++ b/src/PHPDraft/Parse/Tests/BaseParserTest.php @@ -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 * diff --git a/src/PHPDraft/Parse/Tests/DrafterAPITest.php b/src/PHPDraft/Parse/Tests/DrafterAPITest.php index 84cc01f7..eea12384 100644 --- a/src/PHPDraft/Parse/Tests/DrafterAPITest.php +++ b/src/PHPDraft/Parse/Tests/DrafterAPITest.php @@ -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 * diff --git a/src/PHPDraft/Parse/Tests/DrafterTest.php b/src/PHPDraft/Parse/Tests/DrafterTest.php index c1365134..c4172ef2 100644 --- a/src/PHPDraft/Parse/Tests/DrafterTest.php +++ b/src/PHPDraft/Parse/Tests/DrafterTest.php @@ -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 */ diff --git a/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php b/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php index c9c3322d..1925ddf7 100644 --- a/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php +++ b/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php @@ -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')); } /** From 253b056e5590c7303774f75e9c31e5f4a1ac4428 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 16 Jan 2022 16:54:04 +0100 Subject: [PATCH 5/5] Base: add types --- phpdraft | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/phpdraft b/phpdraft index f6feff4b..8a3cda38 100755 --- a/phpdraft +++ b/phpdraft @@ -25,20 +25,25 @@ try // Define the cli options. $cli = new Cli(); $cli->description('Parse API Blueprint files.') - ->opt('file:f', 'Specifies the file to parse.', false, 'string') - ->opt('yes:y', 'Always accept using the online mode.', false) - ->opt('online:o', 'Always use the online mode.', false) - ->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false, 'string') - ->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false, 'string') - ->opt('header_image:i', 'Specifies an image to display in the header.', false, 'string') - ->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false, 'string') - ->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false, 'string') + ->opt('help:h', 'This help text', false) ->opt('version:v', 'Print the version for PHPDraft.', false) - ->opt('debug-json-file', 'Input a rendered JSON file for debugging.', false, 'string') - ->opt('debug-json', 'Input a rendered JSON text for debugging.', false, 'string'); + ->opt('file:f', 'Specifies the file to parse.', false) + ->opt('yes:y', 'Always accept using the online mode.', false, 'bool') + ->opt('online:o', 'Always use the online mode.', false, 'bool') + ->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false) + ->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false) + ->opt('header_image:i', 'Specifies an image to display in the header.', false) + ->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false) + ->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false) + ->opt('debug-json-file', 'Input a rendered JSON file for debugging.', false) + ->opt('debug-json', 'Input a rendered JSON text for debugging.', false); // Parse and return cli args. $args = $cli->parse($argv, FALSE); + if (isset($args['help']) || empty($args->getOpts())) { + $cli->writeHelp(); + throw new ExecutionException('', 0); + } if (isset($args['version'])) { Version::version(); throw new ExecutionException('', 0); @@ -53,6 +58,10 @@ try $file = tempnam(sys_get_temp_dir(), 'phpdraft'); file_put_contents($file, $stdin); } + if ($file === NULL || $file === '') + { + throw new ExecutionException('ERROR: File does not exist', 200); + } if (!($file !== NULL || isset($args['debug-json-file']) || isset($args['debug-json']))) { throw new ExecutionException('Missing required option: file', 1); @@ -97,12 +106,7 @@ try echo $html; } -catch (ExecutionException $exception) -{ - file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); - exit($exception->getCode()); -} -catch (Exception $exception) +catch (ExecutionException|Exception $exception) { file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); exit($exception->getCode());