Conditions | 26 |
Paths | 152 |
Total Lines | 78 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
86 | protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null) |
||
87 | { |
||
88 | $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes); |
||
89 | if ($constructor) { |
||
90 | $constructorParameters = $constructor->getParameters(); |
||
91 | |||
92 | $mismatches = []; |
||
93 | $params = []; |
||
94 | foreach ($constructorParameters as $constructorParameter) { |
||
95 | $paramName = $constructorParameter->name; |
||
96 | $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName; |
||
97 | |||
98 | $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes, true); |
||
99 | $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context); |
||
100 | if ($constructorParameter->isVariadic()) { |
||
101 | if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) { |
||
102 | if (!\is_array($data[$paramName])) { |
||
103 | throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name)); |
||
104 | } |
||
105 | |||
106 | $params = array_merge($params, $data[$paramName]); |
||
107 | } |
||
108 | } elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) { |
||
109 | $parameterData = $data[$key]; |
||
110 | |||
111 | if (null === $parameterData && $constructorParameter->allowsNull()) { |
||
112 | $params[] = null; |
||
113 | // Don't run set for a parameter passed to the constructor |
||
114 | unset($data[$key]); |
||
115 | continue; |
||
116 | } |
||
117 | |||
118 | try { |
||
119 | $parameterType = self::getParameterReflectionClass($constructorParameter); |
||
120 | |||
121 | if (null !== $parameterType) { |
||
122 | $parameterData = $this->recursiveDenormalization($parameterData, $parameterType->getName(), $format, $this->createChildContext($context, $paramName, $format)); |
||
123 | } |
||
124 | } catch (Mismatches\Mismatch $e) { |
||
125 | $mismatches[strtoupper($key)] = $e; |
||
126 | } catch (\ReflectionException $e) { |
||
127 | throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); |
||
128 | } catch (MissingConstructorArgumentsException $e) { |
||
129 | if (!$constructorParameter->getType()->allowsNull()) { |
||
130 | throw $e; |
||
131 | } |
||
132 | $parameterData = null; |
||
133 | } |
||
134 | |||
135 | // Don't run set for a parameter passed to the constructor |
||
136 | $params[] = $parameterData; |
||
137 | unset($data[$key]); |
||
138 | } elseif ($constructorParameter->isDefaultValueAvailable()) { |
||
139 | $params[] = $constructorParameter->getDefaultValue(); |
||
140 | } else { |
||
141 | $message = sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name); |
||
142 | |||
143 | // MissingConstructorArgumentsException added on Sf 4.1 |
||
144 | if (class_exists(MissingConstructorArgumentsException::class)) { |
||
145 | throw new MissingConstructorArgumentsException($message); |
||
146 | } |
||
147 | |||
148 | throw new RuntimeException($message); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if ($mismatches) { |
||
153 | throw new Mismatches\MismatchCollection($mismatches, 'There are {{ count }} errors'); |
||
154 | } |
||
155 | |||
156 | if ($constructor->isConstructor()) { |
||
157 | return $reflectionClass->newInstanceArgs($params); |
||
158 | } |
||
159 | |||
160 | return $constructor->invokeArgs(null, $params); |
||
161 | } |
||
162 | |||
163 | return new $class(); |
||
164 | } |
||
230 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.