WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e9d83d7

Browse files
authored
Fix #349: Add support for constants in interfaces and traits
1 parent 453a7f8 commit e9d83d7

14 files changed

+189
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Yii Framework 2 apidoc extension Change Log
1414
- Enh #341: Add support for nullable types (mspirkov)
1515
- Enh #347: Add PHPDoc types for class properties (mspirkov)
1616
- Enh #348: Inherit descriptions of the parent class (mspirkov)
17+
- Enh #349: Add support for constants in interfaces and traits (mspirkov)
1718

1819

1920
3.0.8 November 24, 2025

models/ClassDoc.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ class ClassDoc extends TypeDoc
4545
* @var string[]
4646
*/
4747
public $subclasses = [];
48-
/**
49-
* @var EventDoc[]
50-
*/
51-
public $events = [];
52-
/**
53-
* @var ConstDoc[]
54-
*/
55-
public $constants = [];
5648

5749

5850
/**
@@ -120,17 +112,5 @@ public function __construct($reflector = null, $context = null, $config = [])
120112
foreach ($reflector->getUsedTraits() as $trait) {
121113
$this->traits[] = ltrim($trait, '\\');
122114
}
123-
foreach ($reflector->getConstants() as $constantReflector) {
124-
$docBlock = $constantReflector->getDocBlock();
125-
if ($docBlock !== null && count($docBlock->getTagsByName('event')) > 0) {
126-
$event = new EventDoc($this, $constantReflector, null, [], $docBlock);
127-
$event->definedBy = $this->name;
128-
$this->events[$event->name] = $event;
129-
} else {
130-
$constant = new ConstDoc($this, $constantReflector);
131-
$constant->definedBy = $this->name;
132-
$this->constants[$constant->name] = $constant;
133-
}
134-
}
135115
}
136116
}

models/ConstDoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ConstDoc extends BaseDoc
3030

3131

3232
/**
33-
* @param ClassDoc|TraitDoc $parent
33+
* @param TypeDoc $parent
3434
* @param Constant|null $reflector
3535
* @param Context|null $context
3636
* @param array $config

models/EventDoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EventDoc extends ConstDoc
2929

3030

3131
/**
32-
* @param ClassDoc|TraitDoc $parent
32+
* @param TypeDoc $parent
3333
* @param Class_|Constant|null $reflector
3434
* @param Context|null $context
3535
* @param array $config

models/TypeDoc.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ class TypeDoc extends BaseDoc
4545
* @var PropertyDoc[]
4646
*/
4747
public $properties = [];
48+
/**
49+
* @var array<string, ConstDoc>
50+
*/
51+
public $constants = [];
52+
/**
53+
* @var array<string, EventDoc>
54+
*/
55+
public $events = [];
4856
/**
4957
* @var string
5058
*/
@@ -271,6 +279,19 @@ public function __construct($reflector = null, $context = null, $config = [])
271279
$this->methods[$method->name] = $method;
272280
}
273281
}
282+
283+
foreach ($reflector->getConstants() as $constantReflector) {
284+
$docBlock = $constantReflector->getDocBlock();
285+
if ($docBlock !== null && count($docBlock->getTagsByName('event')) > 0) {
286+
$event = new EventDoc($this, $constantReflector, null, [], $docBlock);
287+
$event->definedBy = $this->name;
288+
$this->events[$event->name] = $event;
289+
} else {
290+
$constant = new ConstDoc($this, $constantReflector);
291+
$constant->definedBy = $this->name;
292+
$this->constants[$constant->name] = $constant;
293+
}
294+
}
274295
}
275296

