Skip to content

[WIP] Support for Prophecy #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ services:
class: PHPStan\Type\PHPUnit\MockBuilderDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Prophecy\ProphetDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Prophecy\ObjectProphecyDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
38 changes: 38 additions & 0 deletions src/Type/Prophecy/ObjectProphecyDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Prophecy;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Prophecy\Prophecy\ObjectProphecy;

class ObjectProphecyDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return ObjectProphecy::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return true;
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if ($methodReflection->getName() === 'reveal') {
$calledOnType = $scope->getType($methodCall->var);

if ($calledOnType instanceof ObjectProphecyType) {
return new ObjectType($calledOnType->getMockedClass());
}
}

return $methodReflection->getReturnType();
}

}
31 changes: 31 additions & 0 deletions src/Type/Prophecy/ObjectProphecyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Prophecy;

use Prophecy\Prophecy\ObjectProphecy;

class ObjectProphecyType extends \PHPStan\Type\ObjectType
{

/**
* @var string
*/
private $mockedClass;

public function __construct(string $mockedClass)
{
parent::__construct(ObjectProphecy::class);
$this->mockedClass = $mockedClass;
}

public function getMockedClass(): string
{
return $this->mockedClass;
}

public function describe(): string
{
return sprintf('%s<%s>', parent::describe(), $this->mockedClass);
}

}
51 changes: 51 additions & 0 deletions src/Type/Prophecy/ProphetDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Prophecy;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Type;
use PHPUnit\Framework\TestCase;

class ProphetDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return TestCase::class;
}

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

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (count($methodCall->args) === 0) {
return $methodReflection->getReturnType();
}
$arg = $methodCall->args[0]->value;
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
return $methodReflection->getReturnType();
}

$class = $arg->class;
if (!($class instanceof \PhpParser\Node\Name)) {
return $methodReflection->getReturnType();
}

$class = (string) $class;
if ($class === 'static') {
return $methodReflection->getReturnType();
}

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

return new ObjectProphecyType($class);
}

}