It would be nice if Drupal was more compliant with PSR2. I've created this issue to talk about a specific and quite common violation that is discovered whenever I try to validate code using a PSR2 code validator / sniffer

PSR2's rules on opening braces:

Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.

---

Drupal's coding standards agree that opening braces for control structures must go on the same line. They just disagree about the opening brace of classes and methods. With this one change we'll eliminate a lot of validation checks. In my case, I work with a lot of PHP developers who mainly write PSR2 validated code and want to use Drupal for specific kind of projects while keeping code quality consistent. This would be a major improvement.

source: http://www.php-fig.org/psr/psr-2/#1-overview

Comments

cosmicdreams created an issue. See original summary.

cosmicdreams’s picture

Issue summary: View changes
drunken monkey’s picture

Title: PSR2 : Opening braces » PSR-2: Opening braces

I can't find any issue were this was discussed, to be honest, but I'm pretty sure it was decided not to comply with PSR-2 in Drupal – as evident by discrepancies such as this one.

On the practical side, this would be a re-write of more or less our entire code base. That seems a lot of effort for something that's of at best dubious usefulness.

And from a personal side, I find PSR-2 incredibly ugly.

So, -1 from me.

mikeryan’s picture

-1 as well - the inconsistent treatment of opening braces for classes and methods is the very worst part of PSR-2.

Anyone know why that's in there? The only hint I've seen was along the lines of "historical *mumble* C *mumble*"...

dawehner’s picture

dawehner’s picture

My opinion about this is still: I don't like the PSR-2 style, but one can get used to any particular style. Once you though adapt to the style of some source code, it can be much easier to read, and well, there is lot of code out there in PSR-2 style.

This feels a bit like a midterm vs. longterm tradeoff for me.

cosmicdreams’s picture

I feel that we're all sharing personal opinions of what we like and don't like about a coding standard that has now become a standard onto which PHP code is validated. When code doesn't comply with these rules warnings are generated and even if we can mentally ignore them they become a subject of conversation every time I roll a new PHP developer onto a Drupal project.

"No, Drupal has it's own coding standards. You should ignore those warnings"

"No, there isn't a technical reason why we don't implement opening brackets that way, brackets are brackets. We just prefer to keep them on the same line"

"Yes, Drupal complies with many of the PSR standards just not this one."

"No, we've talked about it before and I don't think there's a buy-in for making the change"

"Sure, here's an issue where you can go to complain"

...and they never do because that's too much work / investment. But I've had variants of that conversation many times with PHP devs.

dawehner’s picture

"Yes, Drupal complies with many of the PSR standards just not this one."

Well, I think this is a bad point. PSR-s are independent from each other.

cosmicdreams’s picture

That's a fair point @dawehner.

I understand that I'm entering this discussion at a point where it already has a great amount of history. And that between the 4 of us that have commented at least half don't like the aesthetics of the standard. mikeryan even questioned the original intent of this specific standard and how the standards came to be. That would be a thing I could research and report back to you.

But for now let's revisit the reason why we have standards.

"Remember that the main advantage of standards is that every piece of code looks and feels familiar, it's not about this or that being more readable."

- excerpt: http://symfony.com/doc/current/contributing/code/standards.html

Laravel is an interesting example to bring up because it was a project that maintained it's own coding standards for a long while. Eventually they shifted to PSR-2 coding style because they wanted to improve the developer experience by not forcing PHP developers a special way just for them. Now devs that work on PSR-2 PHP projects and Laravel projects can have a common expectation and understanding while reading code from either. Don't we want that?

Do we want developers to have to adapt their way of thinking and reading code so that they can start understanding Drupal code?
Are we comfortable with the uncanny valley we present when we show objects and methods with a different kind of opening brackets?
Do we want our code's quality to be judged by the number of warnings that are returned from a standard code sniffer?

@mikeryan brought up that

the inconsistent treatment of opening braces for classes and methods is the very worst part of PSR-2.

