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

Action generator: Don't generate nullable types for PHP 7.0 #6154

Merged
merged 1 commit into from Apr 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Codeception/Lib/Generator/Actions.php
Expand Up @@ -233,25 +233,25 @@ private function createReturnTypeHint(\ReflectionMethod $refMethod)
}

/**
* @param \ReflectionType $returnType
* @param \ReflectionType $type
* @return string
*/
private function stringifyType(\ReflectionType $returnType)
private function stringifyType(\ReflectionType $type)
{
if ($returnType instanceof \ReflectionUnionType) {
$types = $returnType->getTypes();
if ($type instanceof \ReflectionUnionType) {
$types = $type->getTypes();
return implode('|', $types);
}

if (PHP_VERSION_ID < 70100) {
$returnTypeString = (string)$returnType;
$returnTypeString = (string)$type;
} else {
$returnTypeString = $returnType->getName();
$returnTypeString = $type->getName();
}
return sprintf(
'%s%s%s',
$returnType->allowsNull() ? '?' : '',
$returnType->isBuiltin() ? '' : '\\',
(PHP_VERSION_ID >= 70100 && $type->allowsNull()) ? '?' : '',
$type->isBuiltin() ? '' : '\\',
$returnTypeString
);
}
Expand Down
39 changes: 38 additions & 1 deletion tests/cli/BuildCest.php
Expand Up @@ -94,4 +94,41 @@ public function noReturnForVoidType(CliGuy $I, Scenario $scenario)
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound($dir): void');
}
}

public function generateNullableParametersOnPHP70(CliGuy $I, Scenario $scenario)
{
if (PHP_VERSION_ID < 70000 || PHP_VERSION_ID >= 70100) {
$scenario->skip('For PHP 7.0 only');
}

$cliHelperContents = file_get_contents(codecept_root_dir('tests/support/CliHelper.php'));
$cliHelperContents = str_replace('public function seeDirFound($dir)', 'public function seeDirFound(\Directory $dir = null)', $cliHelperContents);
file_put_contents(codecept_root_dir('tests/support/CliHelper.php'), $cliHelperContents);

$I->runShellCommand('php codecept build');
$I->seeInShellOutput('generated successfully');
$I->seeInSupportDir('CliGuy.php');
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
$I->seeInThisFile('use _generated\CliGuyActions');
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound(\Directory $dir = NULL)');
}

public function generateNullableParametersOnPHP71AndLater(CliGuy $I, Scenario $scenario)
{
if (PHP_VERSION_ID < 70100) {
$scenario->skip('Does not work in PHP < 7.1');
}
$cliHelperContents = file_get_contents(codecept_root_dir('tests/support/CliHelper.php'));
$cliHelperContents = str_replace('public function seeDirFound($dir)', 'public function seeDirFound(\Directory $dir = null): ?bool', $cliHelperContents);
file_put_contents(codecept_root_dir('tests/support/CliHelper.php'), $cliHelperContents);

$I->runShellCommand('php codecept build');
$I->seeInShellOutput('generated successfully');
$I->seeInSupportDir('CliGuy.php');
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
$I->seeInThisFile('use _generated\CliGuyActions');
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound(?\Directory $dir = NULL): ?bool');
}
}