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\Factory\Reflection; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType; |
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType; |
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType; |
21
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
22
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
23
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\StringType; |
24
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType; |
25
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
26
|
|
|
use Spaark\CompositeUtils\Service\GenericNameProvider; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
*/ |
30
|
|
|
class GenericCompositeGenerator |
31
|
|
|
{ |
32
|
|
|
protected $reflect; |
33
|
|
|
|
34
|
|
|
protected $nameProvider; |
35
|
|
|
|
36
|
1 |
|
public function __construct(ReflectionComposite $reflect) |
37
|
|
|
{ |
38
|
1 |
|
$this->reflect = $reflect; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
private function createObject(...$generics) |
42
|
|
|
{ |
43
|
1 |
|
$object = new ObjectType(get_class($this->reflect), ''); |
|
|
|
|
44
|
1 |
|
$i = 0; |
45
|
|
|
|
46
|
1 |
|
foreach ($this->reflect->generics as $name => $value) |
|
|
|
|
47
|
|
|
{ |
48
|
1 |
|
$object->generics[] = $generics[$i++] ?? $value; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return $object; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function generateClassCode(...$generics) |
55
|
|
|
{ |
56
|
1 |
|
$this->nameProvider = new GenericNameProvider(); |
57
|
1 |
|
$object = $this->createObject(...$generics); |
58
|
1 |
|
$class = $this->nameProvider->inferName($object); |
59
|
|
|
|
60
|
1 |
|
$class = explode('\\', $class); |
61
|
1 |
|
$baseClass = $class[count($class) - 1]; |
62
|
1 |
|
unset($class[count($class) - 1]); |
63
|
1 |
|
$namespace = implode('\\', $class); |
64
|
1 |
|
$originalClass = get_class($this->reflect); |
65
|
1 |
|
$i = 0; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$code = |
68
|
1 |
|
'<?php namespace ' . $namespace . ';' |
69
|
1 |
|
. 'class ' . $baseClass . ' extends ' . $originalClass |
70
|
1 |
|
. '{'; |
71
|
|
|
|
72
|
1 |
|
foreach ($this->reflect->methods as $method) |
73
|
|
|
{ |
74
|
1 |
|
$code .= $this->generateMethodCode($method, $object); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$code .= '}'; |
78
|
|
|
|
79
|
1 |
|
return $code; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function generateMethodCode($method, ObjectType $object) |
83
|
|
|
{ |
84
|
1 |
|
$params = []; |
85
|
1 |
|
$newParams = []; |
86
|
1 |
|
$paramNames = []; |
87
|
1 |
|
foreach ($method->parameters as $i => $param) |
88
|
|
|
{ |
89
|
1 |
|
if (!$param->type instanceof GenericType) |
90
|
|
|
{ |
91
|
1 |
|
$type = $param->type; |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
1 |
|
$index = $this->reflect->generics->indexOfKey |
|
|
|
|
96
|
|
|
( |
97
|
1 |
|
$param->type->name |
|
|
|
|
98
|
|
|
); |
99
|
|
|
|
100
|
1 |
|
$type = $object->generics[$index]; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$paramNames[] = $name = '$' . $param->name; |
104
|
1 |
|
$params[] = $method->nativeParameters[$i] . ' ' . $name; |
105
|
1 |
|
$newParams[] = |
106
|
1 |
|
$this->nameProvider->inferName($type) . ' ' . $name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return |
110
|
1 |
|
($method->scope === 'static' ? 'static ' : '') |
111
|
1 |
|
. 'function ' . $method->name |
112
|
1 |
|
. '(' . implode(',', $params) . '){' |
113
|
1 |
|
. '__generic_' . $method->name |
114
|
1 |
|
. '(' . implode(',', $paramNames) . ');}' |
115
|
|
|
. "\n" |
116
|
1 |
|
. 'function __generic_' . $method->name |
117
|
1 |
|
. '(' . implode(',', $newParams) . '){' |
118
|
1 |
|
. 'parent::' . $method->name |
119
|
1 |
|
. '(' . implode(',', $paramNames) . ');}' |
120
|
1 |
|
. "\n"; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.