Skip to content

Commit 1ce01cb

Browse files
committed
Merge branch '2.14' into 2.15
2 parents df7d7a1 + 63cc07e commit 1ce01cb

File tree

2 files changed

+111
-59
lines changed

2 files changed

+111
-59
lines changed

src/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixer.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public function isRisky()
317317
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
318318
{
319319
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
320-
foreach (array_reverse(iterator_to_array($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens))) as $indexes) {
320+
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
321321
$this->fixPhpUnitClass($tokens, $indexes[0], $indexes[1]);
322322
}
323323
}
@@ -377,18 +377,48 @@ private function fixPhpUnitClass(Tokens $tokens, $startIndex, $endIndex)
377377
{
378378
$analyzer = new TokensAnalyzer($tokens);
379379

380-
for ($index = $endIndex - 1; $index > $startIndex; --$index) {
380+
for ($index = $startIndex; $index < $endIndex; ++$index) {
381+
// skip anonymous classes
382+
if ($tokens[$index]->isGivenKind(T_CLASS)) {
383+
$index = $this->findEndOfNextBlock($tokens, $index);
384+
385+
continue;
386+
}
387+
388+
$callType = $this->configuration['call_type'];
389+
390+
if ($tokens[$index]->isGivenKind(T_FUNCTION)) {
391+
// skip lambda
392+
if ($analyzer->isLambda($index)) {
393+
$index = $this->findEndOfNextBlock($tokens, $index);
394+
395+
continue;
396+
}
397+
398+
// do not change `self` to `this` in static methods
399+
if ('this' === $callType) {
400+
$attributes = $analyzer->getMethodAttributes($index);
401+
if (false !== $attributes['static']) {
402+
$index = $this->findEndOfNextBlock($tokens, $index);
403+
404+
continue;
405+
}
406+
}
407+
}
408+
381409
if (!$tokens[$index]->isGivenKind(T_STRING) || !isset($this->staticMethods[$tokens[$index]->getContent()])) {
382410
continue;
383411
}
384412

385413
$nextIndex = $tokens->getNextMeaningfulToken($index);
386414
if (!$tokens[$nextIndex]->equals('(')) {
415+
$index = $nextIndex;
416+
387417
continue;
388418
}
389419

390420
$methodName = $tokens[$index]->getContent();
391-
$callType = $this->configuration['call_type'];
421+
392422
if (isset($this->configuration['methods'][$methodName])) {
393423
$callType = $this->configuration['methods'][$methodName];
394424
}
@@ -399,10 +429,6 @@ private function fixPhpUnitClass(Tokens $tokens, $startIndex, $endIndex)
399429
continue;
400430
}
401431

402-
if (self::CALL_TYPE_THIS === $callType && $this->isInsideStaticFunction($tokens, $analyzer, $index)) {
403-
continue;
404-
}
405-
406432
$tokens[$operatorIndex] = new Token($this->conversionMap[$callType][0]);
407433
$tokens[$referenceIndex] = new Token($this->conversionMap[$callType][1]);
408434
}
@@ -434,34 +460,15 @@ private function needsConversion(Tokens $tokens, $operatorIndex, $referenceIndex
434460
}
435461

436462
/**
437-
* @param Tokens $tokens
438-
* @param TokensAnalyzer $analyzer
439-
* @param int $index
463+
* @param Tokens $tokens
464+
* @param int $index
440465
*
441-
* @return bool
466+
* @return int
442467
*/
443-
private function isInsideStaticFunction(Tokens $tokens, TokensAnalyzer $analyzer, $index)
468+
private function findEndOfNextBlock(Tokens $tokens, $index)
444469
{
445-
$functionIndex = $tokens->getPrevTokenOfKind($index, [[T_FUNCTION]]);
446-
while ($analyzer->isLambda($functionIndex)) {
447-
$openingCurlyBraceIndex = $tokens->getNextTokenOfKind($functionIndex, ['{']);
448-
$closingCurlyBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openingCurlyBraceIndex);
449-
450-
if ($closingCurlyBraceIndex > $index) {
451-
$prev = $tokens->getPrevMeaningfulToken($functionIndex);
452-
if ($tokens[$prev]->isGivenKind(T_STATIC)) {
453-
return true;
454-
}
455-
}
456-
457-
$functionIndex = $tokens->getPrevTokenOfKind($functionIndex, [[T_FUNCTION]]);
458-
}
459-
460-
$prev = $functionIndex;
461-
do {
462-
$prev = $tokens->getPrevMeaningfulToken($prev);
463-
} while ($tokens[$prev]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL]));
470+
$index = $tokens->getNextTokenOfKind($index, ['{']);
464471

465-
return $tokens[$prev]->isGivenKind(T_STATIC);
472+
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
466473
}
467474
}

