Skip to content

Commit a0b8555

Browse files
committed
Class and method attribute support
1 parent c5a457e commit a0b8555

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

src/PHPFUI/InstaDoc/Section/CodeCommon.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
119119
$ul->addItem(new \PHPFUI\ListItem($this->getColor('name', $name) . ' ' . $this->getColor('description', $body)));
120120
}
121121

122+
$attributes = $this->getAttributes($reflectionMethod);
123+
foreach ($attributes as $attribute)
124+
{
125+
$ul->addItem(new \PHPFUI\ListItem($this->getColor('name', 'attribute') . ' ' . $this->formatAttribute($attribute)));
126+
}
127+
122128
$container->add($ul);
123129
}
124130

@@ -211,6 +217,11 @@ protected function getComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\
211217

212218
protected function getDocBlock($method) : ?\phpDocumentor\Reflection\DocBlock
213219
{
220+
/**
221+
* @todo get attributes everywhere
222+
* $attributes = $this->getAttributes($method);
223+
*/
224+
214225
$comments = $method->getDocComment();
215226
$comments = \str_replace('{@inheritdoc}', '@inheritdoc', $comments);
216227

@@ -342,6 +353,7 @@ protected function getParameters($method) : string
342353
{
343354
$info = '(';
344355
$comma = '';
356+
345357
$docBlock = $this->getDocBlock($method);
346358

347359
$parameterComments = $this->getParameterComments($docBlock);
@@ -361,6 +373,11 @@ protected function getParameters($method) : string
361373
$name = $parameter->getName();
362374
$tip = '$' . $name;
363375

376+
/**
377+
* @todo add attributes for parameters
378+
* $attributes = $this->getAttributes($parameter);
379+
*/
380+
364381
if (isset($parameterComments[$name]))
365382
{
366383
$tip = new \PHPFUI\ToolTip($tip, $parameterComments[$name]);
@@ -479,4 +496,91 @@ protected function section(string $name) : string
479496

480497
return $section;
481498
}
499+
500+
protected function getAttributes($reflection) : array
501+
{
502+
if ($reflection && method_exists($reflection, 'getAttributes'))
503+
{
504+
return $reflection->getAttributes();
505+
}
506+
507+
return [];
508+
}
509+
510+
private function getAttributeName(string $name, bool $asValue = false) : string
511+
{
512+
$link = $this->getClassName($name);
513+
if (strpos($link, 'href='))
514+
{
515+
$name = $link;
516+
}
517+
elseif ($asValue)
518+
{
519+
$name = $this->getValueString($name);
520+
}
521+
522+
return $name;
523+
}
524+
525+
protected function formatAttribute(\ReflectionAttribute $attribute) : string
526+
{
527+
$parameters = '';
528+
$arguments = $attribute->getArguments();
529+
if ($arguments)
530+
{
531+
$parameters = ' (';
532+
$comma = '';
533+
foreach ($arguments as $name => $argument)
534+
{
535+
$name = is_int($name) ? '' : $this->getAttributeName($name) . ': ';
536+
if (is_string($argument))
537+
{
538+
$link = $this->getAttributeName($argument, true);
539+
}
540+
else
541+
{
542+
$link = $this->getValueString($argument);
543+
}
544+
$parameters .= "{$comma} {$name}{$link}";
545+
546+
$comma = ', ';
547+
}
548+
$parameters .= ')';
549+
}
550+
551+
$targeting = '';
552+
/*
553+
554+
Not sure how useful this is, so commenting out for now.
555+
556+
$target = $attribute->getTarget();
557+
$targets = [];
558+
$definedTargets = [
559+
"CLASS" => \Attribute::TARGET_CLASS,
560+
"FUNCTION" => \Attribute::TARGET_FUNCTION,
561+
"METHOD" => \Attribute::TARGET_METHOD,
562+
"PROPERTY" => \Attribute::TARGET_PROPERTY,
563+
"CLASS_CONSTANT" => \Attribute::TARGET_CLASS_CONSTANT,
564+
"PARAMETER" => \Attribute::TARGET_PARAMETER,
565+
];
566+
foreach ($definedTargets as $name => $value)
567+
{
568+
if ($target & $value)
569+
{
570+
$targets[] = '\\Attribute::TARGET_' . $name;
571+
}
572+
}
573+
if ($attribute->isRepeated())
574+
{
575+
$targets[] = '\\Attribute::IS_REPEATABLE';
576+
}
577+
if ($targets)
578+
{
579+
$targeting = ' ' . implode(' | ', $targets);
580+
}
581+
*/
582+
583+
return $this->getClassName($attribute->getName()) . $parameters . $targeting;
584+
}
585+
482586
}

src/PHPFUI/InstaDoc/Section/Doc.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ public function generate(\PHPFUI\Instadoc\PageInterface $page, string $fullClass
159159
}
160160
}
161161

