|
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\Exception\PropertyNotWritableException; |
|
26
|
|
|
use Spaark\CompositeUtils\Exception\PropertyNotReadableException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Used to retrieve the classname for an AbstractType |
|
30
|
|
|
*/ |
|
31
|
|
|
class GenericNameProvider |
|
32
|
|
|
{ |
|
33
|
|
|
const BASE = 'Spaark\CompositeUtils\Generic\\'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Infers the serialized name of the given AbstractType |
|
37
|
|
|
* |
|
38
|
|
|
* @param AbstractType $reflect |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function inferName(AbstractType $reflect) |
|
42
|
|
|
{ |
|
43
|
1 |
|
switch (get_class($reflect)) |
|
44
|
|
|
{ |
|
45
|
1 |
|
case ObjectType::class: |
|
46
|
1 |
|
return $this->inferObjectName($reflect); |
|
|
|
|
|
|
47
|
1 |
|
case BooleanType::class: |
|
48
|
|
|
return 'boolean'; |
|
49
|
1 |
|
case IntegerType::class: |
|
50
|
1 |
|
return 'int'; |
|
51
|
1 |
|
case FloatType::class: |
|
52
|
|
|
return 'float'; |
|
53
|
1 |
|
case MixedType::class: |
|
54
|
|
|
return 'mixed'; |
|
55
|
1 |
|
case StringType::class: |
|
56
|
1 |
|
return 'string'; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Infers the serialized name of the given ObjectType |
|
62
|
|
|
* |
|
63
|
|
|
* @param ObjectType $reflect |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
1 |
|
protected function inferObjectName(ObjectType $reflect) |
|
67
|
|
|
{ |
|
68
|
1 |
|
if ($reflect->generics->empty()) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
1 |
|
return $reflect->classname; |
|
71
|
|
|
} |
|
72
|
|
|
else |
|
73
|
|
|
{ |
|
74
|
1 |
|
$items = []; |
|
75
|
1 |
|
foreach ($reflect->generics as $generic) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
1 |
|
$items[] = $this->inferName($generic); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return self::BASE . $reflect->classname |
|
81
|
1 |
|
. '_g' . implode('_c', $items) . '_e'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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.