|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wabel\Zoho\CRM\Service; |
|
4
|
|
|
|
|
5
|
|
|
use gossi\codegen\generator\CodeFileGenerator; |
|
6
|
|
|
use gossi\codegen\model\PhpClass; |
|
7
|
|
|
use gossi\codegen\model\PhpMethod; |
|
8
|
|
|
use gossi\codegen\model\PhpParameter; |
|
9
|
|
|
use gossi\codegen\model\PhpProperty; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Wabel\Zoho\CRM\ZohoClient; |
|
12
|
|
|
use Wabel\Zoho\CRM\Exception\ZohoCRMException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class is in charge of generating Zoho entities. |
|
16
|
|
|
*/ |
|
17
|
|
|
class EntitiesGeneratorService |
|
18
|
|
|
{ |
|
19
|
|
|
private $zohoClient; |
|
20
|
|
|
private $logger; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(ZohoClient $zohoClient, LoggerInterface $logger) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->zohoClient = $zohoClient; |
|
25
|
|
|
$this->logger = $logger; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Generate ALL entities for all Zoho modules. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $targetDirectory |
|
32
|
|
|
* @param string $namespace |
|
33
|
|
|
* |
|
34
|
|
|
* @return array Array containing each fully qualified dao class name |
|
35
|
|
|
*/ |
|
36
|
|
|
public function generateAll($targetDirectory, $namespace) |
|
37
|
|
|
{ |
|
38
|
|
|
$modules = $this->zohoClient->getModules(); |
|
39
|
|
|
$zohoModules = []; |
|
40
|
|
|
foreach ($modules->getRecords() as $module) { |
|
41
|
|
|
try { |
|
42
|
|
|
$zohoModules[] = $this->generateModule($module['key'], $module['pl'], $module['sl'], $targetDirectory, $namespace); |
|
43
|
|
|
} catch (ZohoCRMException $e) { |
|
44
|
|
|
$this->logger->notice('Error thrown when retrieving fields for module {module}. Error message: {error}.', |
|
45
|
|
|
[ |
|
46
|
|
|
'module' => $module['key'], |
|
47
|
|
|
'error' => $e->getMessage(), |
|
48
|
|
|
'exception' => $e, |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $zohoModules; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Generate a dao for a zoho module. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $moduleName |
|
60
|
|
|
* @param string $modulePlural |
|
61
|
|
|
* @param string $moduleSingular |
|
62
|
|
|
* @param string $targetDirectory |
|
63
|
|
|
* @param string $namespace |
|
64
|
|
|
* |
|
65
|
|
|
* @return string The fully qualified Dao class name |
|
66
|
|
|
*/ |
|
67
|
|
|
public function generateModule($moduleName, $modulePlural, $moduleSingular, $targetDirectory, $namespace) |
|
68
|
|
|
{ |
|
69
|
|
|
$fields = $this->zohoClient->getFields($moduleName); |
|
70
|
|
|
|
|
71
|
|
|
if (!file_exists($targetDirectory)) { |
|
72
|
|
|
mkdir($targetDirectory, 0775, true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$namespace = trim($namespace, '\\'); |
|
76
|
|
|
$className = self::upperCamelCase($moduleSingular); |
|
77
|
|
|
$daoClassName = $className.'ZohoDao'; |
|
78
|
|
|
|
|
79
|
|
|
$fieldRecords = $fields->getRecords(); |
|
80
|
|
|
|
|
81
|
|
|
// Case is a reserved keyword. Let's rename it. |
|
82
|
|
|
if ($className === 'Case') { |
|
83
|
|
|
$className = 'ZohoCase'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->generateBean($fieldRecords, $namespace, $className, $moduleName, $targetDirectory, $moduleSingular); |
|
87
|
|
|
$this->generateDao($fieldRecords, $namespace, $className, $daoClassName, $moduleName, $targetDirectory, $moduleSingular, $modulePlural); |
|
88
|
|
|
|
|
89
|
|
|
return $namespace.'\\'.$daoClassName; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function generateBean($fields, $namespace, $className, $moduleName, $targetDirectory, $moduleSingular) |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
// if (class_exists($namespace."\\".$className)) { |
|
96
|
|
|
// $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$className)); |
|
97
|
|
|
// } else { |
|
98
|
|
|
$class = PhpClass::create(); |
|
99
|
|
|
// } |
|
100
|
|
|
|
|
101
|
|
|
$class->setName($className) |
|
102
|
|
|
->setNamespace($namespace) |
|
103
|
|
|
->addInterface('\\Wabel\\Zoho\\CRM\\ZohoBeanInterface') |
|
104
|
|
|
->setMethod(PhpMethod::create('__construct')); |
|
105
|
|
|
|
|
106
|
|
|
// Let's add the ZohoID property |
|
107
|
|
|
self::registerProperty($class, 'zohoId', "The ID of this record in Zoho\nType: string\n", 'string'); |
|
108
|
|
|
|
|
109
|
|
|
$usedIdentifiers = []; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($fields as &$fieldCategory) { |
|
112
|
|
|
foreach ($fieldCategory as $name => &$field) { |
|
113
|
|
|
$req = $field['req']; |
|
|
|
|
|
|
114
|
|
|
$type = $field['type']; |
|
115
|
|
|
$isreadonly = $field['isreadonly']; |
|
116
|
|
|
$maxlength = $field['maxlength']; |
|
117
|
|
|
$label = $field['label']; |
|
|
|
|
|
|
118
|
|
|
$dv = $field['dv']; |
|
119
|
|
|
$customfield = $field['customfield']; |
|
120
|
|
|
$nullable = false; |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
switch ($type) { |
|
|
|
|
|
|
123
|
|
|
case 'DateTime': |
|
124
|
|
|
case 'Date': |
|
125
|
|
|
$phpType = '\\DateTimeInterface'; |
|
126
|
|
|
$nullable = true; |
|
127
|
|
|
break; |
|
128
|
|
|
case 'Boolean': |
|
129
|
|
|
$phpType = 'bool'; |
|
130
|
|
|
break; |
|
131
|
|
|
case 'Integer': |
|
132
|
|
|
$phpType = 'int'; |
|
133
|
|
|
break; |
|
134
|
|
|
default: |
|
135
|
|
|
$phpType = 'string'; |
|
136
|
|
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$field['phpType'] = $phpType; |
|
140
|
|
|
|
|
141
|
|
|
$identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
|
142
|
|
|
$usedIdentifiers[$identifier] = true; |
|
143
|
|
|
|
|
144
|
|
|
self::registerProperty($class, $identifier, 'Zoho field '.$name."\n". |
|
145
|
|
|
'Type: '.$type."\n". |
|
146
|
|
|
'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
|
147
|
|
|
'Max length: '.$maxlength."\n". |
|
148
|
|
|
'Custom field: '.($customfield ? 'true' : 'false')."\n", $phpType, $nullable); |
|
149
|
|
|
|
|
150
|
|
|
// Adds a ID field for lookups |
|
151
|
|
|
if ($type === 'Lookup') { |
|
152
|
|
|
$generateId = false; |
|
153
|
|
|
|
|
154
|
|
|
if ($customfield) { |
|
155
|
|
|
$name .= '_ID'; |
|
156
|
|
|
$generateId = true; |
|
157
|
|
View Code Duplication |
} elseif ($name === $moduleSingular.' Owner' |
|
|
|
|
|
|
158
|
|
|
|| ($dv === $moduleSingular.' Owner' && $name === $moduleName. ' Owner')) { |
|
159
|
|
|
// Check if this is a "owner" field. |
|
160
|
|
|
$name = 'SMOWNERID'; |
|
161
|
|
|
$generateId = true; |
|
162
|
|
|
} else { |
|
163
|
|
|
$mapping = [ |
|
164
|
|
|
'Account Name' => 'ACCOUNTID', |
|
165
|
|
|
'Contact Name' => 'CONTACTID', |
|
166
|
|
|
'Parent Account' => 'PARENTACCOUNTID', |
|
167
|
|
|
'Campaign Source' => 'CAMPAIGNID', |
|
168
|
|
|
]; |
|
169
|
|
|
if (isset($mapping[$name])) { |
|
170
|
|
|
$name = $mapping[$name]; |
|
171
|
|
|
$generateId = true; |
|
172
|
|
|
} else { |
|
173
|
|
|
$this->logger->warning('Unable to set a ID for the field {name} of the {module} module', [ |
|
174
|
|
|
'name' => $name, |
|
175
|
|
|
'module' => $moduleName, |
|
176
|
|
|
]); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ($generateId) { |
|
181
|
|
|
$req = false; |
|
|
|
|
|
|
182
|
|
|
$type = 'Lookup ID'; |
|
183
|
|
|
$isreadonly = true; |
|
184
|
|
|
$maxlength = $field['maxlength']; |
|
185
|
|
|
$label = $field['label']; |
|
|
|
|
|
|
186
|
|
|
$dv = $field['dv']; |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$field['phpType'] = $phpType; |
|
189
|
|
|
|
|
190
|
|
|
self::registerProperty($class, ($customfield ? self::camelCase($name) : $name), 'Zoho field '.$name."\n". |
|
191
|
|
|
'Type: '.$type."\n". |
|
192
|
|
|
'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
|
193
|
|
|
'Max length: '.$maxlength."\n". |
|
194
|
|
|
'Custom field: '.($customfield ? 'true' : 'false')."\n", 'string'); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
self::registerProperty($class, 'createdTime', "The time the record was created in Zoho\nType: DateTime\n", '\\DateTime'); |
|
201
|
|
|
self::registerProperty($class, 'modifiedTime', "The last time the record was modified in Zoho\nType: DateTime\n", '\\DateTime'); |
|
202
|
|
|
$method = PhpMethod::create('isDirty'); |
|
203
|
|
|
$method->setDescription('Returns whether a property is changed or not.'); |
|
204
|
|
|
$method->addParameter(PhpParameter::create('name')); |
|
205
|
|
|
$method->setBody("\$propertyName = 'dirty'.ucfirst(\$name);\nreturn \$this->\$propertyName;"); |
|
206
|
|
|
$method->setType('bool'); |
|
207
|
|
|
$class->setMethod($method); |
|
208
|
|
|
|
|
209
|
|
|
$generator = new CodeFileGenerator(); |
|
210
|
|
|
$code = $generator->generate($class); |
|
211
|
|
|
|
|
212
|
|
View Code Duplication |
if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$className.'.php', $code)) { |
|
|
|
|
|
|
213
|
|
|
throw new ZohoCRMException("An error occurred while creating the class $className. Please verify the target directory or the rights of the file."); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Returns a unique identifier from the name. |
|
219
|
|
|
* |
|
220
|
|
|
* @param $name |
|
221
|
|
|
* @param array $usedNames |
|
|
|
|
|
|
222
|
|
|
*/ |
|
223
|
|
|
private function getUniqueIdentifier($name, array $usedIdentifiers) |
|
224
|
|
|
{ |
|
225
|
|
|
$id = self::camelCase($name); |
|
226
|
|
|
if (isset($usedIdentifiers[$id])) { |
|
227
|
|
|
$counter = 2; |
|
228
|
|
|
while (isset($usedIdentifiers[$id.'_'.$counter])) { |
|
229
|
|
|
++$counter; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $id.'_'.$counter; |
|
233
|
|
|
} else { |
|
234
|
|
|
return $id; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function generateDao($fields, $namespace, $className, $daoClassName, $moduleName, $targetDirectory, $moduleSingular, $modulePlural) |
|
239
|
|
|
{ |
|
240
|
|
|
// if (class_exists($namespace."\\".$className)) { |
|
241
|
|
|
// $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$daoClassName)); |
|
242
|
|
|
// } else { |
|
243
|
|
|
$class = PhpClass::create(); |
|
244
|
|
|
// } |
|
245
|
|
|
|
|
246
|
|
|
$class->setName($daoClassName) |
|
247
|
|
|
->setNamespace($namespace) |
|
248
|
|
|
->setParentClassName('\\Wabel\\Zoho\\CRM\\AbstractZohoDao'); |
|
249
|
|
|
|
|
250
|
|
|
$usedIdentifiers = []; |
|
251
|
|
|
|
|
252
|
|
|
foreach ($fields as $key => $fieldCategory) { |
|
253
|
|
|
foreach ($fieldCategory as $name => $field) { |
|
254
|
|
|
$type = $field['type']; |
|
255
|
|
|
|
|
256
|
|
View Code Duplication |
switch ($type) { |
|
|
|
|
|
|
257
|
|
|
case 'DateTime': |
|
258
|
|
|
case 'Date': |
|
259
|
|
|
$phpType = '\\DateTime'; |
|
260
|
|
|
break; |
|
261
|
|
|
case 'Boolean': |
|
262
|
|
|
$phpType = 'bool'; |
|
263
|
|
|
break; |
|
264
|
|
|
case 'Integer': |
|
265
|
|
|
$phpType = 'int'; |
|
266
|
|
|
break; |
|
267
|
|
|
default: |
|
268
|
|
|
$phpType = 'string'; |
|
269
|
|
|
break; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$fields[$key][$name]['phpType'] = $phpType; |
|
273
|
|
|
$identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
|
274
|
|
|
$usedIdentifiers[$identifier] = true; |
|
275
|
|
|
$fields[$key][$name]['getter'] = 'get'.ucfirst($identifier); |
|
276
|
|
|
$fields[$key][$name]['setter'] = 'set'.ucfirst($identifier); |
|
277
|
|
|
$fields[$key][$name]['name'] = $identifier; |
|
278
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
//Detect Special Fields from Zoho - Using API, you cannot create or update these system-generated fields: |
|
281
|
|
|
//@Todo: Manage these fields. The problem comes "MODIFIEDBY" and "Modified By" fields |
|
282
|
|
|
$specialOwnerFieldsMapping = [ |
|
283
|
|
|
'Created By' => 'SMCREATORID', |
|
284
|
|
|
'Modified By' =>'MODIFIEDBY', |
|
285
|
|
|
]; |
|
286
|
|
|
$specialDateFieldsMapping = [ |
|
287
|
|
|
'Created Time' => 'createdTime', |
|
288
|
|
|
'Modified Time' =>'modifiedTime' |
|
289
|
|
|
]; |
|
290
|
|
|
$linkedSystemGeneratedFields = [ |
|
291
|
|
|
'Created By' => 'Created Time', |
|
292
|
|
|
'Modified By' =>'Modified Time', |
|
293
|
|
|
]; |
|
294
|
|
|
$systemGenerated = false; |
|
295
|
|
|
if(array_key_exists($field['label'],$specialOwnerFieldsMapping)){ |
|
296
|
|
|
$systemGenerated = true; |
|
297
|
|
|
} |
|
298
|
|
|
if($systemGenerated && isset($linkedSystemGeneratedFields[$field['label']])){ |
|
299
|
|
|
$dateSpecialField = false; |
|
300
|
|
|
if (isset($specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]])) { |
|
301
|
|
|
$name = $specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]]; |
|
302
|
|
|
$generateId = true; |
|
303
|
|
|
$dateSpecialField = true; |
|
304
|
|
|
} |
|
305
|
|
View Code Duplication |
if ($generateId && $dateSpecialField) { |
|
|
|
|
|
|
306
|
|
|
$fields[$key][$name]['req'] = false; |
|
307
|
|
|
$fields[$key][$name]['type'] = 'DateTime'; |
|
308
|
|
|
$fields[$key][$name]['isreadonly'] = false; |
|
309
|
|
|
$fields[$key][$name]['maxlength'] = 20; |
|
310
|
|
|
$fields[$key][$name]['label'] = $name; |
|
311
|
|
|
$fields[$key][$name]['dv'] = $name; |
|
312
|
|
|
$fields[$key][$name]['customfield'] = true; |
|
313
|
|
|
$fields[$key][$name]['phpType'] = '\\DateTime'; |
|
314
|
|
|
$fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
|
315
|
|
|
$fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
|
316
|
|
|
$fields[$key][$name]['name'] = self::camelCase($name); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
if ($type === 'Lookup') { |
|
320
|
|
|
$generateId = false; |
|
321
|
|
|
|
|
322
|
|
|
if ($field['customfield']) { |
|
323
|
|
|
$name .= '_ID'; |
|
324
|
|
|
$generateId = true; |
|
325
|
|
View Code Duplication |
} elseif ($field['label'] === $moduleSingular.' Owner' |
|
|
|
|
|
|
326
|
|
|
|| ($field['dv'] === $moduleSingular.' Owner' && $name === $moduleName. ' Owner')) { |
|
327
|
|
|
// Check if this is a "owner" field. |
|
328
|
|
|
$name = 'SMOWNERID'; |
|
329
|
|
|
$generateId = true; |
|
330
|
|
|
} else { |
|
331
|
|
|
$mapping = [ |
|
332
|
|
|
'Account Name' => 'ACCOUNTID', |
|
333
|
|
|
'Contact Name' => 'CONTACTID', |
|
334
|
|
|
'Parent Account' => 'PARENTACCOUNTID', |
|
335
|
|
|
'Campaign Source' => 'CAMPAIGNID', |
|
336
|
|
|
]; |
|
337
|
|
|
if (isset($mapping[$field['label']])) { |
|
338
|
|
|
$name = $mapping[$field['label']]; |
|
339
|
|
|
$generateId = true; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
View Code Duplication |
if ($generateId) { |
|
|
|
|
|
|
343
|
|
|
$fields[$key][$name]['req'] = false; |
|
344
|
|
|
$fields[$key][$name]['type'] = 'Lookup ID'; |
|
345
|
|
|
$fields[$key][$name]['isreadonly'] = true; |
|
346
|
|
|
$fields[$key][$name]['maxlength'] = 100; |
|
347
|
|
|
$fields[$key][$name]['label'] = $name; |
|
348
|
|
|
$fields[$key][$name]['dv'] = $name; |
|
349
|
|
|
$fields[$key][$name]['customfield'] = true; |
|
350
|
|
|
$fields[$key][$name]['phpType'] = $phpType; |
|
351
|
|
|
$fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
|
352
|
|
|
$fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
|
353
|
|
|
$fields[$key][$name]['name'] = self::camelCase($name); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
$class->setMethod(PhpMethod::create('getModule')->setBody('return '.var_export($moduleName, true).';')); |
|
360
|
|
|
|
|
361
|
|
|
$class->setMethod(PhpMethod::create('getSingularModuleName')->setBody('return '.var_export($moduleSingular, true).';')); |
|
362
|
|
|
|
|
363
|
|
|
$class->setMethod(PhpMethod::create('getPluralModuleName')->setBody('return '.var_export($modulePlural, true).';')); |
|
364
|
|
|
|
|
365
|
|
|
$class->setMethod(PhpMethod::create('getFields')->setBody('return '.var_export($fields, true).';')); |
|
366
|
|
|
|
|
367
|
|
|
$class->setMethod(PhpMethod::create('getBeanClassName')->setBody('return '.var_export($namespace.'\\'.$className, true).';')); |
|
368
|
|
|
|
|
369
|
|
|
$generator = new CodeFileGenerator(); |
|
370
|
|
|
$code = $generator->generate($class); |
|
371
|
|
|
|
|
372
|
|
View Code Duplication |
if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$daoClassName.'.php', $code)) { |
|
|
|
|
|
|
373
|
|
|
throw new ZohoCRMException("An error occurred while creating the DAO $daoClassName. Please verify the target directory exists or the rights of the file."); |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
private static function camelCase($str, array $noStrip = []) |
|
378
|
|
|
{ |
|
379
|
|
|
$str = self::upperCamelCase($str, $noStrip); |
|
380
|
|
|
$str = lcfirst($str); |
|
381
|
|
|
|
|
382
|
|
|
return $str; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
private static function upperCamelCase($str, array $noStrip = []) |
|
386
|
|
|
{ |
|
387
|
|
|
// non-alpha and non-numeric characters become spaces |
|
388
|
|
|
$str = preg_replace('/[^a-z0-9'.implode('', $noStrip).']+/i', ' ', $str); |
|
389
|
|
|
$str = trim($str); |
|
390
|
|
|
// uppercase the first character of each word |
|
391
|
|
|
$str = ucwords($str); |
|
392
|
|
|
$str = str_replace(' ', '', $str); |
|
393
|
|
|
|
|
394
|
|
|
return $str; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
private static function registerProperty(PhpClass $class, $name, $description, $type, $nullable = false) |
|
398
|
|
|
{ |
|
399
|
|
View Code Duplication |
if (!$class->hasProperty($name)) { |
|
|
|
|
|
|
400
|
|
|
$property = PhpProperty::create($name); |
|
401
|
|
|
$property->setDescription($description); |
|
402
|
|
|
$property->setType($type); |
|
403
|
|
|
$property->setVisibility('protected'); |
|
404
|
|
|
|
|
405
|
|
|
$class->setProperty($property); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
$isDirtyName = 'dirty'.ucfirst($name); |
|
409
|
|
View Code Duplication |
if (!$class->hasProperty($isDirtyName)) { |
|
|
|
|
|
|
410
|
|
|
$dirtyProperty = PhpProperty::create($isDirtyName); |
|
411
|
|
|
$dirtyProperty->setDescription("Whether '$name' has been changed or not."); |
|
412
|
|
|
$dirtyProperty->setType('bool'); |
|
413
|
|
|
$dirtyProperty->setVisibility('protected'); |
|
414
|
|
|
$dirtyProperty->setDefaultValue(false); |
|
|
|
|
|
|
415
|
|
|
|
|
416
|
|
|
$class->setProperty($dirtyProperty); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
$getterName = 'get'.ucfirst($name); |
|
420
|
|
|
$getterDescription = 'Get '.lcfirst($description); |
|
421
|
|
|
$setterName = 'set'.ucfirst($name); |
|
422
|
|
|
$setterDescription = 'Set '.lcfirst($description); |
|
423
|
|
|
|
|
424
|
|
|
if (!$class->hasMethod($getterName)) { |
|
425
|
|
|
$method = PhpMethod::create($getterName); |
|
426
|
|
|
$method->setDescription($getterDescription); |
|
427
|
|
|
$method->setBody("return \$this->{$name};"); |
|
428
|
|
|
$class->setMethod($method); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
if (!$class->hasMethod($setterName)) { |
|
432
|
|
|
$method = PhpMethod::create($setterName); |
|
433
|
|
|
$method->setDescription($setterDescription); |
|
434
|
|
|
$parameter = PhpParameter::create($name)->setType($type); |
|
435
|
|
|
if ($nullable) { |
|
436
|
|
|
$parameter->setValue(null); |
|
437
|
|
|
} |
|
438
|
|
|
$method->addParameter($parameter); |
|
439
|
|
|
$method->setBody("\$this->{$name} = \${$name};\n". |
|
440
|
|
|
'$this->dirty'.ucfirst($name)." = true;\n". |
|
441
|
|
|
'return $this;'); |
|
442
|
|
|
$class->setMethod($method); |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
} |
|
446
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.