162+
$reflectionAttributes = $this->getAttributes($this->reflection);
163+
164+
if ($reflectionAttributes)
165+
{
166+
$table = new \PHPFUI\Table();
167+
$table->addClass('hover');
168+
$table->addClass('unstriped');
169+
170+
foreach ($reflectionAttributes as $attribute)
171+
{
172+
$table->addRow([$this->formatAttribute($attribute)]);
173+
}
174+
175+
if (\count($table))
176+
{
177+
$accordion->addTab('Attributes', $table);
178+
}
179+
}
180+
162181
if (\count($accordion))
163182
{
164183
$container->add($accordion);
@@ -245,6 +264,11 @@ protected function getAccess($constant) : string
245264

246265
protected function getConstant(\ReflectionClassConstant $constant, string $name, $value) : string
247266
{
267+
/**
268+
* @todo get attributes everywhere
269+
* $attributes = $this->getAttributes($constant);
270+
*/
271+
248272
$docBlock = $this->getDocBlock($constant);
249273
$info = $this->getAccess($constant) . ' ' . $this->getColor('constant', $this->getColor('constant', $this->getName($constant, $name, true))) . ' = ' . $this->getValueString($value);
250274
$info .= $this->getComments($docBlock);
@@ -270,6 +294,11 @@ protected function getContent(string $accessType) : \PHPFUI\Table
270294
{
271295
$constant = new \ReflectionClassConstant($this->class, $name);
272296

297+
/**
298+
* @todo get attributes everywhere
299+
* $attributes = $this->getAttributes($constant);
300+
*/
301+
273302
if ($constant->{$accessType}())
274303
{
275304
if ($section)

src/PHPFUI/InstaDoc/Section/Functions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ private function documentFunction(string $function) : \PHPFUI\Container
8383
'Variadic',
8484
];
8585

86+
/**
87+
* @todo get attributes everywhere
88+
* $attributes = $this->getAttributes($this->reflection);
89+
*/
90+
8691
$gridX = null;
8792

8893
foreach ($properties as $type)

src/PHPFUI/InstaDoc/Tests/Test80.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@ private function UpperCaseMethodName() : mixed { return null; }
9090
* Testing method sorting
9191
*/
9292
private function upperTest() : void {}
93+
94+
#[\ReturnTypeWillChange]
95+
public function count()
96+
{
97+
return 0;
98+
}
9399
}

src/PHPFUI/InstaDoc/Tests/Test80B.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
*
1010
* @author bruce (1/3/2020)
1111
*/
12+
#[Test80]
13+
#[\PHPFUI\InstaDoc\Tests\Test80]
14+
#[\PHPFUI\InstaDoc\Tests\Test80(1234), \PHPFUI\InstaDoc\Tests\Test80(value: 1234), \PHPFUI\InstaDoc\Tests\Test80(array("key" => "value"))]
15+
#[\PHPFUI\InstaDoc\Tests\Test80(\PHPFUI\InstaDoc\Tests\Test80::CONST_PUBLIC_STRING)]
16+
#[\PHPFUI\InstaDoc\Tests\Test80(100 + 200)]
17+
#[Property(type: 'function', name: 'Hello')]
18+
#[Listens(\PHPFUI\Page::class)]
19+
#[Route(\PHPFUI\InstaDoc\Controller::CLASS_NAME, '/products/create')]
1220
class Test80B extends Test80A
1321
{
1422
/**
@@ -20,4 +28,5 @@ protected function protected_function_no_return(?string $fred, $unknown = 3.14)
2028
* {@inheritDoc}
2129
*/
2230
private function private_function_no_return(string | Test80 | null $fred = 'Eythel') : void {}
31+
2332
}

0 commit comments

Comments
 (0)