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());
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.
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('| key | string | |
', $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('| hello | mixed | | | |
', $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
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