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

Undefined index in ControlStructureSpacingSniff when using Closures with return types #1792

Closed
1 task
mbabker opened this issue Aug 28, 2019 · 3 comments · Fixed by #1837
Closed
1 task
Milestone

Comments

@mbabker
Copy link

mbabker commented Aug 28, 2019

Bug Description

In files where I am using Closures with use statements and return typehints, I am getting this error:

 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: Undefined index: parenthesis_owner in
   |       | <project>/vendor/wp-coding-standards/wpcs/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php on line 349 (Internal.Exception)

The issue seems to be if there is not a space between the close parenthesis of the use statement and the colon for the return type declaration.

Minimal Code Snippet

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

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

Error Code

N/A

Environment

Question Answer
PHP version 7.3.6
PHP_CodeSniffer version 3.5.2
WPCS version 2.2.0
WPCS install type Composer project local
IDE (if relevant) PhpStorm 2019.2.5

Additional Context (optional)

N/A

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of WPCS.

Reviewing code diff since last release, this file is unchanged, so I didn't test against the develop branch but can if need be

@jrfnl
Copy link
Member

jrfnl commented Sep 24, 2019

Related to #1071, #1101

The short summary is that functions and closures were added to the ControlStructureSpacing sniff, but shouldn't have been. They need their own sniff(s).

@mbabker
Copy link
Author

mbabker commented Nov 20, 2019

Updated the issue description a bit since PhpStorm started highlighting some lines after updating all the tooling. Through PhpStorm's inspector, somehow I worked out that the error only exists on a Closure that has a use statement and that the undefined index error goes away if you put a space between the closing parenthesis and the colon. Normal functions and class methods, as well as Closures without a use, don't error if there is no space in between. So, hopefully that helps narrow down a smaller bugfix without needing any larger refactoring.

@jrfnl
Copy link
Member

jrfnl commented Nov 21, 2019

@mbabker Thanks for tracking that down. PR #1837 should fix that for now.

@jrfnl jrfnl added this to the 2.x Next milestone Dec 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants