Skip to content

Commit 04dd44e

Browse files
committed
Add support for interfaces
1 parent dca997c commit 04dd44e

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\Proget\Tests\PHPStan\PhpSpec;
6+
7+
use Proget\Tests\PHPStan\PhpSpec\Baz;
8+
use Proget\Tests\PHPStan\PhpSpec\Foo;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class FooSpec extends ObjectBehavior
12+
{
13+
public function let(Baz $baz): void
14+
{
15+
$this->beConstructedWith($baz);
16+
}
17+
18+
public function it_is_initializable(): void
19+
{
20+
$this->shouldHaveType(Foo::class);
21+
}
22+
23+
public function it_should_make_baz(Baz $baz): void
24+
{
25+
$baz->make()->willReturn(123);
26+
27+
$this->makeBaz()->shouldBe(123);
28+
}
29+
}

src/DependencyInjection/PhpSpecDynamicMethodReturnTypeExtensionCreator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Nette\DI\CompilerExtension;
88
use Nette\DI\ServiceDefinition;
9-
use Proget\PHPStan\PhpSpec\Locator\NonFinalClassLocator;
9+
use Proget\PHPStan\PhpSpec\Locator\SourceClassLocator;
1010
use Proget\PHPStan\PhpSpec\Reflection\CollaboratorDynamicMethodReturnTypeExtension;
1111

1212
final class PhpSpecDynamicMethodReturnTypeExtensionCreator extends CompilerExtension
@@ -15,7 +15,7 @@ public function beforeCompile()
1515
{
1616
$builder = $this->getContainerBuilder();
1717
$workingDir = $this->getContainerBuilder()->parameters['currentWorkingDirectory'];
18-
$classes = (new NonFinalClassLocator())->locate(array_map(function (string $dir) use ($workingDir) {
18+
$classes = (new SourceClassLocator())->locate(array_map(function (string $dir) use ($workingDir) {
1919
return $workingDir.DIRECTORY_SEPARATOR.ltrim($dir, DIRECTORY_SEPARATOR);
2020
}, $this->getContainerBuilder()->parameters['phpspecSourceFiles']));
2121

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Symfony\Component\Finder\Finder;
88
use Symfony\Component\Finder\SplFileInfo;
99

10-
final class NonFinalClassLocator
10+
final class SourceClassLocator
1111
{
1212
public function locate(array $dirs): array
1313
{
@@ -44,7 +44,7 @@ private function getClassName(SplFileInfo $fileInfo): ?string
4444
}
4545
}
4646

47-
if ($tokens[$i][0] === T_CLASS) {
47+
if ($tokens[$i][0] === T_CLASS || $tokens[$i][0] === T_INTERFACE) {
4848
for ($j = $i + 1; $j < $count; ++$j) {
4949
if ($tokens[$j] === '{') {
5050
return $namespace.$tokens[$i + 2][1];

tests/Baz.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proget\Tests\PHPStan\PhpSpec;
6+
7+
interface Baz
8+
{
9+
public function make(): int;
10+
}

tests/Foo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@
66

77
class Foo
88
{
9+
/**
10+
* @var Baz
11+
*/
12+
private $baz;
13+
14+
public function __construct(Baz $baz)
15+
{
16+
$this->baz = $baz;
17+
}
18+
919
public function foo(): string
1020
{
1121
return '';
1222
}
23+
24+
public function makeBaz(): int
25+
{
26+
return $this->baz->make();
27+
}
1328
}

0 commit comments

Comments
 (0)