I don't understand what you mean. Being able to visually differentiate class and method declarations from control statements sounds like a pretty good idea to me. Furthermore you can modify the listing of method arguments and be sure you won't erase the opening bracket. So this makes sense (to me).

I understand that in this issue queue we're proposing changes to our coding standards, gathering consensus, and eventually moving forward with approved changes. I think what I am trying to understand the most from this particular thread is what our intent is. Should Drupal's coding standards be a thing that someone just knows because they've worked a lot on PHP projects? Or do we want them to learn Drupal with the knowledge that they'd have to unlearn them if they work on non-Drupal things?

cosmicdreams’s picture

At the same time, a lot of tools are gathering around the PSR-2 standard. Tools like PHP Code Sniffer, PHP CS Fixer, and Scrutinizer all support PSR-2 out of the box. You have to (and we have) put extra work to adapt these to abide by our rules. But what if they just worked?

What if we made it easier for junior developers to discover the tools and techniques it takes for them to gain confidence with our codebase?

What if discussions around our code was solely focused on it's merits and not it's code style?

When I think of all the things we could do to improve the Developer Experience for the next batch of new developers jumping onto Drupal for the first time, this is the kindest, most helpful form of mentorship we could provide. By forcing them to learn something they'd never use outside of Drupal, we're setting them up for frustration.

dawehner’s picture

@cosmicdreams
Please bring up this discussion again on the PSR-2 issue. Just posting your opinion here is just a duplication.

cosmicdreams’s picture

Now I'm confused.

I was told I needed to make a concrete proposal before the coding standards committee would take up my concerns. Are you saying that creating an issue in this queue is not the proper way to have a conversation about proposals like this?

Are you saying that I need to get people to agree to adopt all of PSR-2 before we can move forward with this smaller change?

Over the past two months I have been trying to raise attention to small steps we could be making today to adopt more of PSR-2. Every other place I've raised these concerns my comments have either been removed or ignored. That's a lot of time to be treated this way.

And now you're saying that in order to make progress I'm going to have to push back against the weight of a five month old issue that seems to have a lot of negativity in it and even some FUD.

Let's say I do that. Let's say I jump in there and make my case. Would it be a different conversation? Sounds like it'll be just me saying Standards are important / allowing PHP developers to ramp up on Drupal without the uncanny valley is important and then a majority of comments say no because they don't like it.

Well what about our audience for this change. What about all the people who wouldn't bother to jump onto a Drupal issue queue. Do they not get a say because they haven't invested years into Drupal like we have?

That's why I need some help understanding what our standards process is. I want to be a positive force for change. How can I do good things here?

dawehner’s picture

Ah gotcha, nevermind, ignore me.

cosmicdreams’s picture

@dawehner, you know me. I don't want to ignore you. I want to engage you in this conversation.

I have asked many questions here. I honestly want answers to them. Who should I talk to to get the answers? Where do I need to go. What process and procedure do I need to follow. I can make this a focus if the task requires it.

Then I can provide feedback on the process so that it can evolve like everything else. But it starts with conversation.

What is my next step here?

drunken monkey’s picture

I have asked many questions here. I honestly want answers to them. Who should I talk to to get the answers? Where do I need to go. What process and procedure do I need to follow. I can make this a focus if the task requires it.

You did things right so far – creating an issue here is the exact way to propose a change to the coding standards.
Now you've received feedback – two negative votes against your "pro". You can first wait whether more people will comment on their own. Then, if it's that important to you, you could ask community members you know and who might have an opinion to comment, too.
If few people are interested, though, or most are opposed, there's not really anything you can do. Coding standards are (unless I'm mistaken) determined democratically, so there's little chance of effecting unpopular changes.
Also, my guess is that few people will bother reading such long comments. It's still just a matter of taste.
And if "PSR-2 says so" would be any help as an argument, #2645010: Switch to PSR-2 as of Drupal 9 wouldn't have been rejected, so you'd need something better than that anyways.

When code doesn't comply with these rules warnings are generated

