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

[2.x] Indention Issues With Closures #206

Closed
mbabker opened this issue Sep 10, 2017 · 9 comments
Closed

[2.x] Indention Issues With Closures #206

mbabker opened this issue Sep 10, 2017 · 9 comments

Comments

@mbabker
Copy link
Contributor

mbabker commented Sep 10, 2017

See https://travis-ci.org/joomla/jissues/jobs/273897032 for reference

Basically, indents for control structures aren't being handled correctly in Closures

@photodude
Copy link
Contributor

possibly related to this issue squizlabs/PHP_CodeSniffer#1580

@photodude
Copy link
Contributor

photodude commented Dec 26, 2017

Here is a case that is failing from the CMS administrator\components\com_installer\models\languages.php

The sniff that is at issue is Joomla.ControlStructures.ControlStructuresBrackets.SpaceBeforeBrace

expected

// Sort the array by value of subarray
usort(
	$languages,
	function($a, $b) use ($that)
	{
		$ordering = $that->getState('list.ordering');

		if (strtolower($that->getState('list.direction')) === 'asc')
		{
			return StringHelper::strcmp($a->$ordering, $b->$ordering);
		}
		else
		{
			return StringHelper::strcmp($b->$ordering, $a->$ordering);
		}
	}
);

Current result of the sniff

// Sort the array by value of subarray
usort(
	$languages,
	function($a, $b) use ($that)
	{
		$ordering = $that->getState('list.ordering');

		if (strtolower($that->getState('list.direction')) === 'asc')
	{
			return StringHelper::strcmp($a->$ordering, $b->$ordering);
		}
		else
	{
			return StringHelper::strcmp($b->$ordering, $a->$ordering);
		}
	}
);

@photodude
Copy link
Contributor

photodude commented Dec 26, 2017

Another CMS case from plugins\system\sef\sef.php where Joomla.ControlStructures.ControlStructuresBrackets.SpaceBeforeBrace fails on closures

expected

$buffer = preg_replace_callback(
	$regex,
	function ($match) use ($base, $protocols)
	{
		preg_match_all('#(?:[^\s]+)\s*(?:[\d\.]+[wx])?(?:\,\s*)?#i', $match[1], $matches);

		foreach ($matches[0] as &$src)
		{
			$src = preg_replace('#^(?!/|' . $protocols . '|\#|\')(.+)#', $base . '$1', $src);
		}

		return ' srcset="' . implode($matches[0]) . '"';
	},
	$buffer
);

Current result from sniff

$buffer = preg_replace_callback(
	$regex,
	function ($match) use ($base, $protocols)
	{
		preg_match_all('#(?:[^\s]+)\s*(?:[\d\.]+[wx])?(?:\,\s*)?#i', $match[1], $matches);

		foreach ($matches[0] as &$src)
	{
			$src = preg_replace('#^(?!/|' . $protocols . '|\#|\')(.+)#', $base . '$1', $src);
		}

		return ' srcset="' . implode($matches[0]) . '"';
	},
	$buffer
);

@photodude
Copy link
Contributor

I've done a little digging here. seems the issue might stem from our reliance on $tokens[$stackPtr]['level'] at line 176 to determine the expected indent for the scope opening brace.

I propose we remove this portion of our rule and rely on the following parts of the ruleset that also cover this indenting

Generic.WhiteSpace.ScopeIndent.IncorrectExact
Generic.WhiteSpace.ScopeIndent.Incorrect
Squiz.WhiteSpace.ScopeClosingBrace.Indent
PEAR.Functions.FunctionCallSignature.Indent

note: Generic.WhiteSpace.ScopeIndent reports in tabs as expected.

I wonder what the overlap is and if we can eliminate some sniffs so there is no duplication in the checks.

@photodude
Copy link
Contributor

#218 now fixes this issue and has a test to verify that it fixes the issue.

@mbabker
Copy link
Contributor Author

mbabker commented Jan 8, 2018

This might be covered by the same, but will this also cover PHP 7 anonymous classes, i.e. from com_code?

@photodude
Copy link
Contributor

Yes, I believe it should as I included T_ANON_CLASS for Anonymous classes in the conditional for the fix.

@photodude
Copy link
Contributor

Just did a test locally, seems to be good. I'll throw a test together.

@photodude
Copy link
Contributor

Close via #218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants