Skip to content

Commit 5ccfb53

Browse files
Copilotlisachenko
andcommitted
Fix getModifiers() method for PHP 8.4 compatibility with readonly properties
Co-authored-by: lisachenko <[email protected]>
1 parent 2d74e99 commit 5ccfb53

File tree

3 files changed

+8
-37
lines changed

3 files changed

+8
-37
lines changed

nikic-php-parser.zip

265 KB
Binary file not shown.

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
</source>
1313
<php>
1414
<ini name="memory_limit" value="1536M" />
15+
<ini name="error_reporting" value="E_ALL" />
16+
<ini name="display_errors" value="1" />
17+
<ini name="display_startup_errors" value="1" />
1518
</php>
1619
</phpunit>

src/ReflectionProperty.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,11 @@ public function getModifiers(): int
201201
}
202202

203203
// Handle PHP 8.4+ asymmetric visibility modifiers
204-
// These constants were introduced in PHP 8.4 for asymmetric property visibility
205-
if (defined('ReflectionProperty::IS_PRIVATE_SET') && $this->hasPrivateSetVisibility()) {
206-
$modifiers += constant('ReflectionProperty::IS_PRIVATE_SET');
207-
}
208-
if (defined('ReflectionProperty::IS_PROTECTED_SET') && $this->hasProtectedSetVisibility()) {
209-
$modifiers += constant('ReflectionProperty::IS_PROTECTED_SET');
204+
// In PHP 8.4, readonly properties automatically get IS_PRIVATE_SET flag
205+
// because readonly properties can't be set after initialization
206+
if ($this->isReadOnly() && PHP_VERSION_ID >= 80400) {
207+
// PHP 8.4 introduced IS_PRIVATE_SET = 2048 for asymmetric visibility
208+
$modifiers += 2048; // ReflectionProperty::IS_PRIVATE_SET
210209
}
211210

212211
return $modifiers;
@@ -356,37 +355,6 @@ public function isReadOnly(): bool
356355
return $this->propertyOrPromotedParam->isReadonly() || $this->getDeclaringClass()->isReadOnly();
357356
}
358357

359-
/**
360-
* Checks if property has private setter visibility (asymmetric visibility)
361-
*
362-
* This is a PHP 8.4+ feature where properties can have different visibility
363-
* for get/set operations, e.g.: public private(set) $prop
364-
*
365-
* @return bool Always returns false until nikic/php-parser supports asymmetric visibility syntax
366-
* @since PHP 8.4
367-
*/
368-
private function hasPrivateSetVisibility(): bool
369-
{
370-
// TODO: Implement when nikic/php-parser supports asymmetric visibility syntax
371-
// For now, always return false since the parser doesn't support this syntax yet
372-
return false;
373-
}
374-
375-
/**
376-
* Checks if property has protected setter visibility (asymmetric visibility)
377-
*
378-
* This is a PHP 8.4+ feature where properties can have different visibility
379-
* for get/set operations, e.g.: public protected(set) $prop
380-
*
381-
* @return bool Always returns false until nikic/php-parser supports asymmetric visibility syntax
382-
* @since PHP 8.4
383-
*/
384-
private function hasProtectedSetVisibility(): bool
385-
{
386-
// TODO: Implement when nikic/php-parser supports asymmetric visibility syntax
387-
// For now, always return false since the parser doesn't support this syntax yet
388-
return false;
389-
}
390358

391359
/**
392360
* {@inheritDoc}

0 commit comments

Comments
 (0)