Skip to content

Commit 5e4f177

Browse files
authored
Merge pull request #450 from PHPCSStandards/backcompat/getmethodparameters-add-extra-test
[BackCompat|FunctionDeclarations]::get[Method]Parameters(): add extra test
2 parents e74812a + b92cc16 commit 5e4f177

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Tests/BackCompat/BCFile/GetMethodParametersTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ class ConstructorPropertyPromotionWithReadOnly {
206206
public function __construct(public readonly ?int $promotedProp, ReadOnly private string|bool &$promotedToo) {}
207207
}
208208

209+
class ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration {
210+
/* testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration */
211+
// Intentional fatal error. Readonly properties MUST be typed.
212+
public function __construct(public readonly $promotedProp, ReadOnly private &$promotedToo) {}
213+
}
214+
209215
/* testPHP8ConstructorPropertyPromotionGlobalFunction */
210216
// Intentional fatal error. Property promotion not allowed in non-constructor, but that's not the concern of this method.
211217
function globalFunction(private $x) {}

Tests/BackCompat/BCFile/GetMethodParametersTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,57 @@ public function testPHP81ConstructorPropertyPromotionWithReadOnly()
19611961
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
19621962
}
19631963

1964+
/**
1965+
* Verify recognition of PHP8 constructor with property promotion using PHP 8.1 readonly keyword
1966+
* without a property type.
1967+
*
1968+
* @return void
1969+
*/
1970+
public function testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration()
1971+
{
1972+
$expected = [];
1973+
$expected[0] = [
1974+
'token' => 8, // Offset from the T_FUNCTION token.
1975+
'name' => '$promotedProp',
1976+
'content' => 'public readonly $promotedProp',
1977+
'has_attributes' => false,
1978+
'pass_by_reference' => false,
1979+
'reference_token' => false,
1980+
'variable_length' => false,
1981+
'variadic_token' => false,
1982+
'type_hint' => '',
1983+
'type_hint_token' => false,
1984+
'type_hint_end_token' => false,
1985+
'nullable_type' => false,
1986+
'property_visibility' => 'public',
1987+
'visibility_token' => 4, // Offset from the T_FUNCTION token.
1988+
'property_readonly' => true,
1989+
'readonly_token' => 6, // Offset from the T_FUNCTION token.
1990+
'comma_token' => 9,
1991+
];
1992+
$expected[1] = [
1993+
'token' => 16, // Offset from the T_FUNCTION token.
1994+
'name' => '$promotedToo',
1995+
'content' => 'ReadOnly private &$promotedToo',
1996+
'has_attributes' => false,
1997+
'pass_by_reference' => true,
1998+
'reference_token' => 15, // Offset from the T_FUNCTION token.
1999+
'variable_length' => false,
2000+
'variadic_token' => false,
2001+
'type_hint' => '',
2002+
'type_hint_token' => false,
2003+
'type_hint_end_token' => false,
2004+
'nullable_type' => false,
2005+
'property_visibility' => 'private',
2006+
'visibility_token' => 13, // Offset from the T_FUNCTION token.
2007+
'property_readonly' => true,
2008+
'readonly_token' => 11, // Offset from the T_FUNCTION token.
2009+
'comma_token' => false,
2010+
];
2011+
2012+
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
2013+
}
2014+
19642015
/**
19652016
* Verify behaviour when a non-constructor function uses PHP 8 property promotion syntax.
19662017
*

0 commit comments

Comments
 (0)