vscode-php-add-property icon indicating copy to clipboard operation
vscode-php-add-property copied to clipboard

[feature] add support for PHP 8: Constructor property promotion

Open ctf0 opened this issue 3 years ago • 5 comments

for example https://stitcher.io/blog/constructor-promotion-in-php-8

ctf0 avatar Apr 05 '22 11:04 ctf0

I'm already working on it 👍

Feel free to add here any particular examples (code before and after) to help me test it. Thanks!

kotfire avatar Apr 07 '22 20:04 kotfire

// before

class CustomerDTO
{
    public $name;

    public $email;

    public $birth_date;

    public function __construct(
        $name, 
        $email, 
        $birth_date
    ) {
        $this->name = $name;
        $this->email = $email;
        $this->birth_date = $birth_date;
    }
}

// after

class CustomerDTO
{
    public function __construct(
        public $name, 
        public $email, 
        public $birth_date,
    ) {}
}

ctf0 avatar Apr 07 '22 23:04 ctf0

Let me see if I understand correctly. Do you want a command to transform from classic constructor to property promotion? It is doable, what I'm working on now is support inserting/adding properties as promoted.

// before

class CustomerDTO
{
    public function __construct(
        public $name, 
        public $email
    ) {}
}

//after

class CustomerDTO
{
    public function __construct(
        public $name, 
        public $email, 
        public $birth_date
    ) {}
}

kotfire avatar Apr 08 '22 03:04 kotfire

having the 2 options would be much appreciated,

  • a cmnd to convert current to v8
  • and a new config that tell the current cmnd of adding new props to follow the v8 flow, exactly like ur example

ctf0 avatar Apr 08 '22 03:04 ctf0

👍

kotfire avatar Apr 08 '22 06:04 kotfire