welovewordpress / SublimePhpTidy

Plugin for Sublime Text 2 to format PHP code to meet the WordPress Coding Standards using a modified version of phptidy
GNU General Public License v2.0
109 stars 30 forks source link

Fix whitespace for square brackets, operators, and DocBlocks params #36

Open keesiemeijer opened 7 years ago

keesiemeijer commented 7 years ago

This pull request fixes whitespace issues with the WordPress Coding Standards for PHP_CodeSniffer rules.

It adds and removes spaces where needed in square brackets and around operators. The add_operator_space() formatter was copied from https://github.com/cmrcx/phptidy

Example before:

$array[  'string'  ] = -1;
$array[$key] = true;
$array[$key+1] = ($var*3)-1;
$array[$var[ 'string' ][ 0 ]] = true;
$array[ ] = 'empty square brackets';
$var = trim('string').'concatenated';
if (!($var1===$var2)&&($var2>3)) {
    $var=true;
}

And after:

$array['string'] = -1;
$array[ $key ] = true;
$array[ $key + 1 ] = ( $var * 3 ) - 1;
$array[ $var['string'][0] ] = true;
$array[] = 'empty square brackets';
$var = trim( 'string' ) . 'concatenated';
if ( ! ( $var1 === $var2 ) && ( $var2 > 3 ) ) {
    $var = true;
}

DocBlock params PhpTidy creates two spaces after the parameter type declaration.

/**
 * @param string  $parameter Parameter.
 */

The padding for the type is hardcoded at 7. This PR calculates the real padding needed.

Example before this PR. In this example there was only one space used between all the types, variables and descriptions. After running PhpTidy the spaces for $var2 are not in line with $variable.

/**
 * A filter
 *
 * @since 1.0
 *
 * @param WP_Query|array $variable Variable 1.
 * @param int     $var2     Variable 2.
 */
return apply_filters( 'my_filter', $variable, $var2 );

Example after running PhpTidy with this pull request:

/**
 * A filter
 *
 * @since 1.0
 *
 * @param WP_Query|array $variable Variable 1.
 * @param int            $var2     Variable 2.
 */
return apply_filters( 'my_filter', $variable, $var2 );