Complex classes like YamlDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use YamlDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class YamlDriver extends AbstractFileDriver |
||
| 32 | { |
||
| 33 | 20 | protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
|
| 34 | { |
||
| 35 | 20 | $config = Yaml::parse(file_get_contents($file)); |
|
| 36 | |||
| 37 | 20 | if ( ! isset($config[$name = $class->name])) { |
|
| 38 | throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file)); |
||
| 39 | } |
||
| 40 | |||
| 41 | 20 | $config = $config[$name]; |
|
| 42 | 20 | $metadata = new ClassMetadata($name); |
|
| 43 | 20 | $metadata->fileResources[] = $file; |
|
| 44 | 20 | $metadata->fileResources[] = $class->getFileName(); |
|
| 45 | 20 | $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE'; |
|
| 46 | 20 | $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false; |
|
| 47 | 20 | $classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY; |
|
| 48 | 20 | $readOnlyClass = isset($config['read_only']) ? (Boolean) $config['read_only'] : false; |
|
| 49 | 20 | $this->addClassProperties($metadata, $config); |
|
| 50 | |||
| 51 | 20 | $propertiesMetadata = array(); |
|
| 52 | 20 | if (array_key_exists('virtual_properties', $config)) { |
|
| 53 | 2 | foreach ($config['virtual_properties'] as $methodName => $propertySettings) { |
|
| 54 | 2 | if ( ! $class->hasMethod($methodName)) { |
|
| 55 | throw new RuntimeException('The method '.$methodName.' not found in class '.$class->name); |
||
| 56 | } |
||
| 57 | |||
| 58 | 2 | $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $methodName); |
|
| 59 | |||
| 60 | 2 | $propertiesMetadata[$methodName] = $virtualPropertyMetadata; |
|
| 61 | 2 | $config['properties'][$methodName] = $propertySettings; |
|
| 62 | 2 | } |
|
| 63 | 2 | } |
|
| 64 | |||
| 65 | 20 | if ( ! $excludeAll) { |
|
| 66 | 20 | foreach ($class->getProperties() as $property) { |
|
| 67 | 19 | if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) { |
|
|
|
|||
| 68 | 2 | continue; |
|
| 69 | } |
||
| 70 | |||
| 71 | 18 | $pName = $property->getName(); |
|
| 72 | 18 | $propertiesMetadata[$pName] = new PropertyMetadata($name, $pName); |
|
| 73 | 20 | } |
|
| 74 | |||
| 75 | 20 | foreach ($propertiesMetadata as $pName => $pMetadata) { |
|
| 76 | 19 | $isExclude = false; |
|
| 77 | 1 | $isExpose = $pMetadata instanceof VirtualPropertyMetadata |
|
| 78 | 19 | || (isset($config['properties']) && array_key_exists($pName, $config['properties'])); |
|
| 79 | |||
| 80 | 19 | if (isset($config['properties'][$pName])) { |
|
| 81 | 16 | $pConfig = $config['properties'][$pName]; |
|
| 82 | |||
| 83 | 16 | if (isset($pConfig['exclude'])) { |
|
| 84 | 1 | $isExclude = (Boolean) $pConfig['exclude']; |
|
| 85 | 1 | } |
|
| 86 | |||
| 87 | 16 | if (isset($pConfig['expose'])) { |
|
| 88 | 2 | $isExpose = (Boolean) $pConfig['expose']; |
|
| 89 | 2 | } |
|
| 90 | |||
| 91 | 16 | if (isset($pConfig['since_version'])) { |
|
| 92 | $pMetadata->sinceVersion = (string) $pConfig['since_version']; |
||
| 93 | } |
||
| 94 | |||
| 95 | 16 | if (isset($pConfig['until_version'])) { |
|
| 96 | $pMetadata->untilVersion = (string) $pConfig['until_version']; |
||
| 97 | } |
||
| 98 | |||
| 99 | 16 | if (isset($pConfig['serialized_name'])) { |
|
| 100 | 3 | $pMetadata->serializedName = (string) $pConfig['serialized_name']; |
|
| 101 | 3 | } |
|
| 102 | |||
| 103 | 16 | if (isset($pConfig['type'])) { |
|
| 104 | 13 | $pMetadata->setType((string) $pConfig['type']); |
|
| 105 | 13 | } |
|
| 106 | |||
| 107 | 16 | if (isset($pConfig['groups'])) { |
|
| 108 | 1 | $pMetadata->groups = $pConfig['groups']; |
|
| 109 | 1 | } |
|
| 110 | |||
| 111 | 16 | if (isset($pConfig['xml_list'])) { |
|
| 112 | 2 | $pMetadata->xmlCollection = true; |
|
| 113 | |||
| 114 | 2 | $colConfig = $pConfig['xml_list']; |
|
| 115 | 2 | if (isset($colConfig['inline'])) { |
|
| 116 | 2 | $pMetadata->xmlCollectionInline = (Boolean)$colConfig['inline']; |
|
| 117 | 2 | } |
|
| 118 | |||
| 119 | 2 | if (isset($colConfig['entry_name'])) { |
|
| 120 | 1 | $pMetadata->xmlEntryName = (string)$colConfig['entry_name']; |
|
| 121 | 1 | } |
|
| 122 | |||
| 123 | 2 | if (isset($colConfig['skip_when_empty'])) { |
|
| 124 | 1 | $pMetadata->xmlCollectionSkipWhenEmpty = (Boolean)$colConfig['skip_when_empty']; |
|
| 125 | 1 | } else { |
|
| 126 | 2 | $pMetadata->xmlCollectionSkipWhenEmpty = true; |
|
| 127 | } |
||
| 128 | |||
| 129 | 2 | if (isset($colConfig['namespace'])) { |
|
| 130 | $pMetadata->xmlEntryNamespace = (string) $colConfig['namespace']; |
||
| 131 | } |
||
| 132 | 2 | } |
|
| 133 | |||
| 134 | 16 | if (isset($pConfig['xml_map'])) { |
|
| 135 | $pMetadata->xmlCollection = true; |
||
| 136 | |||
| 137 | $colConfig = $pConfig['xml_map']; |
||
| 138 | if (isset($colConfig['inline'])) { |
||
| 139 | $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline']; |
||
| 140 | } |
||
| 141 | |||
| 142 | if (isset($colConfig['entry_name'])) { |
||
| 143 | $pMetadata->xmlEntryName = (string) $colConfig['entry_name']; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (isset($colConfig['namespace'])) { |
||
| 147 | $pMetadata->xmlEntryNamespace = (string) $colConfig['namespace']; |
||
| 148 | } |
||
| 149 | |||
| 150 | if (isset($colConfig['key_attribute_name'])) { |
||
| 151 | $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name']; |
||
| 152 | } |
||
| 153 | |||
| 154 | } |
||
| 155 | |||
| 156 | 16 | if (isset($pConfig['xml_element'])) { |
|
| 157 | 4 | $colConfig = $pConfig['xml_element']; |
|
| 158 | 4 | if (isset($colConfig['cdata'])) { |
|
| 159 | 2 | $pMetadata->xmlElementCData = (Boolean) $colConfig['cdata']; |
|
| 160 | 2 | } |
|
| 161 | |||
| 162 | 4 | if (isset($colConfig['namespace'])) { |
|
| 163 | 3 | $pMetadata->xmlNamespace = (string) $colConfig['namespace']; |
|
| 164 | 3 | } |
|
| 165 | 4 | } |
|
| 166 | |||
| 167 | 16 | if (isset($pConfig['xml_attribute'])) { |
|
| 168 | 4 | $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute']; |
|
| 169 | 4 | } |
|
| 170 | |||
| 171 | 16 | if (isset($pConfig['xml_attribute_map'])) { |
|
| 172 | $pMetadata->xmlAttributeMap = (Boolean) $pConfig['xml_attribute_map']; |
||
| 173 | } |
||
| 174 | |||
| 175 | 16 | if (isset($pConfig['xml_value'])) { |
|
| 176 | 2 | $pMetadata->xmlValue = (Boolean) $pConfig['xml_value']; |
|
| 177 | 2 | } |
|
| 178 | |||
| 179 | 16 | if (isset($pConfig['xml_key_value_pairs'])) { |
|
| 180 | 1 | $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs']; |
|
| 181 | 1 | } |
|
| 182 | |||
| 183 | //we need read_only before setter and getter set, because that method depends on flag being set |
||
| 184 | 16 | if (isset($pConfig['read_only'])) { |
|
| 185 | 1 | $pMetadata->readOnly = (Boolean) $pConfig['read_only']; |
|
| 186 | 1 | } else { |
|
| 187 | 15 | $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass; |
|
| 188 | } |
||
| 189 | |||
| 190 | 16 | $pMetadata->setAccessor( |
|
| 191 | 16 | isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType, |
|
| 192 | 16 | isset($pConfig['accessor']['getter']) ? $pConfig['accessor']['getter'] : null, |
|
| 193 | 16 | isset($pConfig['accessor']['setter']) ? $pConfig['accessor']['setter'] : null |
|
| 194 | 16 | ); |
|
| 195 | |||
| 196 | 16 | if (isset($pConfig['inline'])) { |
|
| 197 | $pMetadata->inline = (Boolean) $pConfig['inline']; |
||
| 198 | } |
||
| 199 | |||
| 200 | 16 | if (isset($pConfig['max_depth'])) { |
|
| 201 | 1 | $pMetadata->maxDepth = (int) $pConfig['max_depth']; |
|
| 202 | 1 | } |
|
| 203 | 16 | } |
|
| 204 | 19 | if ((ExclusionPolicy::NONE === $exclusionPolicy && ! $isExclude) |
|
| 205 | 19 | || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) { |
|
| 206 | 19 | $metadata->addPropertyMetadata($pMetadata); |
|
| 207 | 19 | } |
|
| 208 | 20 | } |
|
| 209 | 20 | } |
|
| 210 | |||
| 211 | 20 | if (isset($config['handler_callbacks'])) { |
|
| 212 | 1 | foreach ($config['handler_callbacks'] as $directionName => $formats) { |
|
| 213 | 1 | $direction = GraphNavigator::parseDirection($directionName); |
|
| 214 | 1 | foreach ($formats as $format => $methodName) { |
|
| 215 | 1 | $metadata->addHandlerCallback($direction, $format, $methodName); |
|
| 216 | 1 | } |
|
| 217 | 1 | } |
|
| 218 | 1 | } |
|
| 219 | |||
| 220 | 20 | if (isset($config['callback_methods'])) { |
|
| 221 | $cConfig = $config['callback_methods']; |
||
| 222 | |||
| 223 | if (isset($cConfig['pre_serialize'])) { |
||
| 224 | $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']); |
||
| 225 | } |
||
| 226 | if (isset($cConfig['post_serialize'])) { |
||
| 227 | $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']); |
||
| 228 | } |
||
| 229 | if (isset($cConfig['post_deserialize'])) { |
||
| 230 | $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | 20 | return $metadata; |
|
| 235 | } |
||
| 236 | |||
| 237 | 20 | protected function getExtension() |
|
| 241 | |||
| 242 | 20 | private function addClassProperties(ClassMetadata $metadata, array $config) |
|
| 294 | |||
| 295 | private function getCallbackMetadata(\ReflectionClass $class, $config) |
||
| 314 | } |
||
| 315 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.