Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b20ecd
commit 7b23c31
Showing
12 changed files
with
181 additions
and
24 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
54 changes: 54 additions & 0 deletions
54
src/PhpDoc/PhpUnit/MockObjectTypeNodeResolverExtension.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,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc\PhpUnit; | ||
|
||
use PHPStan\PhpDoc\TypeNodeResolver; | ||
use PHPStan\PhpDoc\TypeNodeResolverAwareExtension; | ||
use PHPStan\PhpDoc\TypeNodeResolverExtension; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeWithClassName; | ||
|
||
class MockObjectTypeNodeResolverExtension implements TypeNodeResolverExtension, TypeNodeResolverAwareExtension | ||
{ | ||
|
||
/** @var TypeNodeResolver */ | ||
private $typeNodeResolver; | ||
|
||
public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void | ||
{ | ||
$this->typeNodeResolver = $typeNodeResolver; | ||
} | ||
|
||
public function getCacheKey(): string | ||
{ | ||
return 'phpunit-v1'; | ||
} | ||
|
||
public function resolve(TypeNode $typeNode, \PHPStan\Analyser\NameScope $nameScope): ?Type | ||
{ | ||
if (!$typeNode instanceof UnionTypeNode) { | ||
return null; | ||
} | ||
|
||
static $mockClassNames = [ | ||
'PHPUnit_Framework_MockObject_MockObject' => true, | ||
'PHPUnit\Framework\MockObject\MockObject' => true, | ||
]; | ||
|
||
$types = $this->typeNodeResolver->resolveMultiple($typeNode->types, $nameScope); | ||
foreach ($types as $type) { | ||
if (!$type instanceof TypeWithClassName) { | ||
continue; | ||
} | ||
|
||
if (array_key_exists($type->getClassName(), $mockClassNames)) { | ||
return \PHPStan\Type\TypeCombinator::intersect(...$types); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc; | ||
|
||
interface TypeNodeResolverAwareExtension | ||
{ | ||
|
||
public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void; | ||
|
||
} |
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,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc; | ||
|
||
use PHPStan\Analyser\NameScope; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use PHPStan\Type\Type; | ||
|
||
interface TypeNodeResolverExtension | ||
{ | ||
|
||
public function getCacheKey(): string; | ||
|
||
public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type; | ||
|
||
} |
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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc; | ||
|
||
use Nette\DI\Container; | ||
|
||
class TypeNodeResolverFactory | ||
{ | ||
|
||
public const EXTENSION_TAG = 'phpstan.phpDoc.typeNodeResolverExtension'; | ||
|
||
/** @var \Nette\DI\Container */ | ||
private $container; | ||
|
||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function create(): TypeNodeResolver | ||
{ | ||
$tagToService = function (array $tags) { | ||
return array_map(function (string $serviceName) { | ||
return $this->container->getService($serviceName); | ||
}, array_keys($tags)); | ||
}; | ||
|
||
return new TypeNodeResolver( | ||
$tagToService($this->container->findByTag(self::EXTENSION_TAG)) | ||
); | ||
} | ||
|
||
} |
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
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