tests/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixerTest.php

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ public function testBaseCase()
204204
$this->fail('foo');
205205
206206
$lambda = function () {
207-
$this->assertSame(1, 2);
208-
$this->assertSame(1, 2);
209-
$this->assertSame(1, 2);
207+
$this->assertSame(1, 23);
208+
self::assertSame(1, 23);
209+
static::assertSame(1, 23);
210210
};
211211
}
212212
}
@@ -223,9 +223,9 @@ public function testBaseCase()
223223
static::fail('foo');
224224
225225
$lambda = function () {
226-
$this->assertSame(1, 2);
227-
self::assertSame(1, 2);
228-
static::assertSame(1, 2);
226+
$this->assertSame(1, 23);
227+
self::assertSame(1, 23);
228+
static::assertSame(1, 23);
229229
};
230230
}
231231
}
@@ -329,6 +329,10 @@ public function bar()
329329
self::assertSame(1, 2);
330330
static::assertSame(1, 2);
331331
};
332+
333+
$myProphecy->setCount(0)->will(function () {
334+
$this->getCount()->willReturn(0);
335+
});
332336
}
333337
334338
static public function baz()
@@ -371,23 +375,23 @@ public function foo()
371375
public function bar()
372376
{
373377
$lambdaOne = static function () {
374-
$this->assertSame(1, 2);
375-
self::assertSame(1, 2);
376-
static::assertSame(1, 2);
378+
$this->assertSame(1, 21);
379+
self::assertSame(1, 21);
380+
static::assertSame(1, 21);
377381
};
378382
379383
$lambdaTwo = function () {
380-
$this->assertSame(1, 2);
381-
$this->assertSame(1, 2);
382-
$this->assertSame(1, 2);
384+
$this->assertSame(1, 21);
385+
self::assertSame(1, 21);
386+
static::assertSame(1, 21);
383387
};
384388
}
385389
386-
public function baz()
390+
public function baz2()
387391
{
388-
$this->assertSame(1, 2);
389-
$this->assertSame(1, 2);
390-
$this->assertSame(1, 2);
392+
$this->assertSame(1, 22);
393+
$this->assertSame(1, 22);
394+
$this->assertSame(1, 22);
391395
}
392396
393397
}
@@ -407,23 +411,23 @@ public function foo()
407411
public function bar()
408412
{
409413
$lambdaOne = static function () {
410-
$this->assertSame(1, 2);
411-
self::assertSame(1, 2);
412-
static::assertSame(1, 2);
414+
$this->assertSame(1, 21);
415+
self::assertSame(1, 21);
416+
static::assertSame(1, 21);
413417
};
414418
415419
$lambdaTwo = function () {
416-
$this->assertSame(1, 2);
417-
self::assertSame(1, 2);
418-
static::assertSame(1, 2);
420+
$this->assertSame(1, 21);
421+
self::assertSame(1, 21);
422+
static::assertSame(1, 21);
419423
};
420424
}
421425
422-
public function baz()
426+
public function baz2()
423427
{
424-
$this->assertSame(1, 2);
425-
self::assertSame(1, 2);
426-
static::assertSame(1, 2);
428+
$this->assertSame(1, 22);
429+
self::assertSame(1, 22);
430+
static::assertSame(1, 22);
427431
}
428432
429433
}
@@ -433,7 +437,7 @@ public function baz()
433437
'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
434438
],
435439
],
436-
[
440+
'do not change class property and method signature' => [
437441
<<<'EOF'
438442
<?php
439443
class HavingPropertyWithNameAsMethodToUpdateTest extends PHPUnit
@@ -442,10 +446,51 @@ public function foo()
442446
{
443447
$this->assertSame = 42;
444448
}
449+
450+
public function assertSame($foo, $bar){}
445451
}
446452
EOF
447453
,
448454
],
449455
];
450456
}
457+
458+
/**
459+
* @requires PHP 7.0
460+
*/
461+
public function testAnonymousClassFixing()
462+
{
463+
$this->doTest(
464+
'<?php
465+
class MyTest extends \PHPUnit_Framework_TestCase
466+
{
467+
public function testBaseCase()
468+
{
469+
static::assertSame(1, 2);
470+
471+
$foo = new class() {
472+
public function assertSame($a, $b)
473+
{
474+
$this->assertSame(1, 2);
475+
}
476+
};
477+
}
478+
}',
479+
'<?php
480+
class MyTest extends \PHPUnit_Framework_TestCase
481+
{
482+
public function testBaseCase()
483+
{
484+
$this->assertSame(1, 2);
485+
486+
$foo = new class() {
487+
public function assertSame($a, $b)
488+
{
489+
$this->assertSame(1, 2);
490+
}
491+
};
492+
}
493+
}'
494+
);
495+
}
451496
}

0 commit comments

Comments
 (0)