Code style: Inject annotations for class properties

Since I can use PHP 8 in my Flow-based projects, I convert doc-comment-based annotations to native PHP annotations where I can. The code feels much tidier afterwards, and I like how PhpStorm highlights the annotations by coloring them yellow.

Now, where I’m still undecided about is, if I should generally optimize the class properties by putting the annotation into the same line like the property declaration.

What’s your preference? Maybe we can even decide on a general rule for future Flow coding guidelines?

Optimized formatting:

Traditional formatting:

I think a general rule would be great, I would go with the first version, might be a bit long for really long class/property names but should read nicely, the problem starts then with eg InjectConfiguration, with path and package that might become quite long in itself and then would need an exception to the rule I guess? So from that perspective variant 2 might just be the more consistent style.

I also tend to prefer the first – concise – version and I even can get used to the long lines with InjectConfiguration, I don’t mind that.

I’m using constructor injection whereever possible. It removes the Flow specific annotation for injection.

If that’s not possible, I add all annotations to the line above the property and annotations for my actions in a controller, has the same

2 Likes

I prefer the first version as it gives a very compact overview of the class-properties. If we define a rule we should also consider cases where multiple and long annotations are needed.

As it might not be the property’s only attribute I prefer the second option

I have to admit that I also prefer the second one as it keeps the important type information aligned.

Not part of your question, but I try to use constructor injection and settings injection via Objects.yaml everywhere except for glue code:

final class AgentCommandController extends CommandController {
    public function __construct(
        private readonly ClusterFinder $clusterFinder,
        private readonly ClusterCommandHandler $clusterCommandHandler,
        private readonly ClusterService $clusterService,
        private readonly DomainCommandHandler $domainCommandHandler,
    ) {
        parent::__construct();
    }
}
1 Like

yes i can aggree with @sorenmalling and @bwaidelich - constructor injection is also my favorite for the domain especially since promoted properties make it even cleaner ^^

… and i would stick to the second one - feels calming to have some white space floating around (says my half as a FE ^^)

While I optimized some code recently to have class properties defined like in the first example, those had no injections. As soon as those are thrown in the mix, I’d rather use the second format.

And especially considering other, multiple and/or longer annotations, the need for an exception from the rule is removed. Consistency FTW.