I don't really believe that there is any IDE or other program for which you cannot turn off PSR-2 violation warnings. Drupal doesn't use it, so it's no wonder you're irritated if you configure your IDE (or whatever) to validate against that. Just configure it to use Drupal's coding standards instead and you won't get warnings. (OK, that's a lie – you'll still get tons. But far fewer tons.)

maverick619’s picture

Hi guys. I came across this issue because I was trying out Atom and looked at the Beautify extension which uses PHP CS Fixer to change my code style to fit PSR-2 standards. While I've been using Drupal for a long time and came to adopt that as my standard, my current job requires me to work with both WP and Drupal. Code style is non-existent with most WP developers and I find myself having a hard time reading their code. This makes the Beautify extension essential for any code that somebody else has written.

While I realise some may find what I'm saying selfish and merely a way to have my layout fixed for me (ie lazy), I can assure you that's not the case. I have always written out my code and have just discovered this tool. At the same time why not use tools to make sure everything is correct before submitting my code. At the end of the day getting it correct as quickly as possible is what we all strive for.

If I'm to use Beautify to bring standards to WP templates and projects it makes sense that I would use the same standards for Drupal. It's all PHP and should be accepting the same standards for that language. While it may be a pain for a while to adjust to writing and reading code in this way it's a fair trade-off to get consistency for the benefit of all developers. We shouldn't be smug hipsters about our coding style.

slootjes’s picture

+1 to bring Drupal closer to other major PHP projects.

keesje’s picture

In general I think code standards need to relax more and evolve towards whats facto-standard in PHP industry. Drupal is big, but not nearly big enough in PHP market to dictate its own standards. Yes its pain to change what we have. But benefits will be rewarding, like other PHP devs to contribute to Drupal. Now new modules on DO dont get security approval bcs they implement PSR2 instead of Drupal strict coding standards. For (experienced) devs new to drupal drupal, thats a "WTF" moment and a neg experience with Drupal. We can avoid this IMHO.

pfrenssen’s picture

-1 We are not using PSR-2, and moving towards it in incremental fashion is a pretty bad idea. This change in particular would be highly disruptive for the entire Drupal ecosystem, requiring every single file in core and contrib (and custom project code!) to be rewritten, and invalidating every single patch on Drupal.org. Since it is a cosmetic change with no practical value it's hard to justify the thousands of man hours it would require to implement it.

cosmicdreams’s picture

Certainly not something we would want to do in a minor release. Drupal 9 would be first opportunity to make this change. And even so we should expect the disruption that @pfrenssen has outlined.

We should recognize that this is more than a "cosmetic" issue. This is about the uncanny valley we present programmers who come to Drupal who have gained significant experience with PHP outside of our Drupal experience. To them, we present fundamentally flawed code as it violates the generally accepted code formatting standards that the rest of the PHP community has agreed upon.

What we're saying in this thread, is that we're ok with that. True, the code validating situation has improved greatly as a result of having tools like phpcs properly use rulesets that enforce Drupal's coding standards. But it is also true, that we want PHP developers adjust to our coding standards rather than adjusting our coding standards for developers. Explicitly, we're fine with this level of unfriendliness towards PHP developers.

My main concern in all of this has been that it was previously hard and at times impossible to get my favorite tools to use the Drupal rulesets. This issue was magnified for me when I had to support others who had a hard requirement that all code pass phpcs before committing. Now that problem has been alleviated as we have figured out how to get that working on our end. Yet the stigma remains. Drupal is both like modern PHP and it isn't. It's in an uncanny valley for the professional PHP developers I work with. Over time the devs I work with become more comfortable navigating through the unspoken boundaries between Drupal PHP and PHP, but initially there is the "explaining" period where I have to point out the Drupalisms.

pfrenssen’s picture

Certainly not something we would want to do in a minor release.

The coding standards are not version-specific at the moment, they cover all supported versions. So the scope of this proposed change applies to Drupal 7 and Drupal 8. We can discuss version-specific standards in another issue if you think it is worthwhile.

