Skip to content

Commit b44d8e4

Browse files
authored
Merge pull request #16 from swaggest/phpdoc-pattern-properties
Add pattern/additional properties to phpdoc
2 parents aa5f276 + 2770144 commit b44d8e4

File tree

13 files changed

+58
-21
lines changed

13 files changed

+58
-21
lines changed

src/JsonSchema/SchemaBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private function processObject()
215215
$this->copyTo(new SchemaBuilder(
216216
$property,
217217
"\$patternProperty",
218-
$this->path . '->patternProperties->{{$pattern}}',
218+
$this->path . "->patternProperties->{{$pattern}}",
219219
$this->phpBuilder
220220
))->build()
221221
);

src/JsonSchema/TypeBuilder.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ private function processLogicType()
5656
}
5757
}
5858

59+
/**
60+
* @throws Exception
61+
* @throws \Swaggest\PhpCodeBuilder\Exception
62+
*/
63+
private function processAdditionalPatternProperties()
64+
{
65+
$schema = $this->schema;
66+
67+
if ($schema->additionalProperties instanceof Schema) {
68+
$type = $this->phpBuilder->getType($schema->additionalProperties, $this->path . '->additionalProperties');
69+
if ($type !== PhpStdType::mixed()) {
70+
$this->result->add(new ArrayOf($type));
71+
}
72+
}
73+
74+
if ($schema->patternProperties !== null) {
75+
foreach ($schema->patternProperties as $pattern => $property) {
76+
if ($property instanceof Schema) {
77+
$type = $this->phpBuilder->getType($property, $this->path . "->patternProperties->{{$pattern}}");
78+
if ($type !== PhpStdType::mixed()) {
79+
$this->result->add(new ArrayOf($type));
80+
}
81+
}
82+
}
83+
}
84+
85+
}
86+
87+
5988
/**
6089
* @throws Exception
6190
* @throws \Swaggest\PhpCodeBuilder\Exception
@@ -147,15 +176,13 @@ private function typeSwitch($type)
147176
}
148177

149178
/**
150-
* @param Schema $schema
151-
* @param string $path
152179
* @throws Exception
153180
* @throws \Swaggest\PhpCodeBuilder\Exception
154181
*/
155-
private function processNamedClass($schema, $path)
182+
private function processNamedClass()
156183
{
157-
if ($schema->properties !== null) {
158-
$class = $this->phpBuilder->getClass($schema, $path);
184+
if ($this->schema->properties !== null) {
185+
$class = $this->phpBuilder->getClass($this->schema, $this->path);
159186
$this->result->add($class);
160187
}
161188
}
@@ -178,10 +205,11 @@ public function build()
178205
}
179206

180207

181-
$this->processNamedClass($this->schema, $this->path);
208+
$this->processNamedClass();
182209
$this->processLogicType();
183210
$this->processArrayType();
184211
$this->processObjectType();
212+
$this->processAdditionalPatternProperties();
185213

186214
if (is_array($this->schema->type)) {
187215
foreach ($this->schema->type as $type) {

src/PhpStdType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public static function string()
5454

5555
public static function mixed()
5656
{
57-
return new self(self::TYPE_MIXED);
57+
static $type = null;
58+
if ($type === null) {
59+
$type = new self(self::TYPE_MIXED);
60+
}
61+
return $type;
5862
}
5963

6064
public static function object()

src/Types/ArrayOf.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
use Swaggest\PhpCodeBuilder\PhpAnyType;
7+
use Swaggest\PhpCodeBuilder\PhpStdType;
78

89
class ArrayOf implements PhpAnyType
910
{
@@ -42,6 +43,8 @@ public function renderPhpDocType()
4243
{
4344
if ($this->type instanceof OrType) {
4445
return $this->type->renderArrayPhpDocType();
46+
} elseif ($this->type === PhpStdType::mixed()) {
47+
return '';
4548
} else {
4649
return $this->type->renderPhpDocType() . '[]';
4750
}

src/Types/OrType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function simplify()
8080
{
8181
if (count($this->types) === 1) {
8282
return $this->types[0];
83+
} elseif (count($this->types) === 0) {
84+
return PhpStdType::mixed();
8385
} else {
8486
return $this;
8587
}

tests/src/PHPUnit/JsonSchema/AdvancedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public function testPatternProperties()
421421

422422
$expected = <<<'PHP'
423423
/**
424-
* @method static mixed import($data, Swaggest\JsonSchema\Context $options = null)
424+
* @method static null[]|float[]|bool[]|string[]|mixed[]|array[]|string[] import($data, Swaggest\JsonSchema\Context $options = null)
425425
*/
426426
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
427427
{
@@ -452,7 +452,7 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
452452
}
453453
454454
/**
455-
* @return null[]|float[]|bool[]|string[]|array[]
455+
* @return null[]|float[]|bool[]|string[]|mixed[]|array[]
456456
* @codeCoverageIgnoreStart
457457
*/
458458
public function getXValues()

tests/src/Tmp/Entity/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
/**
19-
* @method static Entity|string[] import($data, Context $options = null)
19+
* @method static Entity|string[]|int[]|bool[] import($data, Context $options = null)
2020
*/
2121
class Entity extends ClassStructure
2222
{

tests/src/Tmp/Swagger/Operation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Operation extends ClassStructure
4545
/** @var BodyParameter[]|HeaderParameterSubSchema[]|FormDataParameterSubSchema[]|QueryParameterSubSchema[]|PathParameterSubSchema[]|JsonReference[]|array The parameters needed to send a valid API call. */
4646
public $parameters;
4747

48-
/** @var mixed Response objects names can either be any valid HTTP status code or 'default'. */
48+
/** @var Response[]|JsonReference[] Response objects names can either be any valid HTTP status code or 'default'. */
4949
public $responses;
5050

5151
/** @var string[]|array The transfer protocol of the API. */
@@ -192,7 +192,7 @@ public function setParameters($parameters)
192192
/** @codeCoverageIgnoreEnd */
193193

194194
/**
195-
* @param mixed $responses Response objects names can either be any valid HTTP status code or 'default'.
195+
* @param Response[]|JsonReference[] $responses Response objects names can either be any valid HTTP status code or 'default'.
196196
* @return $this
197197
* @codeCoverageIgnoreStart
198198
*/

tests/src/Tmp/Swagger/Paths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Relative paths to the individual endpoints. They must be relative to the 'basePath'.
2020
* Built from #/definitions/paths
21-
* @method static mixed import($data, Context $options = null)
21+
* @method static PathItem[] import($data, Context $options = null)
2222
*/
2323
class Paths extends ClassStructure
2424
{

tests/src/Tmp/Swagger/Responses.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Response objects names can either be any valid HTTP status code or 'default'.
2020
* Built from #/definitions/responses
21-
* @method static mixed import($data, Context $options = null)
21+
* @method static Response[]|JsonReference[] import($data, Context $options = null)
2222
*/
2323
class Responses extends ClassStructure
2424
{

0 commit comments

Comments
 (0)