Skip to content

Commit 679d21f

Browse files
authored
StringableInterfaceFixer - add Stringable at the beginning of interfaces list to avoid "Class cannot implement previously implemented interface" message (#926)
1 parent 9ed385d commit 679d21f

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/Fixer/StringableInterfaceFixer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,16 @@ private function addStringableInterface(Tokens $tokens, int $classIndex): void
216216
return;
217217
}
218218

219-
$implementsEndIndex = $tokens->getNextTokenOfKind($implementsIndex, ['{']);
220-
\assert(\is_int($implementsEndIndex));
221-
222-
$prevIndex = $tokens->getPrevMeaningfulToken($implementsEndIndex);
223-
\assert(\is_int($prevIndex));
219+
$afterImplementsIndex = $tokens->getNextMeaningfulToken($implementsIndex);
220+
\assert(\is_int($afterImplementsIndex));
224221

225222
$tokens->insertAt(
226-
$prevIndex + 1,
223+
$afterImplementsIndex,
227224
[
228-
new Token(','),
229-
new Token([\T_WHITESPACE, ' ']),
230225
new Token([\T_NS_SEPARATOR, '\\']),
231226
new Token([\T_STRING, 'Stringable']),
227+
new Token(','),
228+
new Token([\T_WHITESPACE, ' ']),
232229
],
233230
);
234231
}

tests/Fixer/StringableInterfaceFixerTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __toString() { return "Foo"; }
130130
yield [
131131
'<?php namespace FooNamespace;
132132
use Bar\Stringable;
133-
class Foo implements Stringable, \Stringable
133+
class Foo implements \Stringable, Stringable
134134
{
135135
public function __toString() { return "Foo"; }
136136
}
@@ -167,13 +167,13 @@ public function __toString() { return "Foo"; }
167167

168168
foreach ($implementedInterfacesCases as $implementedInterface) {
169169
yield [
170-
\sprintf($template, $implementedInterface . ', \Stringable'),
170+
\sprintf($template, '\Stringable, ' . $implementedInterface),
171171
\sprintf($template, $implementedInterface),
172172
];
173173
}
174174

175175
yield [
176-
'<?php class Foo implements FooInterface, \Stringable
176+
'<?php class Foo implements \Stringable, FooInterface
177177
{
178178
public function __toString() { return "Foo"; }
179179
}
@@ -215,7 +215,7 @@ public function __TOSTRING() { return "Foo"; }
215215
'<?php
216216
namespace Foo;
217217
use Bar;
218-
class Baz implements Stringable, \Stringable {
218+
class Baz implements \Stringable, Stringable {
219219
public function __toString() { return ""; }
220220
}
221221
',
@@ -273,23 +273,25 @@ class Foo5 { public function __noString() { return "5"; } }
273273

274274
yield [
275275
'<?php
276-
namespace Foo { class C implements I, \Stringable { public function __toString() { return ""; } }}
276+
namespace Foo { class C implements \Stringable, I { public function __toString() { return ""; } }}
277277
namespace Bar { class C implements \Stringable, I { public function __toString() { return ""; } }}
278278
namespace Baz { class C implements I, \Stringable { public function __toString() { return ""; } }}
279+
namespace Qux { class C implements \Stringable, I { public function __toString() { return ""; } }}
279280
;
280281
',
281282
'<?php
282283
namespace Foo { class C implements I { public function __toString() { return ""; } }}
283284
namespace Bar { class C implements \Stringable, I { public function __toString() { return ""; } }}
284-
namespace Baz { class C implements I { public function __toString() { return ""; } }}
285+
namespace Baz { class C implements I, \Stringable { public function __toString() { return ""; } }}
286+
namespace Qux { class C implements I { public function __toString() { return ""; } }}
285287
;
286288
',
287289
];
288290

289291
yield [
290292
'<?php
291293
namespace Foo { use Stringable as Stringy; class C {} }
292-
namespace Bar { class C implements Stringy, \Stringable { public function __toString() { return ""; } }}
294+
namespace Bar { class C implements \Stringable, Stringy { public function __toString() { return ""; } }}
293295
;
294296
',
295297
'<?php

tests/priority_fixtures/Custom_stringable_interface,class_definition.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{ "PhpCsFixerCustomFixers/stringable_interface": true, "class_definition": true }
33
--EXPECTED--
44
<?php class Foo implements
5+
\Stringable,
56
Bar,
6-
Baz,
7-
\Stringable
7+
Baz
88
{
99
public function __toString() { return "Foo"; }
1010
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
--CONFIGURATION--
22
{ "PhpCsFixerCustomFixers/stringable_interface": true, "ordered_interfaces": true }
33
--EXPECTED--
4-
<?php class Foo implements \Stringable, Zzz
4+
<?php class Foo implements \Bar, \Stringable
55
{
66
public function __toString() { return "Foo"; }
77
}
88

99
--INPUT--
10-
<?php class Foo implements Zzz
10+
<?php class Foo implements \Bar
1111
{
1212
public function __toString() { return "Foo"; }
1313
}

0 commit comments

Comments
 (0)