Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Nov 23, 2019
2 parents a4513fa + b24b0a9 commit e2e29f4
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,7 @@ The example below will add two rules to the default list of PSR2 set rules:
;
**NOTE**: ``exclude`` will work only for directories, so if you need to exclude file, try ``notPath``.
Both ``exclude`` and ``notPath`` methods accept only relative paths to the ones defined with the ``in`` method.

See `Symfony\\Finder <https://symfony.com/doc/current/components/finder.html>`_
online documentation for other `Finder` methods.
Expand Down
1 change: 1 addition & 0 deletions src/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public static function getHelpCopy()
?>
**NOTE**: ``exclude`` will work only for directories, so if you need to exclude file, try ``notPath``.
Both ``exclude`` and ``notPath`` methods accept only relative paths to the ones defined with the ``in`` method.
See `Symfony\Finder` (<url>https://symfony.com/doc/current/components/finder.html</url>)
online documentation for other `Finder` methods.
Expand Down
11 changes: 4 additions & 7 deletions src/Fixer/PhpUnit/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -175,6 +176,8 @@ private function fixAssertPositive(Tokens $tokens, $index, $method)
*/
private function fixAssert(array $map, Tokens $tokens, $index, $method)
{
$functionsAnalyzer = new FunctionsAnalyzer();

$sequence = $tokens->findSequence(
[
[T_STRING, $method],
Expand All @@ -188,13 +191,7 @@ private function fixAssert(array $map, Tokens $tokens, $index, $method)
}

$sequenceIndexes = array_keys($sequence);
$operatorIndex = $tokens->getPrevMeaningfulToken($sequenceIndexes[0]);
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
if (
!($tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static']))
) {
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $sequenceIndexes[0])) {
return null;
}

Expand Down
12 changes: 4 additions & 8 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -382,6 +383,8 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall)

private function getPreviousAssertCall(Tokens $tokens)
{
$functionsAnalyzer = new FunctionsAnalyzer();

for ($index = $tokens->count(); $index > 0; --$index) {
$index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]);
if (null === $index) {
Expand All @@ -400,14 +403,7 @@ private function getPreviousAssertCall(Tokens $tokens)
continue;
}

$operatorIndex = $tokens->getPrevMeaningfulToken($index);
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);

if (
!($tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static']))
) {
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $index)) {
continue;
}

Expand Down
10 changes: 3 additions & 7 deletions src/Fixer/PhpUnit/PhpUnitStrictFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -100,6 +101,7 @@ public function isRisky()
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$argumentsAnalyzer = new ArgumentsAnalyzer();
$functionsAnalyzer = new FunctionsAnalyzer();

foreach ($this->configuration['assertions'] as $methodBefore) {
$methodAfter = self::$assertionMap[$methodBefore];
Expand All @@ -111,13 +113,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
break;
}

$operatorIndex = $tokens->getPrevMeaningfulToken($methodIndex);
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
if (
!($tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static']))
) {
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $methodIndex)) {
continue;
}

Expand Down
22 changes: 7 additions & 15 deletions src/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
Expand Down Expand Up @@ -424,7 +425,7 @@ private function fixPhpUnitClass(Tokens $tokens, $startIndex, $endIndex)

$operatorIndex = $tokens->getPrevMeaningfulToken($index);
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
if (!$this->needsConversion($tokens, $operatorIndex, $referenceIndex, $callType)) {
if (!$this->needsConversion($tokens, $index, $referenceIndex, $callType)) {
continue;
}

Expand All @@ -434,27 +435,18 @@ private function fixPhpUnitClass(Tokens $tokens, $startIndex, $endIndex)
}

/**
* @param int $operatorIndex
* @param int $index
* @param int $referenceIndex
* @param string $callType
*
* @return bool
*/
private function needsConversion(Tokens $tokens, $operatorIndex, $referenceIndex, $callType)
private function needsConversion(Tokens $tokens, $index, $referenceIndex, $callType)
{
if ($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static'])) {
return self::CALL_TYPE_STATIC !== $callType;
}

if ($tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this'])) {
return self::CALL_TYPE_THIS !== $callType;
}

if ($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self'])) {
return self::CALL_TYPE_SELF !== $callType;
}
$functionsAnalyzer = new FunctionsAnalyzer();

return false;
return $functionsAnalyzer->isTheSameClassCall($tokens, $index)
&& !$tokens[$referenceIndex]->equals($this->conversionMap[$callType][1], false);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Tokenizer/Analyzer/FunctionsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,30 @@ public function getFunctionReturnType(Tokens $tokens, $methodIndex)

return new TypeAnalysis($type, $typeStartIndex, $typeEndIndex);
}

/**
* @param int $index
*
* @return bool
*/
public function isTheSameClassCall(Tokens $tokens, $index)
{
if (!$tokens->offsetExists($index)) {
return false;
}

$operatorIndex = $tokens->getPrevMeaningfulToken($index);
if (!$tokens->offsetExists($operatorIndex)) {
return false;
}

$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
if (!$tokens->offsetExists($referenceIndex)) {
return false;
}

return $tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this'], false)
|| $tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self'], false)
|| $tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static'], false);
}
}
1 change: 1 addition & 0 deletions tests/Fixer/PhpUnit/PhpUnitConstructFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function provideTestFixCases()
$this->generateCases('<?php $this->assert%s%s($a); //%s %s', '<?php $this->assert%s(%s, $a); //%s %s'),
$this->generateCases('<?php $this->assert%s%s($a, "%s", "%s");', '<?php $this->assert%s(%s, $a, "%s", "%s");'),
$this->generateCases('<?php static::assert%s%s($a); //%s %s', '<?php static::assert%s(%s, $a); //%s %s'),
$this->generateCases('<?php STATIC::assert%s%s($a); //%s %s', '<?php STATIC::assert%s(%s, $a); //%s %s'),
$this->generateCases('<?php self::assert%s%s($a); //%s %s', '<?php self::assert%s(%s, $a); //%s %s')
);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ public function provideTestFixCases()
'<?php self::assertNull($a);',
'<?php self::assertTrue(is_null($a));',
];
$cases[] = [
'<?php SELF::assertNull($a);',
'<?php SELF::assertTrue(is_null($a));',
];

