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
|
2 |
|
public function inferName(AbstractType $reflect) |
42
|
|
|
{ |
43
|
2 |
|
switch (get_class($reflect)) |
44
|
|
|
{ |
45
|
2 |
|
case ObjectType::class: |
46
|
2 |
|
return $this->inferObjectName($reflect); |
|
|
|
|
47
|
2 |
|
case BooleanType::class: |
48
|
|
|
return 'boolean'; |
49
|
2 |
|
case IntegerType::class: |
50
|
1 |
|
return 'int'; |
51
|
2 |
|
case FloatType::class: |
52
|
|
|
return 'float'; |
53
|
2 |
|
case MixedType::class: |
54
|
|
|
return ''; |
55
|
2 |
|
case StringType::class: |
56
|
2 |
|
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
|
2 |
|
protected function inferObjectName(ObjectType $reflect) |
67
|
|
|
{ |
68
|
2 |
|
if ($reflect->generics->empty()) |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
return $reflect->classname; |
71
|
|
|
} |
72
|
|
|
else |
73
|
|
|
{ |
74
|
2 |
|
$items = []; |
75
|
2 |
|
foreach ($reflect->generics as $generic) |
|
|
|
|
76
|
|
|
{ |
77
|
2 |
|
$items[] = $this->inferName($generic); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
return self::BASE . $reflect->classname |
81
|
2 |
|
. '_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.