-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Add support for willImplement()
1 parent
53ce192
commit 7da6080
Showing
9 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Extension/ObjectProphecyWillImplementDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters