Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions wp-phptidy.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
$fix_docblock_space = false;
$add_blank_lines = false;
$indent = true;
$fix_not_operator_space = true;

///////////// END OF DEFAULT CONFIGURATION ////////////////

Expand Down Expand Up @@ -1059,6 +1060,11 @@ function fix_separation_whitespace( &$tokens ) {
// Set the existing whitespace before the bracket to exactly one space or newline
$tokens[$key+1][1] = separation_whitespace( $control_structure );
}
} elseif (
$tokens[$key] === '?'
) {
// Insert space after the not operator
fix_ternary_not_operator_space( $tokens, $key );
}

} else {
Expand Down Expand Up @@ -1103,10 +1109,18 @@ function fix_separation_whitespace( &$tokens ) {
}
break;
case T_IF:
// Insert space after the not operator
fix_not_operator_space( $tokens, $key );
case T_ELSEIF:
// Insert space after the not operator
fix_not_operator_space( $tokens, $key );
case T_FOR:
// Insert space after the not operator
fix_not_operator_space( $tokens, $key );
case T_FOREACH:
case T_WHILE:
// Insert space after the not operator
fix_not_operator_space( $tokens, $key );
case T_SWITCH:
// At least 1 space between a statement and a opening round bracket
if ( $tokens[$key+1] === "(" ) {
Expand Down Expand Up @@ -1226,6 +1240,84 @@ function fix_round_bracket_space( &$tokens ) {
}


/**
* Adds one space after the NOT operator
*
* @param array $tokens (reference)
* @param int index of the statement token in the tokens array
*/
function fix_not_operator_space( &$tokens, $key ) {

$parentheses_opened = 0;
$parentheses_closed = 0;

for ( $index = $key + 1; $index < count( $tokens ); $index++ ) {
if (
// If the current token is a start round bracket...
'(' === $tokens[ $index ]
) {
// Increase the count of start round brackets
$parentheses_opened++;
}
else if (
// If the current token is an end round bracket...
')' === $tokens[ $index ]
) {
// Increase the count of end round brackets
$parentheses_closed++;
if (
// If the count of open round brackets equals the count of closed round brackets...
$parentheses_opened === $parentheses_closed
) {
// This is the final closing round bracket
break;
}
}
else if (
// If the previous token is the not operator...
( isset( $tokens[ $index - 1 ][0] ) and '!' === $tokens[ $index - 1 ][0] ) and
// ...and the current token is not whitespace
( isset( $tokens[ $index ][0] ) and T_WHITESPACE !== $tokens[ $index ][0] )
) {
// Insert whitespace
array_splice( $tokens, $index, 0, array( array( T_WHITESPACE, ' ' ) ) );
}
}
}


function fix_ternary_not_operator_space ( &$tokens, $key ) {

$equals_index = -1;

for ( $index = $key - 1; $index > -1; $index-- ) {
if (
// If the current token is the equal sign
$tokens[$index] === '='
) {
// Make spacing adjustments between this index and the $key
$equals_index = $index;
break;
}
}

if ( $equals_index > -1 ) {
for ( $index = $equals_index + 2; $index < $key; $index++ ) {
if (
// If the previous token is the not operator...
( isset( $tokens[$index][0] ) and '!' === $tokens[$index-1][0] ) and
// ...and the current token is not whitespace
( isset( $tokens[ $index ][0] ) and T_WHITESPACE !== $tokens[ $index ][0] )
) {
// Insert whitespace
array_splice( $tokens, $index, 0, array( array( T_WHITESPACE, ' ' ) ) );
break;
}
}
}
}


/**
* Fixes the format of a DocBlock
*
Expand Down