Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned suddenly wanting _unaligned_ arrows #2400

Closed
smileBeda opened this issue Oct 25, 2023 · 4 comments

Comments

@smileBeda
Copy link

smileBeda commented Oct 25, 2023

Bug Description

Maybe I am dreaming, but I am very sure that up and until now, a well aligned array in WP CS meant the below, in all cases:

array( 
    'thing'       => 'thang',
    'longerthing' => 'thang',
);

However, I just noticed that WPCS tries to correct them to below, when inside a WP Archive template HTML markup:

array( 
    'thing' => 'thang',
    'longerthing' => 'thang',
);

Minimal Code Snippet

<?php
the_post_thumbnail(
    'medium',
    array(
        'class' => 'classes',
        'alt'   => esc_attr( get_the_title() ),
    )
);
?>

WPCS tries to correct that even if it is correctly aligned and says it should not be aligned vertically


Yet, if I plant the array in a function, or plain PHP file, it expects me to align vertically the arrows:

array(
    'thing'        => 'thang',
    'lonngerthing' => 'thing',
)

Error Code

Array double arrow not aligned correctly; expected 1 space(s) between "'alt'" and double arrow, but found 3.
(WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)

Environment

Question Answer
PHP version 8.2
PHP_CodeSniffer version 3.7.2
WordPressCS version 3
@jrfnl
Copy link
Member

jrfnl commented Oct 25, 2023

@smileBeda Tried, but cannot reproduce the issue with the code sample provided.

Also, that sniff has barely had any changes for WPCS 3.x, so there is no reason to see changed behaviour.

Suggest: close, unless a valid reproduction case is added within the next week.

@smileBeda
Copy link
Author

@jrfnl I do not fully understand why, it appears to be related to depth of HTML tree or something else.

See the full code, I placed the double arrow array in several places and you will see that it sometimes flags as wrong, sometimes leaves it alone.

<?php
/**
 * The template for displaying and archive
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Bootscore
 */

the_post_thumbnail(
	'medium',
	array(
		'class' => 'card-img-top img-fluid rounded-0 border-0',
		'alt'   => esc_attr( get_the_title() ),
	)
);
get_header();
?>

<div id="content" class="site-content <?php echo esc_attr( bootscore_container_class() ); ?> py-5 mt-5">
	<div id="primary" class="content-area">
		<?php
		the_post_thumbnail(
			'medium',
			array(
				'class' => 'card-img-top img-fluid rounded-0 border-0',
				'alt'   => esc_attr( get_the_title() ),
			)
		);
		?>
		<?php bs_after_primary(); ?>
		<div class="row">
			<div class="<?php echo esc_attr( bootscore_main_col_class() ); ?>">
				<main id="main" class="site-main">
					<?php get_template_part( 'parts/sections/archive-heading' ); ?>
					<hr>
					<?php if ( have_posts() ) : ?>
						<div class="row row-cols-1 row-cols-md-3 g-4">
							<?php
							while ( have_posts() ) :
								the_post();
								?>
								<div class="col">
									<div class="card h-100 border-0">
										<div class="card-header rounded-0 p-0 border-0">
											<?php
											the_post_thumbnail(
												'medium',
												array(
													'class' => 'card-img-top img-fluid rounded-0 border-0',
													'alt'   => esc_attr( get_the_title() ),
												)
											);
											?>
										</div>
										<div class="card-body bg-dark text-white p-4">
											<a
											href="<?php esc_url( the_permalink() ); ?>"
											class="stretched-link text-white link-underline link-underline-opacity-0"
											>
												<?php the_title( '<h2 class="card-title">', '</h2>' ); ?>
											</a>
											<div class="d-flex justify-content-between align-items-center">
												<a class="read-more text-white z-3 position-relative"
												href="https://blabla.com">
													<?php esc_html_e( 'Bla Bla', 'bootscore' ); ?>
												</a>
												<span>
													<a
													class="read-more text-white text-end z-3 position-relative"
													target="_blank"
													href="<?php echo esc_url( get_post_meta( get_the_ID(), 'meta', true ) ); ?>"
													>
														<?php esc_html_e( 'Bla', 'bootscore' ); ?>
													</a>
													<i class="ms-2 bi bi-arrow-up-right"></i>
												</span>
											</div>
										</div>
									</div>
								</div>
							<?php endwhile; ?>
						</div>
					<?php endif; ?>
					<footer class="entry-footer">
						<?php bootscore_pagination(); ?>
					</footer>
				</main>
			</div>
		</div>
	</div>
</div>

<?php
get_footer();

You will notice that the_post_thumbnail is called 3 times:

  • top of file: WPCS "correctly" expects vertically aligned arrows in the array
  • line 23: WPCS "correctly" expects vertically aligned arrows in the array
  • line 47: WPCS "incorrectly" expects unaligned arrows in the array and will throw warning if you run WPCS on the code shared.

Also hereby attached the full PHP file, in case above indentation gets messed up:
test-wpcs.php.zip

@jrfnl
Copy link
Member

jrfnl commented Oct 26, 2023

@smileBeda Ah! That explains it. That is related to the maxColumn setting as explained here: https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#wordpressarraysmultiplestatementalignment-maximum-column

Again: that behaviour has not changed in WPCS 3.0.0, it was always that way.

@smileBeda
Copy link
Author

smileBeda commented Oct 26, 2023

OK, I then just do not recall having seen it before, not a surprise.

I will try to add a custom rule to extend the column width then.
I allow 120 characters per line, excluding indent. 60 is extremely narrow, barely allows for a standard single post template!

And since it seems to not measure the array length but the full length including indentation from file column 0.... that is reached extremely quickly.

Odd. But ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants