Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | class JmsMetadataParser |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var \Metadata\MetadataFactoryInterface |
||
| 18 | */ |
||
| 19 | private $factory; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var PropertyNamingStrategyInterface |
||
| 23 | */ |
||
| 24 | private $namingStrategy; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor, requires JMS Metadata factory. |
||
| 28 | */ |
||
| 29 | 2 | public function __construct( |
|
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 2 | public function parse(array $input) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Recursively parse all metadata for a class. |
||
| 50 | * |
||
| 51 | * @param string $className Class to get all metadata for |
||
| 52 | * @param array $visited Classes we've already visited to prevent infinite recursion. |
||
| 53 | * @param array $groups Serialization groups to include. |
||
| 54 | * |
||
| 55 | * @return array metadata for given class |
||
| 56 | * |
||
| 57 | * @throws \InvalidArgumentException |
||
| 58 | */ |
||
| 59 | 2 | protected function doParse($className, $visited = array(), array $groups = array()) |
|
| 60 | { |
||
| 61 | 2 | $meta = $this->factory->getMetadataForClass($className); |
|
| 62 | |||
| 63 | 2 | if (null === $meta) { |
|
| 64 | throw new \InvalidArgumentException(sprintf('No metadata found for class %s', $className)); |
||
| 65 | } |
||
| 66 | |||
| 67 | 2 | $exclusionStrategies = array(); |
|
| 68 | 2 | if ($groups) { |
|
|
|
|||
| 69 | 1 | $exclusionStrategies[] = new GroupsExclusionStrategy($groups); |
|
| 70 | 1 | } |
|
| 71 | |||
| 72 | 2 | $params = array(); |
|
| 73 | |||
| 74 | // iterate over property metadata |
||
| 75 | 2 | foreach ($meta->propertyMetadata as $item) { |
|
| 76 | 2 | if (!is_null($item->type)) { |
|
| 77 | 2 | $name = $this->namingStrategy->translateName($item); |
|
| 78 | |||
| 79 | 2 | $dataType = $this->processDataType($item); |
|
| 80 | |||
| 81 | // apply exclusion strategies |
||
| 82 | 2 | foreach ($exclusionStrategies as $strategy) { |
|
| 83 | 1 | if (true === $strategy->shouldSkipProperty($item, SerializationContext::create())) { |
|
| 84 | 1 | continue 2; |
|
| 85 | } |
||
| 86 | 2 | } |
|
| 87 | |||
| 88 | 2 | $params[$name] = array( |
|
| 89 | 2 | 'dataType' => $dataType['normalized'], |
|
| 90 | 2 | 'required' => false, |
|
| 91 | 2 | 'readonly' => $item->readOnly, |
|
| 92 | 2 | 'sinceVersion' => $item->sinceVersion, |
|
| 93 | 2 | 'untilVersion' => $item->untilVersion, |
|
| 94 | ); |
||
| 95 | |||
| 96 | 2 | if (!is_null($dataType['class'])) { |
|
| 97 | 1 | $params[$name]['class'] = $dataType['class']; |
|
| 98 | 1 | } |
|
| 99 | |||
| 100 | // if class already parsed, continue, to avoid infinite recursion |
||
| 101 | 2 | if (in_array($dataType['class'], $visited)) { |
|
| 102 | 1 | continue; |
|
| 103 | } |
||
| 104 | |||
| 105 | // check for nested classes with JMS metadata |
||
| 106 | 2 | if ($dataType['class'] && null !== $this->factory->getMetadataForClass($dataType['class'])) { |
|
| 107 | 1 | $visited[] = $dataType['class']; |
|
| 108 | 1 | $params[$name]['children'] = $this->doParse($dataType['class'], $visited, $groups); |
|
| 109 | 1 | } |
|
| 110 | 2 | } |
|
| 111 | 2 | } |
|
| 112 | |||
| 113 | 2 | return $params; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Figure out a normalized data type (for documentation), and get a |
||
| 118 | * nested class name, if available. |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | 2 | protected function processDataType(PropertyMetadata $item) |
|
| 123 | { |
||
| 124 | // check for a type inside something that could be treated as an array |
||
| 125 | 2 | if ($nestedType = $this->getNestedTypeInArray($item)) { |
|
| 126 | 2 | if ($this->isPrimitive($nestedType)) { |
|
| 127 | return array( |
||
| 128 | 2 | 'normalized' => sprintf('array of %ss', $nestedType), |
|
| 129 | 2 | 'class' => null, |
|
| 130 | 2 | ); |
|
| 131 | } |
||
| 132 | |||
| 133 | 2 | $exp = explode('\\', $nestedType); |
|
| 134 | |||
| 135 | return array( |
||
| 136 | 2 | 'normalized' => sprintf('array of objects (%s)', end($exp)), |
|
| 137 | 2 | 'class' => $nestedType, |
|
| 138 | 2 | ); |
|
| 139 | } |
||
| 140 | |||
| 141 | 2 | $type = $item->type['name']; |
|
| 142 | |||
| 143 | // could be basic type |
||
| 144 | 2 | if ($this->isPrimitive($type)) { |
|
| 145 | return array( |
||
| 146 | 1 | 'normalized' => $type, |
|
| 147 | 1 | 'class' => null, |
|
| 148 | 1 | ); |
|
| 149 | } |
||
| 150 | |||
| 151 | // we can use type property also for custom handlers, then we don't have here real class name |
||
| 152 | 2 | if (!class_exists($type)) { |
|
| 153 | return array( |
||
| 154 | 2 | 'normalized' => sprintf('custom handler result for (%s)', $type), |
|
| 155 | 2 | 'class' => null, |
|
| 156 | 2 | ); |
|
| 157 | } |
||
| 158 | |||
| 159 | // if we got this far, it's a general class name |
||
| 160 | 2 | $exp = explode('\\', $type); |
|
| 161 | |||
| 162 | return array( |
||
| 163 | 2 | 'normalized' => sprintf('object (%s)', end($exp)), |
|
| 164 | 2 | 'class' => $type, |
|
| 165 | 2 | ); |
|
| 166 | } |
||
| 167 | |||
| 168 | 2 | protected function isPrimitive($type) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Check the various ways JMS describes values in arrays, and |
||
| 175 | * get the value type in the array. |
||
| 176 | * |
||
| 177 | * @param PropertyMetadata $item |
||
| 178 | * |
||
| 179 | * @return string|null |
||
| 180 | */ |
||
| 181 | 2 | protected function getNestedTypeInArray(PropertyMetadata $item) |
|
| 196 | } |
||
| 197 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.