Your other points are well considered but are not in scope for this particular issue. This is about changing the position of the opening braces. Everything else will still not be PSR-2, so just making this change will not improve your interpersonal relations and fix problems with your tooling. But I think it is commendable that you care so much about your co-workers' mental well-being that you want to change our coding standards to make them feel more comfortable :)

Note also that Drupal is a project that has been around for more than a decade before there was even talk of PSR-2. Similar to many other projects in its class it has its own coding standard. It's simply not correct to imply that the whole PHP world has settled on PSR-2. Other heavyweight PHP projects that predate PSR-2 also have their own standard, such as Wordpress, Joomla and even PHP_CodeSniffer itself.

cosmicdreams’s picture

Please note the previous conversation this thread has received. This issue was created because I was told I needed to make a concrete proposal in order to be considered. I was also advised not to create a "We should adhere to PSR-2" issue because it apparently was already discussed and the specific developers who comprise the coding standards group didn't want it, therefore proposing it again would be a waste of time. Therefore, I created this specific issue for consideration.

I chose this issue based on the feedback I received from the professional PHP developers I worked with. Of all the specific code standards violations this was the one that upset them the most.

Now, two years after I created this issue, after gaining a minuscule of momentum based on a few developers finding this issue for the first time, the feedback has changed, to paraphrase, "This doesn't go far enough".

I would appreciate some concrete direction on what my next steps should be, continue to push for specific concrete and atomic change? Or to start a new push for "Drupal should adopt PSR-2".

To your point of enforcing a new coding standard on Drupal 7

As discussed recently, introducing this code formatting standard change would be disruptive. Therefore we wouldn't want to introduce it until the next major release. Given our policy of only supporting Drupal(Current) and Drupal(Current - 1), this would mean that introducing the code formatting standard at that time would not mean that we would have to modify Drupal 7 code as Drupal 7 would fall out of support. However, it would mean a disruption for Drupal 8 around the time of Drupal 9's release. Something we would want to avoid.

This begs the question, is Drupal code formatting standards to stay the same forever? Or is it only 100% ensured to not be disruptive changes that are allowed?

The need to find a mutual purpose

If so, what are we doing here? What's the point of any of these discussions? Changing the code formatting standards, if allowed at all, for a project that is over 15 years old, is inherently disruptive and perhaps reckless given the fact that we're not talking about adding functionality or "fixing bugs" other than the code formatting warnings and advised corrections that command line tools and intelligent code editing tools flag as problems that the developer should fix.

I would hope that the purpose of this governing group is to improve the developer experience for new Drupal developers and experienced Drupal developers. To ensure that the code provided by the project meets the expectations of "good code" for those developers. What better way to do that than adopt a code formatting standard that is also used by the PHP professional community?

Imagine: one coding standard, one set of expectations, for PHP developers who work on standard PHP and/or Drupal. We can reduce the weirdness to implementation details and not distract newcomers with silly stuff like code formatting standards.

If we don't share a mutual purpose, then we need to make clear what our purpose is here. We need to make clear that the purpose of Drupal's code standards is to provide code that Drupal developers expect and explicitly doesn't make any effort to adhere to PHP coding standards outside of Drupal's island of code. And that we understand that if you learned PHP outside of Drupal that you'll need to put an extra effort into adopting Drupal's way of doing things to cross that uncanny valley.

My hope this isn't a knee jerk reaction

@pfrenssen, once you read the comments in this thread in their entirety, I hope you can see the same pattern I have. Talking about coding standards from a minority perspective in this thread is starting to seem impossible. At any time in this thread, all it takes is one individual to say, to paraphrase, "I don't like it", or "Too disruptive", or "That would be effectively pointless" without recognizing that any code formatting change is inherently subjective, disruptive, and effectively pointless. At the same time, I feel I'm representing a voiceless minority that don't bother to create Drupal accounts but would be more interested in working with Drupal's code if it didn't spam their automated processes and tools with errors if they get included into the project.

We need a better mode of communication here. We need to find mutual purpose. I'm sure we have mutual respect. I know everyone involved here is speaking from a perspective of trying to decide what's best for the project. I just don't think we agree on what that means.

