Skip to content

Commit

Permalink
[GenerateMarkdownDoc] Don't crash if method uses union type parameter (
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda authored Aug 1, 2022
1 parent ccf8096 commit f50d359
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Task/Development/GenerateMarkdownDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,13 @@ protected function documentParam(\ReflectionParameter $param)
{
$text = "";
$paramType = $param->getType();
if (($paramType != null) && ($paramType->getName() == 'array')) {
$text .= 'array ';
}
if (($paramType != null) && ($paramType->getName() == 'callable')) {
$text .= 'callable ';
if ($paramType instanceof \ReflectionNamedType) {
if ($paramType->getName() === 'array') {
$text .= 'array ';
}
if (($paramType->getName() === 'callable')) {
$text .= 'callable ';
}
}
$text .= '$' . $param->name;
if ($param->isDefaultValueAvailable()) {
Expand Down
14 changes: 14 additions & 0 deletions tests/_data/ClassWithUnionParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Robo\Result;

/**
* A test file. Used for testing documentation generation.
*/
class ClassWithUnionParam
{
final public static function executeTask(Robo\Task\Composer\Install|Robo\Task\Composer\Update $task): string|array
{
return [];
}
}
23 changes: 23 additions & 0 deletions tests/integration/GenerateMarkdownDocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,27 @@ function (\ReflectionMethod $m, $text) {
$this->assertStringContainsString('Set the destination file', $contents);
}

public function testMarkdownOfUnionType()
{
if (version_compare(PHP_VERSION, '8.0') < 0) {
$this->markTestSkipped('Requires PHP 8.0');
}

$sourceFile = $this->fixtures->dataFile('ClassWithUnionParam.php');
$this->assertFileExists($sourceFile);
include $sourceFile;
$this->assertTrue(class_exists('ClassWithUnionParam'));

$collection = $this->collectionBuilderForTest();
$taskGenerator = $collection->taskGenDoc("ClassWithUnionParam.md");
$taskGenerator->docClass('ClassWithUnionParam');
$result = $collection->run();
$this->assertTrue($result->wasSuccessful(), $result->getMessage());

$this->assertFileExists('ClassWithUnionParam.md');

$contents = file_get_contents('ClassWithUnionParam.md');
$this->assertStringContainsString('A test file. Used for testing documentation generation.', $contents);
$this->assertStringContainsString('#### *final public static* executeTask($task)', $contents);
}
}

0 comments on commit f50d359

Please sign in to comment.