-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(documentator):
IncludeDocBlock
(include:docblock
) instruction.
- Loading branch information
1 parent
95e6b23
commit 8aa9a66
Showing
16 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,4 +258,5 @@ MD053: | |
"include:file", | ||
"include:package-list", | ||
"include:template", | ||
"include:docblock", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/documentator/src/Preprocessor/Exceptions/TargetIsNotValidPhpFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions; | ||
|
||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
class TargetIsNotValidPhpFile extends InstructionFailed { | ||
public function __construct(string $path, string $target, Throwable $previous = null) { | ||
parent::__construct( | ||
$path, | ||
$target, | ||
sprintf( | ||
'The `%s` is not a valid PHP file (in `%s`).', | ||
$target, | ||
$path, | ||
), | ||
$previous, | ||
); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions; | ||
|
||
use Exception; | ||
use LastDragon_ru\LaraASP\Documentator\PackageViewer; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Contracts\ParameterizableInstruction; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotFile; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotValidPhpFile; | ||
use LastDragon_ru\LaraASP\Documentator\Utils\Path; | ||
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; | ||
use Override; | ||
use phpDocumentor\Reflection\DocBlockFactory; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\NodeFinder; | ||
use PhpParser\ParserFactory; | ||
|
||
use function dirname; | ||
use function file_get_contents; | ||
use function trim; | ||
|
||
/** | ||
* @implements ParameterizableInstruction<IncludeDocBlockParameters> | ||
*/ | ||
class IncludeDocBlock implements ParameterizableInstruction { | ||
public function __construct( | ||
protected readonly PackageViewer $viewer, | ||
) { | ||
// empty | ||
} | ||
|
||
#[Override] | ||
public static function getName(): string { | ||
return 'include:docblock'; | ||
} | ||
|
||
#[Override] | ||
public static function getDescription(): string { | ||
return <<<'DESC' | ||
Includes the docblock of the first PHP class/interface/trait/enum/etc | ||
from `<target>` file. Inline tags include as is. Other tags are | ||
ignored. | ||
DESC; | ||
} | ||
|
||
#[Override] | ||
public static function getTargetDescription(): ?string { | ||
return 'File path.'; | ||
} | ||
|
||
#[Override] | ||
public static function getParameters(): string { | ||
return IncludeDocBlockParameters::class; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
#[Override] | ||
public static function getParametersDescription(): array { | ||
return [ | ||
'summary' => 'Include the class summary? (default `false`)', | ||
'description' => 'Include the class description? (default `true`)', | ||
]; | ||
} | ||
|
||
#[Override] | ||
public function process(string $path, string $target, Serializable $parameters): string { | ||
// File? | ||
$file = Path::getPath(dirname($path), $target); | ||
$content = file_get_contents($file); | ||
|
||
if ($content === false) { | ||
throw new TargetIsNotFile($path, $target); | ||
} | ||
|
||
// Class? | ||
try { | ||
$class = null; | ||
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); | ||
$stmts = (array) $parser->parse($content); | ||
$finder = new NodeFinder(); | ||
$class = $finder->findFirst($stmts, static function (Node $node): bool { | ||
return $node instanceof ClassLike; | ||
}); | ||
} catch (Exception $exception) { | ||
throw new TargetIsNotValidPhpFile($path, $target, $exception); | ||
} | ||
|
||
if (!($class instanceof ClassLike)) { | ||
return ''; | ||
} | ||
|
||
// DocBlock? | ||
$comment = $class->getDocComment()?->getText(); | ||
|
||
if (!$comment) { | ||
return ''; | ||
} | ||
|
||
// Parse | ||
$eol = "\n"; | ||
$result = ''; | ||
$factory = DocBlockFactory::createInstance(); | ||
$docblock = $factory->create($comment); | ||
|
||
if ($parameters->summary) { | ||
$summary = trim($docblock->getSummary()); | ||
|
||
if ($summary) { | ||
$result .= $summary.$eol.$eol; | ||
} | ||
} | ||
|
||
if ($parameters->description) { | ||
$description = trim((string) $docblock->getDescription()); | ||
|
||
if ($description) { | ||
$result .= $description.$eol.$eol; | ||
} | ||
} | ||
|
||
if ($result) { | ||
$result = trim($result).$eol; | ||
} | ||
|
||
// Return | ||
return $result; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockParameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions; | ||
|
||
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; | ||
|
||
class IncludeDocBlockParameters implements Serializable { | ||
public function __construct( | ||
public readonly bool $summary = false, | ||
public readonly bool $description = true, | ||
) { | ||
// empty | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions; | ||
|
||
use Exception; | ||
use Illuminate\Container\Container; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotValidPhpFile; | ||
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(IncludeDocBlock::class)] | ||
class IncludeDocBlockTest extends TestCase { | ||
// <editor-fold desc="Tests"> | ||
// ========================================================================= | ||
/** | ||
* @dataProvider dataProviderProcess | ||
*/ | ||
public function testProcess(Exception|string $expected, string $file, IncludeDocBlockParameters $params): void { | ||
$file = self::getTestData()->file($file); | ||
$instance = Container::getInstance()->make(IncludeDocBlock::class); | ||
|
||
if ($expected instanceof Exception) { | ||
self::expectExceptionObject($expected); | ||
} else { | ||
$expected = self::getTestData()->content($expected); | ||
} | ||
|
||
self::assertEquals($expected, $instance->process($file->getPathname(), $file->getFilename(), $params)); | ||
} | ||
|
||
public function testProcessAbsolute(): void { | ||
$path = 'invalid/directory'; | ||
$file = self::getTestData()->path('Valid.txt'); | ||
$params = new IncludeDocBlockParameters(); | ||
$instance = Container::getInstance()->make(IncludeDocBlock::class); | ||
$expected = self::getTestData()->content('ValidExpected.txt'); | ||
|
||
self::assertEquals($expected, $instance->process($path, $file, $params)); | ||
} | ||
//</editor-fold> | ||
|
||
// <editor-fold desc="DataProviders"> | ||
// ========================================================================= | ||
/** | ||
* @return array<string, array{Exception|string, string, IncludeDocBlockParameters}> | ||
*/ | ||
public static function dataProviderProcess(): array { | ||
return [ | ||
'default' => [ | ||
'ValidExpected.txt', | ||
'Valid.txt', | ||
new IncludeDocBlockParameters(), | ||
], | ||
'with summary' => [ | ||
'ValidWithSummaryExpected.txt', | ||
'Valid.txt', | ||
new IncludeDocBlockParameters(summary: true), | ||
], | ||
'only summary' => [ | ||
'ValidOnlySummaryExpected.txt', | ||
'Valid.txt', | ||
new IncludeDocBlockParameters(summary: true, description: false), | ||
], | ||
'no docblock' => [ | ||
'NoDocblockExpected.txt', | ||
'NoDocblock.txt', | ||
new IncludeDocBlockParameters(), | ||
], | ||
'invalid' => [ | ||
new TargetIsNotValidPhpFile(__DIR__.'/IncludeDocBlockTest/Invalid.txt', 'Invalid.txt'), | ||
'Invalid.txt', | ||
new IncludeDocBlockParameters(), | ||
], | ||
]; | ||
} | ||
// </editor-fold> | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockTest/Invalid.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Description description description description description description | ||
* description description description description description description | ||
* description description description description description description | ||
*/ | ||
A { | ||
// empty | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockTest/NoDocBlock.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
trait A { | ||
// empty | ||
} | ||
|
||
/** | ||
* Summary B. | ||
* | ||
* Description description description description description description | ||
* description description description description description description | ||
* description description description description description description | ||
*/ | ||
class B { | ||
// empty | ||
} |
Empty file.
25 changes: 25 additions & 0 deletions
25
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockTest/Valid.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Summary A. | ||
* | ||
* Description description description description description description | ||
* description description description description description description | ||
* description description description description description description | ||
* | ||
* @see https://example.com/ | ||
*/ | ||
interface A { | ||
// empty | ||
} | ||
|
||
/** | ||
* Summary B. | ||
* | ||
* Description description description description description description | ||
* description description description description description description | ||
* description description description description description description | ||
*/ | ||
class B { | ||
// empty | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/documentator/src/Preprocessor/Instructions/IncludeDocBlockTest/ValidExpected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Description description description description description description | ||
description description description description description description | ||
description description description description description description |
1 change: 1 addition & 0 deletions
1
...cumentator/src/Preprocessor/Instructions/IncludeDocBlockTest/ValidOnlySummaryExpected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Summary A. |
5 changes: 5 additions & 0 deletions
5
...cumentator/src/Preprocessor/Instructions/IncludeDocBlockTest/ValidWithSummaryExpected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Summary A. | ||
|
||
Description description description description description description | ||
description description description description description description | ||
description description description description description description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters