Skip to content

Commit 5dca58c

Browse files
authored
Merge pull request #798 from werlos/over-eager-stringable
Fix over-eager stringable matching
2 parents a384ed8 + e83f6bd commit 5dca58c

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3450-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3451-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
"Tests\\": "tests"
3232
}
3333
},
34+
"config": {
35+
"allow-plugins": {
36+
"infection/extension-installer": false,
37+
"ergebnis/composer-normalize": false,
38+
"kubawerlos/composer-smaller-lock": false,
39+
"phpstan/extension-installer": false
40+
}
41+
},
3442
"scripts": {
3543
"analyse": [
3644
"@install-dev-tools",

src/Fixer/StringableInterfaceFixer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,22 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8686

8787
private function doesHaveToStringMethod(Tokens $tokens, int $classStartIndex, int $classEndIndex): bool
8888
{
89-
for ($index = $classStartIndex + 1; $index < $classEndIndex; $index++) {
90-
if (!$tokens[$index]->isGivenKind(\T_FUNCTION)) {
89+
$index = $classStartIndex;
90+
91+
while ($index < $classEndIndex) {
92+
$index++;
93+
94+
if ($tokens[$index]->equals('{')) {
95+
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
9196
continue;
9297
}
9398

94-
$functionNameIndex = $tokens->getNextTokenOfKind($index, [[\T_STRING]]);
95-
96-
if ($functionNameIndex === null || $functionNameIndex > $classEndIndex) {
97-
return false;
99+
if (!$tokens[$index]->isGivenKind(\T_FUNCTION)) {
100+
continue;
98101
}
99102

103+
$functionNameIndex = $tokens->getNextMeaningfulToken($index);
104+
100105
if ($tokens[$functionNameIndex]->equals([\T_STRING, '__toString'], false)) {
101106
return true;
102107
}

tests/Fixer/StringableInterfaceFixerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,17 @@ class Foo5 { public function __noString() { return "5"; } }
300300
;
301301
',
302302
];
303+
304+
yield ['<?php
305+
namespace Foo;
306+
use Stringable;
307+
class Bar {
308+
public function foo() {
309+
new class () implements Stringable {
310+
public function __toString() { return ""; }
311+
};
312+
}
313+
}
314+
'];
303315
}
304316
}

0 commit comments

Comments
 (0)