Skip to content

Commit

Permalink
Enhancement: Add support for willImplement()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 27, 2019
1 parent 53ce192 commit 045edf4
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/Extension/ObjectProphecyRevealDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return $parametersAcceptor->getReturnType();
}

return TypeCombinator::intersect(
new ObjectType($calledOnType->getProphesizedClass()),
$parametersAcceptor->getReturnType()
);
$types = \array_map(static function (string $class): ObjectType {
return new ObjectType($class);
}, $calledOnType->getProphesizedClasses());

$types[] = $parametersAcceptor->getReturnType();

return TypeCombinator::intersect(...$types);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace JanGregor\Prophecy\Extension;

use JanGregor\Prophecy\Type\ObjectProphecyType;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;

class ObjectProphecyWillImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return 'Prophecy\Prophecy\ObjectProphecy';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return 'willImplement' === $methodReflection->getName();
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$calledOnType = $scope->getType($methodCall->var);

$prophecyType = $parametersAcceptor->getReturnType();

if (!$calledOnType instanceof ObjectProphecyType) {
return $prophecyType;
}

if (0 === \count($methodCall->args)) {
return $prophecyType;
}

$argType = $scope->getType($methodCall->args[0]->value);

if (!($argType instanceof ConstantStringType)) {
return $prophecyType;
}

$class = $argType->getValue();

if (!($prophecyType instanceof TypeWithClassName)) {
throw new ShouldNotHappenException();
}

if ('static' === $class) {
return $prophecyType;
}

if ('self' === $class && null !== $scope->getClassReflection()) {
$class = $scope->getClassReflection()->getName();
}

$calledOnType->addProphesizedClass($class);

return $calledOnType;
}
}
31 changes: 24 additions & 7 deletions src/Type/ObjectProphecyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,46 @@
class ObjectProphecyType extends ObjectType
{
/**
* @var string
* @var string[]
*/
protected $prophesizedClass;
protected $prophesizedClasses;

public function __construct(string $prophesizedClass)
{
$this->prophesizedClass = $prophesizedClass;
$this->prophesizedClasses = [
$prophesizedClass,
];

parent::__construct('Prophecy\Prophecy\ObjectProphecy');
}

public static function __set_state(array $properties): Type
{
return new self($properties['prophesizedClass']);
return new self($properties['prophesizedClasses']);
}

public function describe(VerbosityLevel $level): string
{
return \sprintf('%s<%s>', parent::describe($level), $this->prophesizedClass);
return \sprintf(
'%s<%s>',
parent::describe($level),
\implode(
'&',
$this->prophesizedClasses
)
);
}

public function getProphesizedClass(): string
public function addProphesizedClass(string $prophesizedClass): void
{
return $this->prophesizedClass;
$this->prophesizedClasses[] = $prophesizedClass;
}

/**
* @return string[]
*/
public function getProphesizedClasses(): array
{
return $this->prophesizedClasses;
}
}
5 changes: 5 additions & 0 deletions src/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ services:
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: JanGregor\Prophecy\Extension\ObjectProphecyWillImplementDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: JanGregor\Prophecy\Reflection\ProphecyMethodsClassReflectionExtension
tags:
Expand Down
10 changes: 10 additions & 0 deletions tests/Model/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace JanGregor\Prophecy\Test\Model;

interface Bar
{
public function bar(): string;
}
5 changes: 5 additions & 0 deletions tests/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function doubleTheNumber(int $number)
{
return 2 * $number;
}

public function bar(Bar $bar): string
{
return $bar->bar();
}
}
10 changes: 10 additions & 0 deletions tests/Model/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace JanGregor\Prophecy\Test\Model;

interface Foo
{
public function foo(): string;
}
18 changes: 18 additions & 0 deletions tests/Test/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace JanGregor\Prophecy\Test\Test;

use JanGregor\Prophecy\Test\Model\Bar;
use JanGregor\Prophecy\Test\Model\BaseModel;
use JanGregor\Prophecy\Test\Model\Foo;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -59,4 +61,20 @@ public function testProphesizedAttributesShouldAlsoWork(): void
self::assertEquals('bar', $subject->getFoo());
self::assertEquals(5, $subject->doubleTheNumber(2));
}

public function testWillImplementWorks(): void
{
$fooThatAlsoBars = $this->prophesize(Foo::class);

$fooThatAlsoBars->willImplement(Bar::class);

$fooThatAlsoBars
->bar()
->shouldBeCalled()
->willReturn('Oh');

$subject = new BaseModel();

self::assertSame('Oh', $subject->bar($fooThatAlsoBars->reveal()));
}
}

0 comments on commit 045edf4

Please sign in to comment.