Skip to content

Commit 0f353ad

Browse files
minor #21754 [Serializer] Reduce nesting in YamlFileLoader (gadelat)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Reduce nesting in YamlFileLoader | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - We don't need to check if $this->classes is empty, because isset takes care of it in next call anyway Diffs on GH are hard to read for this type of change, here is old and new code: ```php public function loadClassMetadata(ClassMetadataInterface $classMetadata) { if (null === $this->classes) { $this->classes = $this->getClassesFromYaml(); } if (!$this->classes) { return false; } if (isset($this->classes[$classMetadata->getName()])) { $yaml = $this->classes[$classMetadata->getName()]; if (isset($yaml['attributes']) && is_array($yaml['attributes'])) { ... } return true; } return false; } ``` ```php public function loadClassMetadata(ClassMetadataInterface $classMetadata) { if (null === $this->classes) { $this->classes = $this->getClassesFromYaml(); } if (!isset($this->classes[$classMetadata->getName()])) { return false; } $yaml = $this->classes[$classMetadata->getName()]; if (isset($yaml['attributes']) && is_array($yaml['attributes'])) { ... } return true; } ``` Commits ------- 45f0b16 [Serializer] Reduce nesting in YamlFileLoader
2 parents 9aebfad + 45f0b16 commit 0f353ad

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,39 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
6060
$this->classes = $classes;
6161
}
6262

63-
if (isset($this->classes[$classMetadata->getName()])) {
64-
$yaml = $this->classes[$classMetadata->getName()];
65-
66-
if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
67-
$attributesMetadata = $classMetadata->getAttributesMetadata();
68-
69-
foreach ($yaml['attributes'] as $attribute => $data) {
70-
if (isset($attributesMetadata[$attribute])) {
71-
$attributeMetadata = $attributesMetadata[$attribute];
72-
} else {
73-
$attributeMetadata = new AttributeMetadata($attribute);
74-
$classMetadata->addAttributeMetadata($attributeMetadata);
75-
}
63+
if (!isset($this->classes[$classMetadata->getName()])) {
64+
return false;
65+
}
7666

77-
if (isset($data['groups'])) {
78-
if (!is_array($data['groups'])) {
79-
throw new MappingException('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName());
80-
}
67+
$yaml = $this->classes[$classMetadata->getName()];
8168

82-
foreach ($data['groups'] as $group) {
83-
if (!is_string($group)) {
84-
throw new MappingException('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName());
85-
}
69+
if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
70+
$attributesMetadata = $classMetadata->getAttributesMetadata();
71+
72+
foreach ($yaml['attributes'] as $attribute => $data) {
73+
if (isset($attributesMetadata[$attribute])) {
74+
$attributeMetadata = $attributesMetadata[$attribute];
75+
} else {
76+
$attributeMetadata = new AttributeMetadata($attribute);
77+
$classMetadata->addAttributeMetadata($attributeMetadata);
78+
}
8679

87-
$attributeMetadata->addGroup($group);
80+
if (isset($data['groups'])) {
81+
if (!is_array($data['groups'])) {
82+
throw new MappingException('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName());
83+
}
84+
85+
foreach ($data['groups'] as $group) {
86+
if (!is_string($group)) {
87+
throw new MappingException('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName());
8888
}
89+
90+
$attributeMetadata->addGroup($group);
8991
}
9092
}
9193
}
92-
93-
return true;
9494
}
9595

96-
return false;
96+
return true;
9797
}
9898
}

0 commit comments

Comments
 (0)