-
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.
Allow using prophecy in test attributes as well
- Loading branch information
1 parent
9177e63
commit 3953120
Showing
5 changed files
with
137 additions
and
43 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,61 @@ | ||
<?php | ||
|
||
namespace JanGregor\Prophecy\PhpDoc; | ||
|
||
use JanGregor\Prophecy\Type\ObjectProphecyType; | ||
use PHPStan\Analyser\NameScope; | ||
use PHPStan\PhpDoc\TypeNodeResolver; | ||
use PHPStan\PhpDoc\TypeNodeResolverAwareExtension; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
class TypeNodeResolverExtension implements \PHPStan\PhpDoc\TypeNodeResolverExtension, TypeNodeResolverAwareExtension | ||
{ | ||
|
||
/** @var TypeNodeResolver */ | ||
private $typeNodeResolver; | ||
|
||
public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void | ||
{ | ||
$this->typeNodeResolver = $typeNodeResolver; | ||
} | ||
|
||
public function getCacheKey(): string | ||
{ | ||
return 'prophecy-v1'; | ||
} | ||
|
||
public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type | ||
{ | ||
if (!$typeNode instanceof UnionTypeNode) { | ||
return null; | ||
} | ||
|
||
if (count($typeNode->types) === 2) { | ||
$objectProphecyType = null; | ||
$prophesizedType = null; | ||
foreach ($typeNode->types as $innerType) { | ||
$type = $this->typeNodeResolver->resolve($innerType, $nameScope); | ||
if ($type instanceof TypeWithClassName) { | ||
if ($type->getClassName() === ObjectProphecy::class) { | ||
$objectProphecyType = $type; | ||
} else { | ||
$prophesizedType = $type; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if ($objectProphecyType !== null && $prophesizedType !== null) { | ||
return new ObjectProphecyType($prophesizedType->getClassName()); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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