Skip to content

Commit

Permalink
Merge branch 'master' into 3.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonge committed Jul 12, 2019
2 parents a00428d + bcd5ae7 commit ccf8482
Show file tree
Hide file tree
Showing 24 changed files with 1,134 additions and 354 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ phpunit.xml
.settings
.DS_Store
.idea
.vscode
.vscode
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ matrix:
- php: 7.0
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4
- php: nightly
allow_failures:
- php: 7.2
- php: 7.4
- php: nightly

before_script:
Expand Down
448 changes: 423 additions & 25 deletions Joomla/ExampleRulesets/Joomla-CMS/ruleset.xml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Joomla/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
{
if ($tokens[$returnToken]['code'] === T_CLOSURE
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
)
)
{
$returnToken = $tokens[$returnToken]['scope_closer'];
continue;
Expand All @@ -156,7 +156,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
if ($tokens[$returnToken]['code'] === T_RETURN
|| $tokens[$returnToken]['code'] === T_YIELD
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
)
)
{
break;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
{
if ($tokens[$returnToken]['code'] === T_CLOSURE
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
)
)
{
$returnToken = $tokens[$returnToken]['scope_closer'];
continue;
Expand All @@ -195,7 +195,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
if ($tokens[$returnToken]['code'] === T_RETURN
|| $tokens[$returnToken]['code'] === T_YIELD
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
)
)
{
break;
}
Expand Down
50 changes: 42 additions & 8 deletions Joomla/Sniffs/ControlStructures/ControlStructuresBracketsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,51 @@ public function process(File $phpcsFile, $stackPtr)
}
}

// Anonymous classes and functions set the indent at one plus their own indent level.
if ($phpcsFile->hasCondition($stackPtr, T_CLOSURE) === true
|| $phpcsFile->hasCondition($stackPtr, T_ANON_CLASS) === true)
{
$expected = ($tokens[$stackPtr]['level'] + 1) * $this->indent;
}
else
$baseLevel = $tokens[$stackPtr]['level'];

/**
* Take into account any nested parenthesis that don't contribute to the level (often required for
* closures and anonymous classes
*/
if (array_key_exists('nested_parenthesis', $tokens[$stackPtr]) === true)
{
$expected = $tokens[$stackPtr]['level'] * $this->indent;
$nestedStructures = $tokens[$stackPtr]['nested_parenthesis'];
$nestedCount = 0;

foreach ($nestedStructures as $start => $end)
{
/**
* Crude way of checking for a chained method which requires an extra indent. We navigate to the open
* parenthesis of the nested structure. The element before that is the function name. Before that we
* check for an operator (->) and a whitespace before it (which makes it a chained method on a new line)
* TODO: Is there a better way to check for a chained method? This feels very dirty!
*/
if ($tokens[$start - 2]['type'] === 'T_OBJECT_OPERATOR' && $tokens[$start - 3]['type'] === 'T_WHITESPACE')
{
/**
* If we have an anonymous function/class on the same line as our chained method then we
* balance out so only increase the count by 1. Else by 2.
*/
if ($tokens[$start + 1]['type'] === 'T_CLOSURE' || $tokens[$start + 1]['type'] === 'T_ANON_CLASS')
{
$nestedCount++;
}
else
{
$nestedCount += 2;
}
}
else
{
$nestedCount++;
}
}

$baseLevel += $nestedCount;
}

$expected = $baseLevel * $this->indent;

// We need to divide by 4 here since there is a space vs tab intent in the check vs token
$expected /= $this->indent;
$spaces /= $this->indent;
Expand Down
138 changes: 138 additions & 0 deletions Joomla/Tests/ControlStructures/ControlStructuresBracketsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,141 @@ $this->attachRule(
}
}
);

// Round 2 Closure indentation tests
$i = 1;
$func = function ($match) use ($query, $args, &$i)
{
if (isset($match[6]) && $match[6] === '%')
{
return '%';
}
// No argument required, do not increment the argument index.
switch ($match[5])
{
case 't':
return $query->currentTimestamp();
break;
case 'z':
if (true)
{
return $query->nullDate(true);
}
return $query->nullDate(false);
break;
case 'Z':
return $query->nullDate(true);
break;
}
};

class Foo
{
public function format($format)
{
$query = $this;
$args = \array_slice(\func_get_args(), 1);
array_unshift($args, null);

$i = 1;
$func = function ($match) use ($query, $args, &$i) {
if (isset($match[6]) && $match[6] === '%')
{
return '%';
}

};
}
}

// Anonymous classes test
$this->attachRule(
new class($this) implements RulesInterface
{
public function parse(&$segments, &$vars)
{
// Move forward based on view
switch ($segments)
{
case 'tracker':
usort(
$view,
function($a, $b) use ($vars)
{
$ordering = $vars->getState('list.ordering');

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

return;
}
}
);

class Application
{
public function doThings($container)
{
$container->alias(MyClass::class, 'MyClass')
->share(
'MyClass',
function (Container $container) {
doThing();

if ($thing)
{
bar();
}

return foo();
},
true
);
}
}

class Application
{
public function doThings($container)
{
$container->share(
'MyClass',
function (Container $container) {
doThing();

if ($thing)
{
bar();
}
else {
something();

if (somethingelse())
{
bar();
}

anotherFunction()
->withNested(function() {
if ($foo){
bar();
}
});

return foo();
}
},
true
);
}
}
Loading

0 comments on commit ccf8482

Please sign in to comment.