• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XenForo PSR-2

Jake B.

Well-known member
#1
I've always wondered, Why doesn't XenForo follow the PSR-2 coding style? I've also noticed that different parts of code are styled differently. For example, I've seen places that align all of the fat arrows in arrays, and other places that don't, I believe I've even seen some places that use spaces and others that use tabs, and some places have the beginning curly brace on the same line as the if statement, and places that don't do this. I'll perhaps try to find some specific examples of all of these, but was just a bit curious about this :p
 

Jake B.

Well-known member
#3
Don't all the dev's use different IDE's? They've probably never synchronised their code styles fully... :)

Liam
As far as I know Mike uses phpStorm, and I'm fairly certain Chris does as well as he's the one that suggested it to me. For PSR-2 it's just a matter of selecting an option in a dropdown box and it takes care of everything. The only change I make to it is to use tabs instead of spaces, and that's only because spaces are aggravating :p
 

Mike

XenForo developer
Staff member
#4
The code predates PSR-2 anyway, though I don't really like some of the decisions/mandates in it either.
 

Chris D

XenForo developer
Staff member
#7
I'm not totally sure when PSR-2 was accepted, but bearing in mind the first beta releases of XF were 5 years ago, it must surely pre-date that.

That said, I hate PSR-2.
PHP:
<?php
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}
Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
Noooooooo! Seems really petty, but this is the worst for me.

I think Mike perhaps agrees with me on that, too, and likely some of the others. Seems like a bad decision to me.

We do actually all use PhpStorm so we could probably standardise a few things quite easily (for XF 2) but hopefully not PSR-2 ;)
 

Jake B.

Well-known member
#8
Noooooooo! Seems really petty, but this is the worst for me.
I'm somewhat curious of why this is, even before I found out about PSR-2 I did this. Even tried "conforming" to doing
PHP:
if (true)
{
    // Do this
}
when I started working on XenForo Add-ons, but it drove me mad so I switched back after a few months :p
 

Daniel Hood

Well-known member
#9
I do it the same way as Chris and I can't stand the bracket being on the same line as the if condition. At this point, it's just because I'm used to it. I also think it's beneficial when skimming code because I can look at the farthest left character and tell where things start easily. When the { is on the first line, it seems as if the } is closing something that hasn't been started from a quick glance.
 

Jake B.

Well-known member
#11
@Daniel Hood I think both you and @Chris D would hate Ruby. No curly braces at all.

PHP:
/*
  * This is a conditional
  * and it does something
  */
if ($thisVar === null) {
    $thatVar = 'this';
}
turns into...

Code:
=begin
This is a conditional
and it does something
=end
if this_var.nil?
    that_var = 'this'
end
No semicolons either, and you generally don't use camel case for variable names either :p
 

Chris D

XenForo developer
Staff member
#12
Never used Ruby.

iOS Swift has sort of gone in that direction, though - generally you don't need braces, but you can still use them if you want.
 

Daniel Hood

Well-known member
#13
I can actually deal with
=being
and
=end since the first character symbolizes the start and finish in both lines ;)

I can also work without the brackets
PHP:
if (true)
 do();
else
 dont();
the thing is, if the closing symbol exists, I want the beginning symbol to exist in the same spot.
 

Jake B.

Well-known member
#14
I can actually deal with
=being
and
=end since the first character symbolizes the start and finish in both lines ;)
This is the one that really drives me crazy. that is the only way to do a block comment, AND the =begin and =end have to be on the far left, no whitespace before them (including tabs).

I can also work without the brackets
PHP:
if (true)
do();
else
dont();
the thing is, if the closing symbol exists, I want the beginning symbol to exist in the same spot.
You definitely get used to it. I can't stand not using curly braces in PHP though. That's how you end up with things like this
 

Daniel Hood

Well-known member
#15
You definitely get used to it. I can't stand not using curly braces in PHP though. That's how you end up with things like this
I always use curly braces, I'm just saying that I can read
PHP:
if (true)
   yes();
else
   no();
easier than I can read
PHP:
if (true) {
  yes();
} else {
  no();
}
but I still really want
PHP:
if (true)
{
   yes();
}
else
{
   no();
}
This is the one that really drives me crazy. that is the only way to do a block comment, AND the =begin and =end have to be on the far left, no whitespace before them (including tabs).
the whitespace part is a deal breaker.
 

Jake B.

Well-known member
#16
I like it, even with all of its quirks. Rails is pretty awesome, and makes it really easy to do just about anything.

iOS Swift has sort of gone in that direction, though - generally you don't need braces, but you can still use them if you want.
Haven't had a chance to play with Swift, definitely looks good from what I've seen though.

the whitespace part is a deal breaker.
Yeah, this one has really been driving me mad. Also the way you define private methods in Ruby is ridiculous.

Example:

Code:
class User < ActiveRecord::Base
=begin
  This method sets the last active date to now.
=end
  def set_last_active
    self.last_active = Time.now
    self.save!
  end

=begin
  This method checks if the user is currently online
=end
  def is_online?
    if self.last_active > 15.minutes.ago
      return true
    end
    return false
  end

  private

  def some_method
    # This method does something
  end

  def another_method
    # This method does something else.
  end
end
everything after 'private' is a private method, so some_method and another_method are both private.

If you're creating a method that is a setter that you pass paramters to (not like the set_last_active method above) you have to suffix it with an equal sign, if it's a getter that will return a boolean it has to be suffixed with a question mark, and any other getter doesn't get a suffix

Also with Ruby you're supposed to indent using two spaces ;)

So many rules, and so many things that will break if you don't follow them exactly. :|