|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Stratadox\PhpGenerics\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
6
|
|
|
use PhpParser\NodeVisitor; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Stratadox\PhpGenerics\Generator\Type\TypeArguments; |
|
9
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\ClassNameReplacement; |
|
10
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\DocCommentReplacement; |
|
11
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\ImportsReplacement; |
|
12
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\InstanceOfReplacement; |
|
13
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\NamespaceReplacement; |
|
14
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\ParentBypass; |
|
15
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\ReturnTypeEnforcement; |
|
16
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\TypeHintReplacement; |
|
17
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\TypeMapAssembly; |
|
18
|
|
|
use Stratadox\PhpGenerics\Generator\Type\TypeMap; |
|
19
|
|
|
use Stratadox\PhpGenerics\Generator\Visitor\TypeNameReplacement; |
|
20
|
|
|
|
|
21
|
|
|
final class StandardVisitorFactory implements VisitorFactory |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Reader */ |
|
24
|
|
|
private $annotationReader; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Reader $annotationReader) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->annotationReader = $annotationReader; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** @return NodeVisitor[] */ |
|
32
|
|
|
public function replacing( |
|
33
|
|
|
string $namespace, |
|
34
|
|
|
string $newClassName, |
|
35
|
|
|
ReflectionClass $genericBase, |
|
36
|
|
|
TypeArguments $typeArguments |
|
37
|
|
|
): array { |
|
38
|
|
|
$typeMap = new TypeMap(); |
|
39
|
|
|
return [ |
|
40
|
|
|
new NamespaceReplacement($namespace), |
|
41
|
|
|
new ImportsReplacement( |
|
42
|
|
|
$genericBase->getName(), |
|
43
|
|
|
$typeArguments |
|
44
|
|
|
), |
|
45
|
|
|
new ClassNameReplacement($newClassName), |
|
46
|
|
|
new TypeMapAssembly( |
|
47
|
|
|
$typeMap, |
|
48
|
|
|
$typeArguments, |
|
49
|
|
|
'Stratadox\\PhpGenerics\\Generic\\' |
|
50
|
|
|
), |
|
51
|
|
|
new TypeHintReplacement($typeMap), |
|
52
|
|
|
new ReturnTypeEnforcement( |
|
53
|
|
|
$typeMap, |
|
54
|
|
|
$this->annotationReader, |
|
55
|
|
|
$genericBase |
|
56
|
|
|
), |
|
57
|
|
|
new DocCommentReplacement( |
|
58
|
|
|
$typeMap, |
|
59
|
|
|
'@param %s ', |
|
60
|
|
|
'@return %s' |
|
61
|
|
|
), |
|
62
|
|
|
new InstanceOfReplacement($typeMap), |
|
63
|
|
|
new ParentBypass($genericBase), |
|
64
|
|
|
new TypeNameReplacement($typeMap), |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|