Strange code reformatting

I'm testing PHPStorm and I'm finding the code reformatting pretty weird. I have no idea if this is my fault or a bug, or even intended behaviour. I'm including a reduced fragment of a file below with nested ifs and whiles (a piece of a WP template). This is how it looks after an alt-cmd-L. It's a mess. What am I doing wrong?

PHPStorm 2016.2 demo. OSX 10.11.6

 

<?php if ( have_posts() ) :
the_post(); ?>
<?php if ( is_category() ) : ?>
<h1>Archive for category: <?php single_cat_title(); ?></h1>
<?php elseif ( is_author() ): ?>
<h1>Author archive</h1>
<?php endif; ?>
<?php rewind_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php if ( is_category() ) : ?>
<h1>Sorry, but there aren't any posts in the <?php single_cat_title(); ?> category yet.</h1>
<?php else: ?>
<h1>No posts found.</h1>
<?php endif; ?>
<?php endif; ?>
4 comments
Comment actions Permalink

How do you expect this to be formatted?

0
Comment actions Permalink

I'd expect "if" to be lined up in the same column as its corresponding "elseif" and "endif". I'd also expect "while" to line up with "endwhile". So the above should look like this:

 

<?php if ( have_posts() ) :
the_post(); ?>
<?php if ( is_category() ) : ?>
<h1>Archive for category: <?php single_cat_title(); ?></h1>
<?php elseif ( is_author() ): ?>
<h1>Author archive</h1>
<?php endif; ?>
<?php rewind_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php if ( is_category() ) : ?>
<h1>Sorry, but there aren't any posts in the <?php single_cat_title(); ?> category yet.</h1>
<?php else: ?>
<h1>No posts found.</h1>
<?php endif; ?>
<?php endif; ?>
0
Comment actions Permalink

I'm having similar issues.  I have set my coding style to use PSR2:

Preferences > Editor > Code Style > PHP --> set from predefined Style (PSR1/PSR2)

But if I ever use the option + Apple + L (alt + command + L) command to reformat code, it will actually CAUSE PSR2 smells.  I'm checking this using the phpcs code-sniffer tool.  For example, I have a simple example.php file.  I can run "phpcs --standard=PSR2 example.php" and it will report no errors.  Then I use the PHPStorm Reformat Code command, and boom... it introduces LOTS of violations of PSR2.  Is there a solution to this?

0

Please sign in to leave a comment.