Skip to content

Sniff WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement fails on abstract methods #580

@kevinfodness

Description

@kevinfodness

Bug Description

If you define an abstract class that sets up a filter in its constructor and sets the callback to an abstract method in the class which will be defined by implementing classes, the WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement sniff causes phpcs to crash, because it's trying to evaluate the return statement in an abstract method.

The specific error message being returned is this:

An error occurred during processing; checking has been aborted. The error message was: Undefined index: scope_opener in /path/to/.composer/vendor/automattic/vipwpcs/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php on line 194 (Internal.Exception)

Minimal Code Snippet

abstract class good_example_abstract_class { // Ok.
	public function __construct() {
		add_filter( 'good_example_class_filter', [ $this, 'class_filter' ] );
	}

	abstract public function class_filter( $param );
}

class good_example_abstract_class_implementation { // Ok.
	public function class_filter( $param ) {
		if ( 1 === 1 ) {
			if ( 1 === 0 ) {
				return 'whoops';
			} else {
				return 'here!';
			}
		}
		return 'This is Okay';
	}
}

Error Code

No error code should be triggered.

Environment

Use php -v and composer show to get versions.

Question Answer
PHP version 7.3.0
PHP_CodeSniffer version 3.5.6
VIPCS version 2.2.0

Additional Context (optional)

Fixed in PR #581

Tested Against master branch?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions