Skip to content

Commit

Permalink
Merge pull request #1433 from terax6669/fix_duplicate_properties
Browse files Browse the repository at this point in the history
Fix duplicate camelCase properties
  • Loading branch information
Jeckerson authored Apr 10, 2020
2 parents daf2696 + ba3b15d commit b0fed14
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Builder/Component/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,17 @@ public function build(): void
}
}

$possibleFields = [];
$possibleFields = $possibleFieldsTransformed = [];
foreach ($fields as $field) {
$possibleFields[$field->getName()] = true;
if ($this->modelOptions->getOption('camelize')) {
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-'));
} else {
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-'));
}
$possibleFieldsTransformed[$fieldName] = true;
}

if (method_exists($reflection, 'getReflectionConstants')) {
foreach ($reflection->getReflectionConstants() as $constant) {
if ($constant->getDeclaringClass()->getName() != $fullClassName) {
Expand Down Expand Up @@ -329,16 +336,16 @@ public function build(): void
}
}

foreach ($reflection->getProperties() as $propertie) {
$propertieName = $propertie->getName();
foreach ($reflection->getProperties() as $property) {
$propertyName = $property->getName();

if ($propertie->getDeclaringClass()->getName() != $fullClassName ||
!empty($possibleFields[$propertieName])) {
if ($property->getDeclaringClass()->getName() != $fullClassName ||
!empty($possibleFieldsTransformed[$propertyName])) {
continue;
}

$modifiersPreg = '';
switch ($propertie->getModifiers()) {
switch ($property->getModifiers()) {
case \ReflectionProperty::IS_PUBLIC:
$modifiersPreg = '^(\s*)public(\s+)';
break;
Expand All @@ -359,7 +366,7 @@ public function build(): void
break;
}

$modifiersPreg = '/' . $modifiersPreg . '\$' . $propertieName . '([\s=;]+)/';
$modifiersPreg = '/' . $modifiersPreg . '\$' . $propertyName . '([\s=;]+)/';
$endLine = $startLine = 0;
foreach ($linesCode as $line => $code) {
if (preg_match($modifiersPreg, $code)) {
Expand All @@ -379,16 +386,16 @@ public function build(): void
}

if (!empty($startLine) && !empty($endLine)) {
$propertieDeclaration = join(
$propertyDeclaration = join(
'',
array_slice(
$linesCode,
$startLine,
$endLine - $startLine + 1
)
);
$attributes[] = PHP_EOL . " " . $propertie->getDocComment() . PHP_EOL .
$propertieDeclaration;
$attributes[] = PHP_EOL . " " . $property->getDocComment() . PHP_EOL .
$propertyDeclaration;
}
}
} catch (\Exception $e) {
Expand Down

0 comments on commit b0fed14

Please sign in to comment.