Skip to content

Commit 032228e

Browse files
feat: New Kirby\Reflection\DocComment class to parse docblock comments
1 parent 5e5f093 commit 032228e

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/Reflection/DocComment.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use ReflectionClass;
6+
use ReflectionFunction;
7+
use ReflectionMethod;
8+
use ReflectionProperty;
9+
10+
class DocComment
11+
{
12+
public function __construct(
13+
protected string $comment,
14+
) {
15+
}
16+
17+
public static function from(
18+
ReflectionClass|ReflectionFunction|ReflectionMethod|ReflectionProperty|null $object
19+
) {
20+
return new static($object?->getDocComment() ?? '');
21+
}
22+
23+
/**
24+
* Returns the cleaned docblock text of the given property.
25+
*/
26+
public function description(): string|null
27+
{
28+
$comment = preg_replace(['#^/\*\*#', '#\*/$#'], '', $this->comment);
29+
$lines = preg_split('/\R/', (string)$comment) ?: [];
30+
$lines = array_map(static fn (string $line): string => ltrim(trim($line), "* \t"), $lines);
31+
$lines = array_filter(
32+
$lines,
33+
static fn (string $line): bool => $line !== '' && str_starts_with($line, '@') === false
34+
);
35+
36+
return implode(' ', $lines);
37+
}
38+
}

0 commit comments

Comments
 (0)