Skip to content

Commit 40c7880

Browse files
committed
save
1 parent bb1b399 commit 40c7880

File tree

13 files changed

+152
-49
lines changed

13 files changed

+152
-49
lines changed

helpers/TypeHelper.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @link https://www.yiiframework.com/
5+
* @copyright Copyright (c) 2008 Yii Software LLC
6+
* @license https://www.yiiframework.com/license/
7+
*/
8+
9+
namespace yii\apidoc\helpers;
10+
11+
use phpDocumentor\Reflection\Type;
12+
use phpDocumentor\Reflection\Types\AggregatedType;
13+
14+
/**
15+
* An auxiliary class for working with types.
16+
*
17+
* @author Maksim Spirkov <[email protected]>
18+
*/
19+
final class TypeHelper
20+
{
21+
/**
22+
* @return string[]
23+
*/
24+
public static function splitType(?Type $type): array
25+
{
26+
if ($type === null) {
27+
return [];
28+
}
29+
30+
// TODO: Don't split the Intersection
31+
if (!$type instanceof AggregatedType) {
32+
return [(string) $type];
33+
}
34+
35+
$types = [];
36+
foreach ($type as $childType) {
37+
$types[] = (string) $childType;
38+
}
39+
40+
return $types;
41+
}
42+
}

models/BaseDoc.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,6 @@ class BaseDoc extends BaseObject
5757
*/
5858
public $todos = [];
5959

