1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ValueObject; |
4
|
|
|
|
5
|
|
|
use ReflectionClass as BaseReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
|
8
|
|
|
class ValueObjectDefinition extends BaseReflectionClass |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
protected $uses = []; |
12
|
|
|
|
13
|
|
|
/** @var \Spatie\ValueObject\ValueObject */ |
14
|
|
|
protected $valueObject; |
15
|
|
|
|
16
|
|
|
public function __construct(ValueObject $valueObject) |
17
|
|
|
{ |
18
|
|
|
parent::__construct($valueObject); |
19
|
|
|
|
20
|
|
|
$this->valueObject = $valueObject; |
21
|
|
|
|
22
|
|
|
$this->resolveUseStatements(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function resolveUseStatements() |
26
|
|
|
{ |
27
|
|
|
$handle = fopen($this->getFileName(), 'r'); |
28
|
|
|
|
29
|
|
|
while ($line = fgets($handle)) { |
30
|
|
|
$line = trim($line); |
31
|
|
|
|
32
|
|
|
if (strpos($line, 'use ') !== 0) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$fqcn = str_replace(['use ', ';'], '', $line); |
37
|
|
|
|
38
|
|
|
$classParts = explode('\\', $fqcn); |
39
|
|
|
|
40
|
|
|
$alias = end($classParts); |
41
|
|
|
|
42
|
|
|
$this->uses[$alias] = $fqcn; |
43
|
|
|
|
44
|
|
|
if (strpos($line, 'class') !== false) { |
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
fclose($handle); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function hasAlias(string $alias): bool |
53
|
|
|
{ |
54
|
|
|
return isset($this->uses[$alias]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function resolveAlias(string $alias): string |
58
|
|
|
{ |
59
|
|
|
return $this->uses[$alias]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \Spatie\ValueObject\ValueObjectProperty[] |
64
|
|
|
*/ |
65
|
|
|
public function getValueObjectProperties(): array |
66
|
|
|
{ |
67
|
|
|
$properties = []; |
68
|
|
|
|
69
|
|
|
foreach (parent::getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
|
|
|
|
70
|
|
|
$properties[$reflectionProperty->getName()] = ValueObjectProperty::fromReflection( |
71
|
|
|
$this->valueObject, |
72
|
|
|
$this, |
73
|
|
|
$reflectionProperty |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $properties; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.