Skip to content

Commit 3a69b8b

Browse files
committed
added support for return $this in methods
1 parent ce0b1b0 commit 3a69b8b

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

renderers/BaseRenderer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public function createTypeLink($types, $context = null, $title = null, $options
8585
$type = substr($type, 0, -2);
8686
}
8787

88-
if (($t = $this->apiContext->getType(ltrim($type, '\\'))) !== null) {
88+
if ($type === '$this' && $context instanceof TypeDoc) {
89+
$title = '$this';
90+
$type = $context;
91+
} elseif (($t = $this->apiContext->getType(ltrim($type, '\\'))) !== null) {
8992
$type = $t;
9093
} elseif ($type[0] !== '\\' && ($t = $this->apiContext->getType($this->resolveNamespace($context) . '\\' . ltrim($type, '\\'))) !== null) {
9194
$type = $t;
@@ -97,6 +100,7 @@ public function createTypeLink($types, $context = null, $title = null, $options
97100
$linkText = ltrim($type, '\\');
98101
if ($title !== null) {
99102
$linkText = $title;
103+
$title = null;
100104
}
101105
$phpTypes = [
102106
'callable',
@@ -122,6 +126,7 @@ public function createTypeLink($types, $context = null, $title = null, $options
122126
$linkText = $type->name;
123127
if ($title !== null) {
124128
$linkText = $title;
129+
$title = null;
125130
}
126131
$links[] = $this->generateLink($linkText, $this->generateApiUrl($type->name), $options) . $postfix;
127132
}

templates/html/ApiRenderer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,40 +224,40 @@ public function renderClasses($names)
224224
* @param PropertyDoc $property
225225
* @return string
226226
*/
227-
public function renderPropertySignature($property)
227+
public function renderPropertySignature($property, $context = null)
228228
{
229229
if ($property->getter !== null || $property->setter !== null) {
230230
$sig = [];
231231
if ($property->getter !== null) {
232-
$sig[] = $this->renderMethodSignature($property->getter);
232+
$sig[] = $this->renderMethodSignature($property->getter, $context);
233233
}
234234
if ($property->setter !== null) {
235-
$sig[] = $this->renderMethodSignature($property->setter);
235+
$sig[] = $this->renderMethodSignature($property->setter, $context);
236236
}
237237

238238
return implode('<br />', $sig);
239239
}
240240

241-
return $this->createTypeLink($property->types) . ' ' . $this->createSubjectLink($property, $property->name) . ' '
241+
return $this->createTypeLink($property->types, $context) . ' ' . $this->createSubjectLink($property, $property->name) . ' '
242242
. ApiMarkdown::highlight('= ' . ($property->defaultValue === null ? 'null' : $property->defaultValue), 'php');
243243
}
244244

245245
/**
246246
* @param MethodDoc $method
247247
* @return string
248248
*/
249-
public function renderMethodSignature($method)
249+
public function renderMethodSignature($method, $context = null)
250250
{
251251
$params = [];
252252
foreach ($method->params as $param) {
253-
$params[] = (empty($param->typeHint) ? '' : $param->typeHint . ' ')
253+
$params[] = (empty($param->typeHint) ? '' : $this->createTypeLink($param->typeHint, $context) . ' ')
254254
. ($param->isPassedByReference ? '<b>&</b>' : '')
255255
. $param->name
256256
. ($param->isOptional ? ' = ' . $param->defaultValue : '');
257257
}
258258

259259
return ($method->isReturnByReference ? '<b>&</b>' : '')
260-
. ($method->returnType === null ? 'void' : $this->createTypeLink($method->returnTypes))
260+
. ($method->returnType === null ? 'void' : $this->createTypeLink($method->returnTypes, $context))
261261
. ' <strong>' . $this->createSubjectLink($method, $method->name) . '</strong>'
262262
. ApiMarkdown::highlight(str_replace(' ', ' ', '( ' . implode(', ', $params) . ' )'), 'php');
263263
}

templates/html/views/methodDetails.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</div>
5454

5555
<table class="detail-table table table-striped table-bordered table-hover">
56-
<tr><td colspan="3" class="signature"><?= $renderer->renderMethodSignature($method) ?></td></tr>
56+
<tr><td colspan="3" class="signature"><?= $renderer->renderMethodSignature($method, $type) ?></td></tr>
5757
<?php if (!empty($method->params) || !empty($method->return) || !empty($method->exceptions)): ?>
5858
<?php foreach ($method->params as $param): ?>
5959
<tr>
@@ -65,7 +65,7 @@
6565
<?php if (!empty($method->return)): ?>
6666
<tr>
6767
<th class="param-name-col">return</th>
68-
<td class="param-type-col"><?= $renderer->createTypeLink($method->returnTypes) ?></td>
68+
<td class="param-type-col"><?= $renderer->createTypeLink($method->returnTypes, $type) ?></td>
6969
<td class="param-desc-col"><?= ApiMarkdown::process($method->return, $type) ?></td>
7070
</tr>
7171
<?php endif; ?>

templates/html/views/propertyDetails.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
</span>
4747
</div>
4848

49-
<div class="signature"><?php echo $renderer->renderPropertySignature($property); ?></div>
49+
<div class="signature"><?php echo $renderer->renderPropertySignature($property, $type); ?></div>
5050

5151
<?= ApiMarkdown::process($property->description, $type) ?>
5252

0 commit comments

Comments
 (0)