barryvdh/laravel-ide-helper

[2.15.0] Inserting AllowDynamicProperties attribute breaks final classes

tminich opened this issue · 4 comments

Versions:

  • ide-helper Version: 2.15.0
  • Laravel Version: 10.44.0
  • PHP Version: 8.2.15

Description:

Apparently 2.15.0 introduced adding the AllowDynamicProperties attribute to generated model helper classes. Unfortunately, if the model class is declared final, this attribute is inserted between the final and class keywords leading to invalid php.

	final #[\AllowDynamicProperties]
	class IdeHelperParticipant {}

Steps To Reproduce:

  • Declare a model class final
  • Generate model helper (with -M -R, not sure if and how far that matters)
mfn commented

I guess we just need to shuffle $keyword and $allowDynamicAttributes in

$output = "namespace {$namespace}{\n{$docComment}\n\t{$keyword}{$allowDynamicAttributes}class {$classname} ";
?

Do you want to make a PR, including a test using final (ideally also abstract, which is also covered in \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getClassKeyword

Does this fix it for you? #1517

I guess we just need to shuffle $keyword and $allowDynamicAttributes in

$output = "namespace {$namespace}{\n{$docComment}\n\t{$keyword}{$allowDynamicAttributes}class {$classname} ";

Yeah that fixes it, according to the tests

Looks like it's fixed in 1.5.1, thanks a lot!