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

PHP 8.1 | BCFile/FunctionDeclarations::get[Method]Parameters(): add test with new in initializers #364

Merged
merged 1 commit into from
Oct 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ class ParametersWithAttributes(
) {}
}

/* testPHP81NewInInitializers */
function newInInitializers(
TypeA $new = new TypeA(self::CONST_VALUE),
\Package\TypeB $newToo = new \Package\TypeB(10, 'string'),
) {}

/* testFunctionCallFnPHPCS353-354 */
$value = $obj->fn(true);

Expand Down
50 changes: 50 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,56 @@ public function testParameterAttributesInFunctionDeclaration()
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify behaviour when the default value uses the "new" keyword, as is allowed per PHP 8.1.
*
* @return void
*/
public function testPHP81NewInInitializers()
{
$php8Names = parent::usesPhp8NameTokens();

$expected = [];
$expected[0] = [
'token' => 8, // Offset from the T_FUNCTION token.
'name' => '$new',
'content' => 'TypeA $new = new TypeA(self::CONST_VALUE)',
'default' => 'new TypeA(self::CONST_VALUE)',
'default_token' => 12, // Offset from the T_FUNCTION token.
'default_equal_token' => 10, // Offset from the T_FUNCTION token.
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => 'TypeA',
'type_hint_token' => 6, // Offset from the T_FUNCTION token.
'type_hint_end_token' => 6, // Offset from the T_FUNCTION token.
'nullable_type' => false,
'comma_token' => 20,
];
$expected[1] = [
'token' => ($php8Names === true) ? 25 : 28, // Offset from the T_FUNCTION token.
'name' => '$newToo',
'content' => '\Package\TypeB $newToo = new \Package\TypeB(10, \'string\')',
'default' => "new \Package\TypeB(10, 'string')",
'default_token' => ($php8Names === true) ? 29 : 32, // Offset from the T_FUNCTION token.
'default_equal_token' => ($php8Names === true) ? 27 : 30, // Offset from the T_FUNCTION token.
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => '\Package\TypeB',
'type_hint_token' => 23, // Offset from the T_FUNCTION token.
'type_hint_end_token' => ($php8Names === true) ? 23 : 26, // Offset from the T_FUNCTION token.
'nullable_type' => false,
'comma_token' => ($php8Names === true) ? 38 : 44,
];

$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify handling of a closure.
*
Expand Down