60-
61-
/**
62-
* @param Type|null $aggregatedType
63-
* @return string[]
64-
*/
65-
protected function splitTypes($aggregatedType)
66-
{
67-
if ($aggregatedType === null) {
68-
return [];
69-
}
70-
71-
$types = [];
72-
foreach ($aggregatedType as $type) {
73-
$types[] = (string) $type;
74-
}
75-
76-
return $types ?: [(string) $aggregatedType];
77-
}
78-
7960
/**
8061
* Checks if doc has tag of a given name
8162
* @param string $name tag name

models/EventDoc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
*/
2121
class EventDoc extends ConstDoc
2222
{
23+
/**
24+
* @var string|null
25+
*/
2326
public $type;
27+
/**
28+
* @var string[]|null
29+
*/
2430
public $types;
2531

2632

models/FunctionDoc.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace yii\apidoc\models;
99

10-
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
1110
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1211
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
1312
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
1413
use phpDocumentor\Reflection\Php\Method;
14+
use yii\apidoc\helpers\TypeHelper;
1515
use yii\helpers\StringHelper;
1616

1717
/**
@@ -63,7 +63,7 @@ public function __construct($reflector = null, $context = null, $config = [])
6363

6464
foreach ($this->tags as $i => $tag) {
6565
if ($tag instanceof Throws) {
66-
$this->exceptions[implode($this->splitTypes($tag->getType()))] = $tag->getDescription();
66+
$this->exceptions[implode(TypeHelper::splitType($tag->getType()))] = $tag->getDescription();
6767
unset($this->tags[$i]);
6868
} elseif ($tag instanceof Param) {
6969
$paramName = '$' . $tag->getVariableName();
@@ -77,11 +77,11 @@ public function __construct($reflector = null, $context = null, $config = [])
7777
}
7878
$this->params[$paramName]->description = StringHelper::mb_ucfirst($tag->getDescription());
7979
$this->params[$paramName]->type = (string) $tag->getType();
80-
$this->params[$paramName]->types = $this->splitTypes($tag->getType());
80+
$this->params[$paramName]->types = TypeHelper::splitType($tag->getType());
8181
unset($this->tags[$i]);
8282
} elseif ($tag instanceof Return_) {
8383
$this->returnType = (string) $tag->getType();
84-
$this->returnTypes = $this->splitTypes($tag->getType());
84+
$this->returnTypes = TypeHelper::splitType($tag->getType());
8585
$this->return = StringHelper::mb_ucfirst($tag->getDescription());
8686
unset($this->tags[$i]);
8787
} elseif ($this->isInheritdocTag($tag)) {

models/ParamDoc.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace yii\apidoc\models;
1010

1111
use phpDocumentor\Reflection\Php\Argument;
12+
use yii\apidoc\helpers\TypeHelper;
1213
use yii\base\BaseObject;
1314

1415
/**
@@ -20,7 +21,6 @@
2021
class ParamDoc extends BaseObject
2122
{
2223
public $name;
23-
public $typeHint;
2424
public $isOptional;
2525
/**
2626
* @var string|null
@@ -51,11 +51,10 @@ public function __construct($reflector = null, $context = null, $config = [])
5151

5252
if ($reflector !== null) {
5353
$this->name = $reflector->getName();
54-
$this->typeHint = (string) $reflector->getType();
5554

5655
if ($this->type === null) {
57-
$this->type = $this->typeHint;
58-
$this->types = [$this->type];
56+
$this->type = (string) $reflector->getType();
57+
$this->types = TypeHelper::splitType($reflector->getType());
5958
}
6059

6160
if (PHP_VERSION_ID >= 80100) {
@@ -70,9 +69,5 @@ public function __construct($reflector = null, $context = null, $config = [])
7069
}
7170

7271
$this->name = '$' . $this->name;
73-
74-
if ($this->typeHint === 'mixed') {
75-
$this->typeHint = '';
76-
}
7772
}
7873
}

models/PropertyDoc.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
1111
use phpDocumentor\Reflection\Php\Property;
12+
use yii\apidoc\helpers\TypeHelper;
1213
use yii\helpers\StringHelper;
1314

1415
/**
@@ -24,7 +25,13 @@ class PropertyDoc extends BaseDoc
2425
{
2526
public $visibility;
2627
public $isStatic;
28+
/**
29+
* @var string|null
30+
*/
2731
public $type;
32+
/**
33+
* @var string[]|null
34+
*/
2835
public $types;
2936
/**
3037
* @var string|null
@@ -80,7 +87,7 @@ public function __construct($reflector = null, $context = null, $config = [])
8087
foreach ($this->tags as $tag) {
8188
if ($tag instanceof Var_) {
8289
$this->type = (string) $tag->getType();
83-
$this->types = $this->splitTypes($tag->getType());
90+
$this->types = TypeHelper::splitType($tag->getType());
8491

8592
$this->description = StringHelper::mb_ucfirst($tag->getDescription());
8693
$this->shortDescription = BaseDoc::extractFirstSentence($this->description);

models/TypeDoc.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
1414
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
1515
use phpDocumentor\Reflection\Php\Class_;
16+
use yii\apidoc\helpers\TypeHelper;
1617
use yii\helpers\StringHelper;
1718

1819
/**
@@ -212,7 +213,7 @@ public function __construct($reflector = null, $context = null, $config = [])
212213
'visibility' => 'public',
213214
'definedBy' => $this->name,
214215
'type' => (string) $tag->getType(),
215-
'types' => $this->splitTypes($tag->getType()),
216+
'types' => TypeHelper::splitType($tag->getType()),
216217
'shortDescription' => $shortDescription,
217218
'description' => $tag->getDescription(),
218219
]);
@@ -229,9 +230,8 @@ public function __construct($reflector = null, $context = null, $config = [])
229230
$params[] = new ParamDoc(null, $context, [
230231
'sourceFile' => $this->sourceFile,
231232
'name' => $parameter->getName(),
232-
'typeHint' => (string) $argumentType,
233233
'type' => (string) $argumentType,
234-
'types' => $this->splitTypes($argumentType),
234+
'types' => TypeHelper::splitType($argumentType),
235235
]);
236236
}
237237

@@ -249,7 +249,7 @@ public function __construct($reflector = null, $context = null, $config = [])
249249
'isStatic' => $tag->isStatic(),
250250
'return' => ' ',
251251
'returnType' => (string) $tag->getReturnType(),
252-
'returnTypes' => $this->splitTypes($tag->getReturnType()),
252+
'returnTypes' => TypeHelper::splitType($tag->getReturnType()),
253253
]);
254254
$method->definedBy = $this->name;
255255
$this->methods[$method->name] = $method;

templates/html/ApiRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function renderMethodSignature($method, $context = null)
272272
{
273273
$params = [];
274274
foreach ($method->params as $param) {
275-
$params[] = (empty($param->typeHint) ? '' : '<span class="signature-type">' . $this->createTypeLink($param->typeHint, $context) . '</span> ')
275+
$params[] = '<span class="signature-type">' . $this->createTypeLink($param->types, $context) . '</span> '
276276
. ($param->isPassedByReference ? '<b>&</b>' : '')
277277
. ApiMarkdown::highlight(
278278
$param->name

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ <h2>Method Details</h2>
437437

438438
<table class="detail-table table table-striped table-bordered table-hover">
439439
<tr><td colspan="3" class="signature">
440-
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#getSomething()-detail">getSomething</a></strong> ( <span style="color: #0000BB">$test</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$test2</span>, <span class="signature-type">int|string</span> <span style="color: #0000BB">$test3</span> )</td></tr>
440+
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#getSomething()-detail">getSomething</a></strong> ( <span class="signature-type"><a href="https://www.php.net/language.types.mixed">mixed</a></span> <span style="color: #0000BB">$test</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$test2</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a>|<a href="https://www.php.net/language.types.string">string</a></span> <span style="color: #0000BB">$test3</span> )</td></tr>
441441

442442
<tr>
443443
<td class="param-name-col"><span style="color: #0000BB">$test</span></td>
@@ -493,7 +493,7 @@ <h2>Method Details</h2>
493493

494494
<table class="detail-table table table-striped table-bordered table-hover">
495495
<tr><td colspan="3" class="signature">
496-
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.boolean">boolean</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder</a></strong> ( <span style="color: #0000BB">$date</span> )</td></tr>
496+
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.boolean">boolean</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder</a></strong> ( <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$date</span> )</td></tr>
497497

498498
<tr>
499499
<td class="param-name-col"><span style="color: #0000BB">$date</span></td>

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__2.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ <h2>Method Details</h2>
323323

324324
<table class="detail-table table table-striped table-bordered table-hover">
325325
<tr><td colspan="3" class="signature">
326-
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#getSomething()-detail">getSomething</a></strong> ( <span style="color: #0000BB">$test</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$test2</span>, <span class="signature-type">int|string</span> <span style="color: #0000BB">$test3</span> )</td></tr>
326+
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#getSomething()-detail">getSomething</a></strong> ( <span class="signature-type"><a href="https://www.php.net/language.types.mixed">mixed</a></span> <span style="color: #0000BB">$test</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$test2</span>, <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a>|<a href="https://www.php.net/language.types.string">string</a></span> <span style="color: #0000BB">$test3</span> )</td></tr>
327327

328328
<tr>
329329
<td class="param-name-col"><span style="color: #0000BB">$test</span></td>
@@ -382,7 +382,7 @@ <h2>Method Details</h2>
382382

383383
<table class="detail-table table table-striped table-bordered table-hover">
384384
<tr><td colspan="3" class="signature">
385-
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.boolean">boolean</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder</a></strong> ( <span style="color: #0000BB">$date</span> )</td></tr>
385+
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.boolean">boolean</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder</a></strong> ( <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <span style="color: #0000BB">$date</span> )</td></tr>
386386

387387
<tr>
388388
<td class="param-name-col"><span style="color: #0000BB">$date</span></td>
@@ -436,9 +436,17 @@ <h2>Method Details</h2>
436436

437437
<table class="detail-table table table-striped table-bordered table-hover">
438438
<tr><td colspan="3" class="signature">
439-
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.mixed">mixed</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-cat.html#methodWithoutDocAndTypeHints()-detail">methodWithoutDocAndTypeHints</a></strong> ( )</td></tr>
439+
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.mixed">mixed</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-cat.html#methodWithoutDocAndTypeHints()-detail">methodWithoutDocAndTypeHints</a></strong> ( <span class="signature-type"><a href="https://www.php.net/language.types.mixed">mixed</a></span> <span style="color: #0000BB">$param</span> )</td></tr>
440440

441-
</table>
441+
<tr>
442+
<td class="param-name-col"><span style="color: #0000BB">$param</span></td>
443+
<td class="param-type-col"><a href="https://www.php.net/language.types.mixed">mixed</a></td>
444+
<td class="param-desc-col">
445+
</td>
446+
</tr>
447+
448+
449+
</table>
442450

443451

444452

@@ -451,9 +459,9 @@ <h2>Method Details</h2>
451459
<div class="collapse">
452460
<div class="card card-body">
453461
<pre>
454-
<code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">methodWithoutDocAndTypeHints</span><span class="hljs-params">()</span>
462+
<code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">methodWithoutDocAndTypeHints</span><span class="hljs-params">($param)</span>
455463
</span>{
456-
<span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
464+
<span class="hljs-keyword">return</span> $param;
457465
}
458466
</code>
459467
</pre>

0 commit comments

Comments
 (0)