Implement "Constructor Promotion"#5291
Conversation
2e86157 to
4200794
Compare
There was a problem hiding this comment.
Could add a test of global functions/closures - I'd guess that the following snippet probably has a misleading error message (not sure what scope->properties_info is)
<?php
function __construct(public $x) {}LGTM otherwise
There was a problem hiding this comment.
Added a test in 717134e, error message seems fine. Or did you expect something different there?
There was a problem hiding this comment.
Ah sorry, I missed that you had function __construct() in particular in mind here. Changed the name in 7139cdc.
Still works fine though, because is_ctor above includes a check that scope is not null.
There was a problem hiding this comment.
Oh, didn't see the scope part of the check
|
Somehow I'm more interested in the |
|
@nikic Is there a reason to not go a step further, and use a "Kotlin style" syntax? class Point (
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {
//
} |
7139cdc to
f3dbd18
Compare
After this change, static arrow functions with parameters don't get the doc comments parsed properly in Reflection. It should be easy to fix by changing An example of affected code: |
150d893 to
1a9dfaf
Compare
|
@TysonAndre Nice catch! It should be fixed now. |
6827721 to
34c0b66
Compare
| public function __construct( | ||
| public float $x = 0.0, | ||
| public float $y = 1.0, | ||
| public float $z = 2.0 |
There was a problem hiding this comment.
In the RFC code example the trailing comma is allowed, but not here.
Which is correct?
There was a problem hiding this comment.
This is the subject of different RFC
There was a problem hiding this comment.
Yes, here is PHP Wiki: https://wiki.php.net/rfc/trailing_comma_in_parameter_list
|
Is there any reflection possibility for visibility ? |
|
@FlorianSteenbuck The visibility should be accessible through ReflectionProperty as usual. |
|
@nikic ok: https://3v4l.org/JsbXT <?php
class Point {
public function __construct(
public float $x,
public float $y = 0.0,
public float $z = 0.0,
) {}
}
foreach ((new ReflectionClass(Point::class))->getProperties() as $property) {
var_dump($property->name);
} |
|
@nikic what is the reason for which it is not allowed to declare promoted property in an abstract constructor? interface InstructionInterface
{
/**
* InstructionInterface constructor.
*
* @param mixed $operand
*/
public function __construct(
private mixed $operand,
);
/**
* @param SplStack $stack
*/
public function __invoke(SplStack $stack): void;
} |
|
Promoted properties combine a property declaration with initialization of that property in the constructor. There is no way to initialize the property in an abstract constructor. Your example is doubly illegal because it would also require declaring a property in an interface, which is not allowed. |
* RemoveUnusedVariableInCatchRector (https://wiki.php.net/rfc/non-capturing_catches) * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * MixedTypeRector * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * NullToStrictStringFuncCallArgRector * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
Applied rules: * LongArrayToShortArrayRector * TernaryToNullCoalescingRector * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * MixedTypeRector * ChangeSwitchToMatchRector (https://wiki.php.net/rfc/match_expression_v2) * NullToStrictStringFuncCallArgRector * TypedPropertyFromAssignsRector
RFC: https://wiki.php.net/rfc/constructor_promotion
As recently discussed on list, this implements the following short hand syntax:
This desugars to: