Skip to content

Commit 7a40a41

Browse files
author
Frank Kleine
committed
fix bug with return type hint self
Callmap instance creation for interfaces and classes using return type hint self lead to a fatal error because code creation simply added ": \self" as return type hint which is invalid. It will now replace this with the type hint for the class which declares the method.
1 parent 1d55456 commit 7a40a41

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
3.0.2 (2016-07-21)
2+
------------------
3+
4+
* fixed bug that return type hint `self` was not used correctly and lead to a fatal error when creating callmap instances of interfaces or classes using such a return type hint
5+
6+
17
3.0.1 (2016-07-11)
28
------------------
39

src/main/php/NewInstance.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ private static function determineReturnType(\ReflectionMethod $method): string
271271
return ': ' . $returnType;
272272
}
273273

274+
if ('self' == $returnType) {
275+
return ': \\' . $method->getDeclaringClass()->getName();
276+
}
277+
274278
return ': \\' . $returnType;
275279
}
276280

src/test/php/ReturnSelfTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace bovigo\callmap;
1212
use function bovigo\assert\assert;
13+
use function bovigo\assert\predicate\isInstanceOf;
1314
use function bovigo\assert\predicate\isSameAs;
1415
use function bovigo\assert\predicate\isNull;
1516
/**
@@ -133,6 +134,22 @@ interface Fump
133134
*/
134135
public function noReturn();
135136
}
137+
/**
138+
* @since 3.0.2
139+
*/
140+
interface WithSelfReturnTypeHint
141+
{
142+
public function wow(): self;
143+
}
144+
/**
145+
* @since 3.0.2
146+
*/
147+
class Really implements WithSelfReturnTypeHint
148+
{
149+
public function wow(): WithSelfReturnTypeHint { return $this; }
150+
151+
public function hui(): self { return $this; }
152+
}
136153
/**
137154
* Tests for automated return self.
138155
*
@@ -258,4 +275,43 @@ public function doesNotReturnSelfWhenNoReturnTypeHintInDocComment()
258275
{
259276
assert(NewInstance::of(Fump::class)->noReturn(), isNull());
260277
}
278+
279+
/**
280+
* @test
281+
* @since 3.0.2
282+
* @group self_type_hint
283+
*/
284+
public function canWorkWithSelfReturnTypeHintForInterfaceDirectly()
285+
{
286+
assert(
287+
NewInstance::of(WithSelfReturnTypeHint::class)->wow(),
288+
isInstanceOf(WithSelfReturnTypeHint::class)
289+
);
290+
}
291+
292+
/**
293+
* @test
294+
* @since 3.0.2
295+
* @group self_type_hint
296+
*/
297+
public function canWorkWithSelfReturnTypeHintForImplementingClass()
298+
{
299+
assert(
300+
NewInstance::stub(Really::class)->wow(),
301+
isInstanceOf(WithSelfReturnTypeHint::class)
302+
);
303+
}
304+
305+
/**
306+
* @test
307+
* @since 3.0.2
308+
* @group self_type_hint
309+
*/
310+
public function canWorkWithSelfReturnTypeHintForClassDirectly()
311+
{
312+
assert(
313+
NewInstance::stub(Really::class)->hui(),
314+
isInstanceOf(Really::class)
315+
);
316+
}
261317
}

0 commit comments

Comments
 (0)