Skip to content

Commit 9feece6

Browse files
committed
fix extract info from trait
1 parent d73da85 commit 9feece6

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/FunctionalDocBlockExtractor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function enterNode(Node $node): null
3737
if ($node instanceof Node\Stmt\Class_ && $node->name) {
3838
$this->currentClassName = $node->name->toString();
3939
}
40+
if ($node instanceof Node\Stmt\Trait_ && $node->name) {
41+
$this->currentClassName = $node->name->toString();
42+
}
4043

4144
// Process the doc comment if it exists
4245
if ($node->getDocComment()) {
@@ -63,6 +66,9 @@ public function leaveNode(Node $node): null
6366
if ($node instanceof Node\Stmt\Class_) {
6467
$this->currentClassName = null;
6568
}
69+
if ($node instanceof Node\Stmt\Trait_) {
70+
$this->currentClassName = null;
71+
}
6672
if ($node instanceof Node\Stmt\Namespace_) {
6773
$this->currentNamespace = null;
6874
}
@@ -230,6 +236,11 @@ private function getOwnerIdentifier(Node $node): string
230236

231237
return ltrim($fqcn, '\\');
232238
}
239+
if ($node instanceof Node\Stmt\Trait_) {
240+
$fqcn = $namespace.$node->name->toString();
241+
242+
return ltrim($fqcn, '\\');
243+
}
233244
if ($node instanceof ClassMethod && $this->currentClassName) {
234245
$fqcn = $namespace.$this->currentClassName.'::'.$node->name->toString();
235246

@@ -249,6 +260,9 @@ private function getDefaultTitleForNode(Node $node): string
249260
if ($node instanceof Node\Stmt\Class_ && $node->name) {
250261
return $node->name->toString();
251262
}
263+
if ($node instanceof Node\Stmt\Trait_ && $node->name) {
264+
return $node->name->toString();
265+
}
252266
if ($node instanceof ClassMethod && $this->currentClassName) {
253267
return $this->currentClassName.'::'.$node->name->toString();
254268
}

tests/Unit/FunctionalDocBlockExtractorTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,87 @@ class ChildService {}
378378
expect($doc['navId'])->toBe('child-service');
379379
expect($doc['navParent'])->toBe('parent-service');
380380
});
381+
382+
it('can extract functional documentation from trait docblocks', function () {
383+
$extractor = new FunctionalDocBlockExtractor;
384+
$extractor->setCurrentFilePath('/test/path/TestTrait.php');
385+
386+
$phpCode = <<<'PHP'
387+
<?php
388+
namespace App\Traits;
389+
390+
/**
391+
* Timestamp handling trait
392+
*
393+
* @functional
394+
* This trait provides timestamp functionality for models.
395+
*
396+
* # Key Features
397+
* - Automatic timestamp management
398+
* - Custom timestamp formats
399+
*
400+
* @nav Traits / Timestamp Handler
401+
* @uses \Carbon\Carbon
402+
* @link https://example.com/traits
403+
*/
404+
trait TimestampHandler
405+
{
406+
public function updateTimestamps() {}
407+
}
408+
PHP;
409+
410+
$parser = (new ParserFactory)->createForNewestSupportedVersion();
411+
$traverser = new NodeTraverser;
412+
$traverser->addVisitor($extractor);
413+
414+
$ast = $parser->parse($phpCode);
415+
$traverser->traverse($ast);
416+
417+
expect($extractor->foundDocs)->toHaveCount(1);
418+
419+
$doc = $extractor->foundDocs[0];
420+
expect($doc['owner'])->toBe('App\Traits\TimestampHandler');
421+
expect($doc['navPath'])->toBe('Traits / Timestamp Handler');
422+
expect($doc['description'])->toContain('This trait provides timestamp functionality');
423+
expect($doc['uses'])->toContain('\Carbon\Carbon');
424+
expect($doc['links'])->toContain('https://example.com/traits');
425+
expect($doc['sourceFile'])->toBe('/test/path/TestTrait.php');
426+
});
427+
428+
it('can extract functional documentation from trait methods', function () {
429+
$extractor = new FunctionalDocBlockExtractor;
430+
$extractor->setCurrentFilePath('/test/path/TestTrait.php');
431+
432+
$phpCode = <<<'PHP'
433+
<?php
434+
namespace App\Traits;
435+
436+
trait TimestampHandler
437+
{
438+
/**
439+
* Update timestamps method
440+
*
441+
* @functional
442+
* This method updates the created_at and updated_at timestamps.
443+
*
444+
* @nav Traits / Update Timestamps Process
445+
* @uses \Carbon\Carbon
446+
*/
447+
public function updateTimestamps() {}
448+
}
449+
PHP;
450+
451+
$parser = (new ParserFactory)->createForNewestSupportedVersion();
452+
$traverser = new NodeTraverser;
453+
$traverser->addVisitor($extractor);
454+
455+
$ast = $parser->parse($phpCode);
456+
$traverser->traverse($ast);
457+
458+
expect($extractor->foundDocs)->toHaveCount(1);
459+
460+
$doc = $extractor->foundDocs[0];
461+
expect($doc['owner'])->toBe('App\Traits\TimestampHandler::updateTimestamps');
462+
expect($doc['navPath'])->toBe('Traits / Update Timestamps Process');
463+
expect($doc['description'])->toContain('This method updates the created_at and updated_at timestamps');
464+
});

0 commit comments

Comments
 (0)