1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Interfaces; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\AbstractCreator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\FindReplaceProcess; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\AbstractFieldCreator; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits\FieldTraitCreator; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
22
|
|
|
*/ |
23
|
|
|
class FieldInterfaceCreator extends AbstractFieldCreator |
24
|
|
|
{ |
25
|
|
|
public const TEMPLATE_PATH = self::ROOT_TEMPLATE_PATH . |
26
|
|
|
'/src/Entity/Fields/Interfaces/' . |
27
|
|
|
self::FIND_NAME . 'FieldInterface.php'; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public const SUFFIX = FieldTraitCreator::INTERFACE_SUFFIX; |
31
|
|
|
/** |
32
|
|
|
* @var TypeHelper |
33
|
|
|
*/ |
34
|
|
|
private $typeHelper; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
FileFactory $fileFactory, |
38
|
|
|
NamespaceHelper $namespaceHelper, |
39
|
|
|
Writer $fileWriter, |
40
|
|
|
Config $config, |
41
|
|
|
FindReplaceFactory $findReplaceFactory, |
42
|
|
|
CodeHelper $codeHelper, |
43
|
|
|
TypeHelper $typeHelper |
44
|
|
|
) { |
45
|
|
|
parent::__construct($fileFactory, $namespaceHelper, $fileWriter, $config, $findReplaceFactory, $codeHelper); |
46
|
|
|
$this->typeHelper = $typeHelper; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function setNewObjectFqn(string $newObjectFqn): AbstractCreator |
51
|
|
|
{ |
52
|
|
|
$this->validateFqnEndsWithSuffix($newObjectFqn); |
53
|
|
|
|
54
|
|
|
return parent::setNewObjectFqn($newObjectFqn); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function validateFqnEndsWithSuffix(string $newObjectFqn): void |
58
|
|
|
{ |
59
|
|
|
if (substr($newObjectFqn, 0 - strlen(self::SUFFIX)) === self::SUFFIX) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
throw new InvalidArgumentException('$newObjectFqn must end in ' . self::SUFFIX); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
protected function configurePipeline(): void |
67
|
|
|
{ |
68
|
|
|
parent::configurePipeline(); |
69
|
|
|
$this->registerReplaceDefaultValue(); |
70
|
|
|
$this->registerReplacePropertyName(); |
71
|
|
|
$this->registerReplaceType(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function registerReplaceDefaultValue(): void |
75
|
|
|
{ |
76
|
|
|
$find = "'defaultValue'"; |
77
|
|
|
$replace = $this->getReplaceForDefaultValue(); |
78
|
|
|
$process = new FindReplaceProcess($find, $replace); |
79
|
|
|
$this->pipeline->register($process); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
private function getReplaceForDefaultValue(): string |
87
|
|
|
{ |
88
|
|
|
$defaultType = $this->typeHelper->getType($this->defaultValue); |
89
|
|
|
switch (true) { |
90
|
|
|
case $defaultType === 'null': |
91
|
|
|
return 'null'; |
92
|
|
|
case $this->phpType === 'string': |
93
|
|
|
return "'$this->defaultValue'"; |
94
|
|
|
case $this->phpType === 'bool': |
95
|
|
|
return (true === $this->defaultValue) ? 'true' : 'false'; |
96
|
|
|
break; |
|
|
|
|
97
|
|
|
case $this->phpType === 'float': |
98
|
|
|
$replace = (string)$this->defaultValue; |
99
|
|
|
if (false === \ts\stringContains($replace, '.')) { |
|
|
|
|
100
|
|
|
$replace .= '.0'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $replace; |
104
|
|
|
case $this->phpType === 'int': |
105
|
|
|
return (string)$this->defaultValue; |
106
|
|
|
case $this->phpType === trim(MappingHelper::PHP_TYPE_DATETIME, '\\'): |
107
|
|
|
if ($this->defaultValue !== null) { |
108
|
|
|
throw new InvalidArgumentException( |
109
|
|
|
'Invalid default value ' . $this->defaultValue |
110
|
|
|
. 'Currently we only support null as a default for DateTime' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return 'null'; |
115
|
|
|
default: |
116
|
|
|
throw new RuntimeException( |
117
|
|
|
'failed to calculate replace based on defaultType ' . $defaultType |
118
|
|
|
. ' and phpType ' . $this->phpType . ' in ' . __METHOD__ |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.