276297
/**

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ parameters:
2121
path: renderers/BaseRenderer.php
2222

2323
-
24-
message: "#^Property yii\\\\apidoc\\\\models\\\\ClassDoc\\:\\:\\$events \\(array\\<yii\\\\apidoc\\\\models\\\\EventDoc\\>\\) does not accept array\\<yii\\\\apidoc\\\\models\\\\ConstDoc\\|yii\\\\apidoc\\\\models\\\\MethodDoc\\|yii\\\\apidoc\\\\models\\\\PropertyDoc\\>\\.$#"
24+
message: "#^Property yii\\\\apidoc\\\\models\\\\TypeDoc\\:\\:\\$events \\(array\\<string\\, yii\\\\apidoc\\\\models\\\\EventDoc\\>\\) does not accept array\\<string\\, yii\\\\apidoc\\\\models\\\\ConstDoc\\|yii\\\\apidoc\\\\models\\\\MethodDoc\\|yii\\\\apidoc\\\\models\\\\PropertyDoc\\>\\.$#"
2525
count: 1
2626
path: models/Context.php
2727

templates/html/views/type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
<?php if (!empty($type->methods)): ?>
4242
| <a href="#methods">Methods</a>
4343
<?php endif; ?>
44-
<?php if ($type instanceof ClassDoc && !empty($type->events)): ?>
44+
<?php if (!empty($type->events)): ?>
4545
| <a href="#events">Events</a>
4646
<?php endif; ?>
47-
<?php if ($type instanceof ClassDoc && !empty($type->constants)): ?>
47+
<?php if (!empty($type->constants)): ?>
4848
| <a href="#constants">Constants</a>
4949
<?php endif; ?>
5050
</div>

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ Array
331331

332332
[47] => Array
333333
(
334-
[line] => 24
334+
[line] => 28
335335
[file] => /tests/data/api/db/ActiveQuery.php
336336
[message] => No short description for Method 'one'
337337
)
338338

339339
[48] => Array
340340
(
341-
[line] => 31
341+
[line] => 35
342342
[file] => /tests/data/api/db/ActiveQuery.php
343343
[message] => No short description for Method 'all'
344344
)
@@ -352,14 +352,14 @@ Array
352352

353353
[50] => Array
354354
(
355-
[line] => 13
355+
[line] => 16
356356
[file] => /tests/data/api/db/ActiveQueryInterface.php
357357
[message] => No docblock for element 'one'
358358
)
359359

360360
[51] => Array
361361
(
362-
[line] => 14
362+
[line] => 17
363363
[file] => /tests/data/api/db/ActiveQueryInterface.php
364364
[message] => No docblock for element 'all'
365365
)

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__11.html

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@
6969
<h1>Class yiiunit\apidoc\data\api\db\ActiveQuery</h1>
7070
<div class="top-nav">
7171
<a href="index">All Classes</a>
72+
| <a>Properties</a>
7273
| <a>Methods</a>
73-
</div>
74+
| <a>Constants</a>
75+
</div>
7476

7577
<table class="summaryTable docClass table table-bordered">
7678
<colgroup>
@@ -85,7 +87,11 @@ <h1>Class yiiunit\apidoc\data\api\db\ActiveQuery</h1>
8587
<th>Implements</th>
8688
<td><a href="yiiunit-apidoc-data-api-db-activequeryinterface.html">yiiunit\apidoc\data\api\db\ActiveQueryInterface</a></td>
8789
</tr>
88-
</table>
90+
<tr>
91+
<th>Uses Traits</th>
92+
<td><a href="yiiunit-apidoc-data-api-db-activerelationtrait.html">yiiunit\apidoc\data\api\db\ActiveRelationTrait</a></td>
93+
</tr>
94+
</table>
8995

9096
<div class="class-description">
9197
<p><strong>ActiveQuery represents a DB query associated with an Active Record class.</strong></p>
@@ -98,6 +104,41 @@ <h1>Class yiiunit\apidoc\data\api\db\ActiveQuery</h1>
98104

99105
<a></a>
100106

107+
<div class="doc-property summary toggle-target-container">
108+
<h2>Public Properties</h2>
109+
110+
<p><a class="toggle">Hide inherited properties</a></p>
111+
112+
<table class="summary-table table table-striped table-bordered table-hover">
113+
<colgroup>
114+
<col class="col-property">
115+
<col class="col-type">
116+
<col class="col-description">
117+
<col class="col-defined">
118+
</colgroup>
119+
<tr>
120+
<th>Property</th>
121+
<th>Type</th>
122+
<th>Description</th>
123+
<th>Defined By</th>
124+
</tr>
125+
126+
<tr class="inherited">
127+
<td><a href="yiiunit-apidoc-data-api-db-activequery.html#%24modelClass-detail">$modelClass</a></td>
128+
<td>
129+
<a href="https://phpstan.org/writing-php-code/phpdoc-types#class-string">class-string</a>&lt;<a href="yiiunit-apidoc-data-api-db-activerecordinterface.html">yiiunit\apidoc\data\api\db\ActiveRecordInterface</a>&gt;</td>
130+
<td></td>
131+
<td><a href="yiiunit-apidoc-data-api-db-activerelationtrait.html">yiiunit\apidoc\data\api\db\ActiveRelationTrait</a></td>
132+
</tr>
133+
<tr class="inherited">
134+
<td><a href="yiiunit-apidoc-data-api-db-activequery.html#%24someProperty-detail">$someProperty</a></td>
135+
<td>(<a href="https://www.php.net/language.types.integer">integer</a>|<a href="https://www.php.net/language.types.string">string</a>)[]</td>
136+
<td></td>
137+
<td><a href="yiiunit-apidoc-data-api-db-activerelationtrait.html">yiiunit\apidoc\data\api\db\ActiveRelationTrait</a></td>
138+
</tr>
139+
</table>
140+
</div>
141+
101142
<a></a>
102143

103144
<div class="doc-method summary toggle-target-container">
@@ -149,6 +190,36 @@ <h2>Public Methods</h2>
149190

150191
<a></a>
151192

193+
<div class="doc-const summary toggle-target-container">
194+
<h2>Constants</h2>
195+
196+
<p><a class="toggle">Hide inherited constants</a></p>
197+
198+
<table class="summary-table table table-striped table-bordered table-hover">
199+
<colgroup>
200+
<col class="col-const">
201+
<col class="col-value">
202+
<col class="col-description">
203+
<col class="col-defined">
204+
</colgroup>
205+
<tr>
206+
<th>Constant</th>
207+
<th>Value</th>
208+
<th>Description</th>
209+
<th>Defined By</th>
210+
</tr>
211+
212+
<tr class="">
213+
<td>SOME_CLASS_CONST</td>
214+
<td>'someClassConst'</td>
215+
<td>
216+
217+
</td>
218+
<td><a href="yiiunit-apidoc-data-api-db-activequery.html">yiiunit\apidoc\data\api\db\ActiveQuery</a></td>
219+
</tr>
220+
</table>
221+
</div>
222+
152223

153224
<h2>Method Details</h2>
154225

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__12.html

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ <h1>Interface yiiunit\apidoc\data\api\db\ActiveQueryInterface</h1>
7070
<div class="top-nav">
7171
<a href="index">All Classes</a>
7272
| <a>Methods</a>
73-
</div>
73+
| <a>Constants</a>
74+
</div>
7475

7576
<table class="summaryTable docClass table table-bordered">
7677
<colgroup>
@@ -130,6 +131,44 @@ <h2>Public Methods</h2>
130131

131132
<a></a>
132133

134+
<div class="doc-const summary toggle-target-container">
135+
<h2>Constants</h2>
136+
137+
<p><a class="toggle">Hide inherited constants</a></p>
138+
139+
<table class="summary-table table table-striped table-bordered table-hover">
140+
<colgroup>
141+
<col class="col-const">
142+
<col class="col-value">
143+
<col class="col-description">
144+
<col class="col-defined">
145+
</colgroup>
146+
<tr>
147+
<th>Constant</th>
148+
<th>Value</th>
149+
<th>Description</th>
150+
<th>Defined By</th>
151+
</tr>
152+
153+
<tr class="">
154+
<td>FIRST_CONST</td>
155+
<td>'firstConst'</td>
156+
<td>
157+
158+
</td>
159+
<td><a href="yiiunit-apidoc-data-api-db-activequeryinterface.html">yiiunit\apidoc\data\api\db\ActiveQueryInterface</a></td>
160+
</tr>
161+
<tr class="">
162+
<td>SECOND_CONST</td>
163+
<td>'secondConst'</td>
164+
<td>
165+
166+
</td>
167+
<td><a href="yiiunit-apidoc-data-api-db-activequeryinterface.html">yiiunit\apidoc\data\api\db\ActiveQueryInterface</a></td>
168+
</tr>
169+
</table>
170+
</div>
171+
133172

134173
<h2>Method Details</h2>
135174

0 commit comments

Comments
 (0)