|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
|
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType; |
|
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType; |
|
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\FloatType; |
|
21
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
|
22
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType; |
|
23
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType; |
|
24
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\StringType; |
|
25
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType; |
|
26
|
|
|
use Spaark\CompositeUtils\Model\Generic\GenericContext; |
|
27
|
|
|
use Spaark\CompositeUtils\Exception\MissingContextException; |
|
28
|
|
|
use Spaark\CompositeUtils\Traits\AutoConstructTrait; |
|
29
|
|
|
use Spaark\CompositeUtils\Model\ClassName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Used to retrieve the classname for an AbstractType |
|
33
|
|
|
*/ |
|
34
|
|
|
class GenericNameProvider |
|
35
|
|
|
{ |
|
36
|
|
|
use AutoConstructTrait; |
|
37
|
|
|
|
|
38
|
|
|
const BASE = 'Spaark\CompositeUtils\Generic\\'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var GenericContext |
|
42
|
|
|
* @construct optional |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $context; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Infers the serialized name of the given AbstractType |
|
48
|
|
|
* |
|
49
|
|
|
* @param AbstractType $reflect |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
10 |
|
public function inferName(AbstractType $reflect) |
|
53
|
|
|
{ |
|
54
|
10 |
|
switch (get_class($reflect)) |
|
55
|
|
|
{ |
|
56
|
10 |
|
case ObjectType::class: |
|
57
|
3 |
|
return $this->inferObjectName($reflect); |
|
|
|
|
|
|
58
|
10 |
|
case BooleanType::class: |
|
59
|
1 |
|
return 'bool'; |
|
60
|
9 |
|
case IntegerType::class: |
|
61
|
2 |
|
return 'int'; |
|
62
|
8 |
|
case FloatType::class: |
|
63
|
1 |
|
return 'float'; |
|
64
|
7 |
|
case MixedType::class: |
|
65
|
1 |
|
return ''; |
|
66
|
6 |
|
case StringType::class: |
|
67
|
3 |
|
return 'string'; |
|
68
|
4 |
|
case GenericType::class: |
|
69
|
3 |
|
return $this->inferGenericName($reflect); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
throw new \DomainException |
|
73
|
|
|
( |
|
74
|
1 |
|
'Unknown type: ' . get_class($reflect) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Infers the serialized name of the given GenericType |
|
80
|
|
|
* |
|
81
|
|
|
* @param GenericType $reflect |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
3 |
|
protected function inferGenericName(GenericType $reflect) |
|
86
|
|
|
{ |
|
87
|
3 |
|
if (!$this->context) |
|
88
|
|
|
{ |
|
89
|
1 |
|
throw new MissingContextException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
return $this->inferName |
|
93
|
|
|
( |
|
94
|
2 |
|
$this->context->getGenericType($reflect->name) |
|
|
|
|
|
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Infers the serialized name of the given ObjectType |
|
100
|
|
|
* |
|
101
|
|
|
* @param ObjectType $reflect |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
3 |
|
protected function inferObjectName(ObjectType $reflect) |
|
105
|
|
|
{ |
|
106
|
3 |
|
if ($reflect->generics->empty()) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
3 |
|
return $reflect->classname; |
|
109
|
|
|
} |
|
110
|
|
|
else |
|
111
|
|
|
{ |
|
112
|
2 |
|
$items = []; |
|
113
|
2 |
|
foreach ($reflect->generics as $generic) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
2 |
|
$items[] = $this->inferName($generic); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return new ClassName |
|
119
|
|
|
( |
|
120
|
2 |
|
self::BASE . $reflect->classname |
|
121
|
2 |
|
. '_g' . implode('_c', $items) . '_e' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.