<?php

// Bad, no space after opening control structure.
while ( have_posts() ) { // Okay, comments are okay here.
	// Okay, comments are okay here as well.
} // Okay, comments are okay here.

// See https://github.com/WordPress/WordPress-Coding-Standards/issues/40 .
if ( true ) {

	// code.
} else { // Are we allowed to comment here? If not, message is wrong.
	// ...
}

// Bad.
if ( 'update' === $option_operation['operation'] ) {
	update_option( $option_operation['option_name'], $option_operation['old_value'] );

}

// Good.
if ( 'update' === $option_operation['operation'] ) {
	update_option( $option_operation['option_name'], $option_operation['old_value'] );
}

// Bad.
if ( true ) {}


if ( true ) {
	while ( false ) {

		echo 'OK';

	}
}

if ( false ) :
else :
endif;

if ( false ) :
else :
endif;
// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren 1
$a = function ( $arg ) {}; // Bad.
$a = function ( $arg ) {
	// Ok.
};

$a = function () {
	// Ok.
};

function something( $arg ) {} // Bad.
function foo( $arg ) {
	// Ok.
}

function no_params() {
	// Ok.
}

function another() {} // Bad, space before open parenthesis prohibited.
function and_another() {} // Bad, space before function name prohibited.
function bar() {} // Bad.
function baz() {} // Bad.
function testA() {} // Bad.

function &return_by_ref() {} // Ok.

// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren 0

$a = function() {}; // Ok.
$a = function( $arg ) {}; // Ok.
$a = function( $arg ) {}; // Bad.
$a = function() {}; // Bad.

$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Ok.
$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad.

// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren 1

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Ok.

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, no space before open parenthesis prohibited.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space before opening parenthesis.

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, space between closing parenthesis and control structure required.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.

// Namespaces.
use Foo\Admin;

// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren -1

$a = function( $arg ) {}; // Ok.
$a = function ( $arg ) {}; // Ok.

$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Ok.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Ok.

/*
 * Test for bug where this sniff was triggering a "Blank line found after control structure" error
 * if there is a blank line after the last method in a class.
 *
 * Bug did not trigger when a comment was found after the closing brace of the method.
 *
 * Neither of the below examples should trigger the error.
 */
class Bar_Foo {

	function foo() {
	} // Now you won't see the bug.

}

class Foo_Bar {

	// Now you will.
	function bar() {
	}

}

// Handle try/catch statements as well.
try { // Bad.
	// Something
} catch ( Exception $e ) { // Bad.
	// Something
}

// Upstream bug PEAR #20248.
// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing blank_line_check true
// Bad.
if ( $one ) {
}
// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing blank_line_check false

// Upstream bug PEAR #20247.
do {
	echo 'hi';
} while ( $blah ); // Bad.

// Upstream bug GH #782
// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing blank_line_check true
if ( $foo ) {


    /**
     * Comment
     */
    function foo() {
        // Code here
    }


    /**
     * Comment
     */
    class bar {

    }//end class


}
// phpcs:set WordPress.WhiteSpace.ControlStructureSpacing blank_line_check false

// Check for too many spaces as long as the next non-blank token is on the same line.
function test( $blah ) {} // Bad.
$a = function(   $bar ) {}; // Bad.

if ( 'abc' === $test ) { // Bad.
	echo 'hi';
} elseif ( false === $foo ) { // Bad.
	echo 'bye';
}

do {
	echo 'hi';
} while ( $blah ); // Bad.

while ( $blah ) { // Bad.
	echo 'bye bye';
}

for ( $i = 0; $i < 1; $i++ ) { // Bad.
	echo 'hi';
}

foreach ( $foo as $bar ) { // Bad.
	echo 'hi';
}

if (
	'abc' === $test
) { // Ok.
	echo 'hi';
} elseif (
	false === $foo
	&& true === $bar
) { // Ok.
	echo 'bye';
}

// Bug #976 - the case of the disappearing comment.
if ( isset( $submenu_file ) ) {
	if ( $submenu_file == $sub_item[2] ) {
		$class[] = 'current';
	}
// If plugin_page is set the parent must either match the current page or not physically exist.
// This allows plugin pages with the same hook to exist under different parents.
} else {
	$class[] = 'current';
}

// Test finding & fixing blank line after control structure.
if ( $one ) {
}
elseif ( $two ) {
}
// else if something
else if ( $three ) {
} // else do something
else {
}

do {
}
// Comment
while ( $a === $b );

if ( $foo ) {
	try {
		// Something
	} catch ( Exception $e ) {
		// Something
	}
}

if ( $foo ) {
	try {
		// Something
	} catch ( Exception $e ) {
		// Something
	}//end try/catch <- Bad: "blank line after".
}

if ( $foo ) {
	try { // Bad.
		// Something
	} catch ( Exception $e ) {
		// Something
	} // End try/catch <- Bad: "blank line after".
}

// Issue 1202.
function hi(): array
{
    return [];
}

// Issue 1420.
function returntype1(): string {} // Ok.
function returntype2( $input ): string {} // Ok.
function returntype3( $input ) : string {} // Ok.
function returntype4( string $input ): string {} // Ok.
function returntype5( string $input ) : string {} // Ok.
function returntype6( string $input, array $inputs ): string {} // Ok.
function returntype7( string $input, array $inputs ) : string {} // Ok.
$returntype = function (): string {}; // Ok.
$returntype = function ( $input ): string {}; // Ok.
$returntype = function ( $input ) : string {}; // Ok.
$returntype = function ( string $input ): string {}; // Ok.
$returntype = function ( string $input ) : string {}; // Ok.
$returntype = function ( string $input, array $inputs ): string {}; // Ok.
$returntype = function ( string $input, array $inputs ) : string {}; // Ok.

// Issue 1792.
$matching_options = array_filter(
	$imported_options,
	function ( $option ) use ( $option_id ): bool {
		return $option['id'] === $option_id;
	}
);

$matching_options = array_filter(
	$imported_options,
	function ( $option ) use ( $option_id ) : bool {
		return $option['id'] === $option_id;
	}
);
