Page MenuHomePhabricator

Add sniff to check if all returns of a function are void or non-void returns
Open, Needs TriagePublic

Description

Bad code can contains one return with a value and one without a value:

	/**
	 * @param string $arg1
	 * @return string
	 */
	public function funcTest( $arg1 ) {
		if ( $arg1 === '' ) {
			return 'none';
		}
		return;
	}

Such code can surprise the caller by getting a void and may break.

A sniff should check if all return are void or non-void inside the code.

Return inside closures or inner functions could make this a bit harder to check for.
Ignore return in __construct

Event Timeline

DannyS712 added a subscriber: DannyS712.

@Umherirrender should this autofix to include void in the return (@return int -> @return int|void) or just add a warning with no autofix?

I would not add an autofix for this case. It should be looked by a human if there is an error or not.

I would not add an autofix for this case. It should be looked by a human if there is an error or not.

Okay

Change 591451 had a related patch set uploaded (by DannyS712; owner: DannyS712):
[mediawiki/tools/codesniffer@master] FunctionCommentSniff: warn if returning void when not expected

https://gerrit.wikimedia.org/r/591451