silverstripeltd / bespoke-standards

Silverstripe PHPCS standards for Bespoke squads
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Feat: dynamic property annotations for static config #16

Open Cambis opened 1 year ago

Cambis commented 1 year ago

Dynamic property annotations for static config

PHP version: 8.1
Proposed Sniff name: Silverstripe.Sniffs.Commenting.DynamicPropertySniff

The idea for this sniff is to enforce magic property annotations for static config array fields ($db, $has_one, $extensions etc).

Reason for proposed change

Disallowed Code Example

class Foo extends DataObject
{
    private static array $db = [
        'Bar' => 'Varchar(255)',
    ];

    private static array $has_one = [
        'Baz' => Baz:class,
    ];

    private static array $extensions = [
         FooBar::class,
    ];
}

Allowed Code Example

+ /**
+  * @property string $Bar
+  * @property int $BazID
+  * @method Baz Baz()
+  * @mixin FooBar
+  */
class Foo extends DataObject
{
    private static array $db = [
        'Bar' => 'Varchar(255)',
    ];

    private static array $has_one = [
        'Baz' => Baz:class,
    ];

    private static array $extensions = [
         FooBar::class,
    ];
}
satrun77 commented 1 year ago

This can be achieved with PHPStan

Cambis commented 1 year ago

For reference here's a module that can currenlty achieve this https://github.com/silverleague/silverstripe-ideannotator/tree/master, it is a silverstripe module that runs on dev/build

We could achieve this using rector, which would have the added advantage of not running on dev/build

satrun77 commented 1 year ago

This would be risky. dev/build is not for changing codebase :)

For reference here's a module that can currenlty achieve this https://github.com/silverleague/silverstripe-ideannotator/tree/master, it is a silverstripe module that runs on dev/build

We could achieve this using rector, which would have the added advantage of not running on dev/build

Cambis commented 1 year ago

Yeah not keen on the whole dev/build thing 🙅 , just noting the package there for inspiration 💡

edwilde commented 1 year ago

Would be a fun feature to add to kendo templates, definitely possible with minimal logic. It would really help dispel some of the magic around DataObjects ✨🧙

Maintaining it would be a bit hit and miss without some kind of automation though

GuySartorelli commented 9 months ago

FYI there's a rule for validating @method Baz Baz() (and other @method annotations for relations) in the new https://github.com/silverstripe/silverstripe-standards (only compatible with framework 5.2+, due to its reliance on generics which are added in that release - coming in April this year)