return $cases;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/PhpUnit/PhpUnitStrictFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function provideTestFixCases()
"<?php static::{$methodAfter}(1, 2);",
"<?php static::{$methodBefore}(1, 2);",
];
$cases[] = [
"<?php STATIC::{$methodAfter}(1, 2);",
"<?php STATIC::{$methodBefore}(1, 2);",
];
}

return $cases;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ public function baz2()
$this->assertSame(1, 22);
$this->assertSame(1, 22);
$this->assertSame(1, 22);
$this->assertSame(1, 23);
}
}
Expand Down Expand Up @@ -427,6 +428,7 @@ public function baz2()
$this->assertSame(1, 22);
self::assertSame(1, 22);
static::assertSame(1, 22);
STATIC::assertSame(1, 23);
}
}
Expand All @@ -439,7 +441,7 @@ public function baz2()
'do not change class property and method signature' => [
<<<'EOF'
<?php
class HavingPropertyWithNameAsMethodToUpdateTest extends PHPUnit
class FooTest extends TestCase
{
public function foo()
{
Expand All @@ -448,6 +450,19 @@ public function foo()
public function assertSame($foo, $bar){}
}
EOF
,
],
'do not change when only case is different' => [
<<<'EOF'
<?php
class FooTest extends TestCase
{
public function foo()
{
STATIC::assertSame(1, 1);
}
}
EOF
,
],
Expand Down
70 changes: 70 additions & 0 deletions tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,74 @@ public function provideFunctionsWithReturnTypePhp74Cases()
['<?php fn($a): \Foo/** TODO: change to something else */\Bar => null;', 1, new TypeAnalysis('\Foo\Bar', 7, 11)],
];
}

/**
* @param bool $isTheSameClassCall
* @param string $code
* @param int $index
*
* @dataProvider provideIsTheSameClassCallCases
*/
public function testIsTheSameClassCall($isTheSameClassCall, $code, $index)
{
$tokens = Tokens::fromCode($code);
$analyzer = new FunctionsAnalyzer();

static::assertSame($isTheSameClassCall, $analyzer->isTheSameClassCall($tokens, $index));
}

public function provideIsTheSameClassCallCases()
{
$template = '<?php
class Foo {
public function methodOne() {
$x = %sotherMethod(1, 2, 3);
}
}
';

yield [
false,
sprintf($template, '$this->'),
-1,
];

// 24 is index of "otherMethod" token

for ($i = 0; $i < 40; ++$i) {
yield [
24 === $i,
sprintf($template, '$this->'),
$i,
];
yield [
24 === $i,
sprintf($template, 'self::'),
$i,
];
yield [
24 === $i,
sprintf($template, 'static::'),
$i,
];
}

yield [
true,
sprintf($template, '$THIS->'),
24,
];

yield [
false,
sprintf($template, '$notThis->'),
24,
];

yield [
false,
sprintf($template, 'Bar::'),
24,
];
}
}

0 comments on commit e2e29f4

Please sign in to comment.