slootjes’s picture

@pfrenssen Wordpress and Joomla are very good examples of other projects where more advanced PHP developers don't want to dive in to, coding style is probably not the most important reason but certainly one of them.

@cosmicdreams very well said, thank you.

I know everyone involved here is speaking from a perspective of trying to decide what's best for the project. I just don't think we agree on what that means.

That is for sure! I guess I'm a minority user here as I do not really develop sites using Drupal but I'm involved developing modules though. I really have the feeling I can influence this project in a positive way to make it more attractive. Being on the Symfony side of PHP my biggest "negs" regarding Drupal are: not following coding style PSRs, useless comments and the use of FQCN everywhere bloating my code (I created issues for those), not using PR's for patches, a weird "security badge" which has nothing to do with security and maybe worst of all: the endless discussions in issues that last for years. It seems no one is taking ownership or is setting the direction Drupal is going to which does happen for most other PHP projects (Symfony, Laravel, Yii) which have a clear leadership (as in a project lead with help of a "decider" team for instance). I have the feeling that I can comment in these issues many times but without any result and I've the feeling this doesn't benefit Drupal at all. I feel welcome in the community when it comes to people but not in it's code. To open Drupal up to the rest of the PHP community is a far bigger topic than just coding style and I hope to be a part in helping with it.

pfrenssen’s picture

Can we keep this on topic please. Please give a +1 or a -1 and discuss your specific reasoning for having opening braces on the next line, or why we shouldn't change it. Everything else is out of scope.

slootjes’s picture

An issue can be used for discussion and being able to give pros or cons about what decision someone can make to give a +1 or -1 so I do think this is all relevant mostly. I do agree that some parts aren't but explain the bigger picture to why change can benefit.

cosmicdreams’s picture

Sure, we can continue to accrue these +1's and -1's.

It seems that what you're saying is that all we need to break the holding pattern on this issue is to have developers who may be impacted by this change (including all developers who may work with Drupal within their projects but don't have Drupal accounts) login and add a single +1 or -1 comment to this thread.

You've asked that the discussion remain focus on simply adding those votes and to the pros and cons of this change.

Pros:
- Follows the PSR-2 code formatting standard
- Makes code more consistent with expectations of PHP developers.
- Less friction with automated code validating tools

Cons:
- Wide impact of change
- Visual improvement is subjective
- Code improvement is negligible
- No Functionality improvement
- Disruptive to Drupal 8 code.

borisson_’s picture

-1, we should either accept the entire psr-2 standard or nothing at all. Doing it in parts would be a massive disruption in multiple batches.

ClarkMitchell’s picture

I am a new Drupal Developer coming from a WordPress and Symfony background. Reading through the code standards documentation my first instinct was to seek out any issues related to adopting PSR-2 and see what kind of traction they might be getting. I'm quite disappointed in the response.

By not adopting a growing standard Drupal will further isosate itself from the larger PHP community, as WordPress has. I think a major version change is an appropriate time for such a transition.

+1 for adopting PSR-2 as a whole because I believe it would facilitate rapid onboarding of new developers and would allow better integration with automated tools.

+1 for adopting next line braces for class and method names even without the rest of PSR-2 as it helps to create a visual heirarchy of methods over control structures.

dev11235’s picture

I'm from Laravel, Symfony, Yii background

Same thoughts as @ClarkMitchell comment #28:

+1 for adopting PSR-2 as a whole because I believe it would facilitate rapid onboarding of new developers and would allow better integration with automated tools.

+1 for adopting next line braces for class and method names even without the rest of PSR-2 as it helps to create a visual heirarchy of methods over control structures.

pwolanin’s picture

Status: Active » Closed (won't fix)

Seems like adopting PSR-2 was already discussed and rejected in #2645010: Switch to PSR-2 as of Drupal 9. On that basis marking this "closed"

There are already automated tools to check against Drupal-specific coding standards, so it's not clear to me why this one change would be helpful to someone using a PSR-2 checker since a lot of other items would not pass.