Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GenerateMarkdownDoc] Don't crash if method uses union type parameter #1118

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}