Conditions | 23 |
Paths | 165 |
Total Lines | 111 |
Code Lines | 63 |
Lines | 46 |
Ratio | 41.44 % |
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 |
||
72 | public static function fromData(array $jsonData) |
||
73 | { |
||
74 | if (!isset($jsonData['Response'])) { |
||
75 | throw new InvalidPdpResponseException('Key "Response" was not found in the PDP response'); |
||
76 | } |
||
77 | |||
78 | if (!is_array($jsonData['Response'])) { |
||
79 | throw new InvalidPdpResponseException('Key "Response" is not an array'); |
||
80 | } |
||
81 | |||
82 | |||
83 | if (!isset($jsonData['Response'][0])) { |
||
84 | throw new InvalidPdpResponseException('No response data found'); |
||
85 | } |
||
86 | |||
87 | $responseData = $jsonData['Response'][0]; |
||
88 | |||
89 | if (!isset($responseData['Status'])) { |
||
90 | throw new InvalidPdpResponseException('Key "Status" was not found in the PDP response'); |
||
91 | } |
||
92 | |||
93 | if (!isset($responseData['Decision'])) { |
||
94 | throw new InvalidPdpResponseException('Key "Decision" was not found in the PDP response'); |
||
95 | } |
||
96 | |||
97 | $response = new self; |
||
98 | |||
99 | $response->status = new Status; |
||
100 | $response->status->statusCode = new StatusCode; |
||
101 | $response->status->statusCode->value = $responseData['Status']['StatusCode']['Value']; |
||
102 | View Code Duplication | if (isset($responseData['Status']['StatusDetail'])) { |
|
|
|||
103 | $response->status->statusDetail = $responseData['Status']['StatusDetail']; |
||
104 | } |
||
105 | View Code Duplication | if (isset($responseData['Status']['StatusMessage'])) { |
|
106 | $response->status->statusMessage = $responseData['Status']['StatusMessage']; |
||
107 | } |
||
108 | |||
109 | if (isset($responseData['Category'])) { |
||
110 | foreach ($responseData['Category'] as $categoryData) { |
||
111 | $category = new Category; |
||
112 | $category->categoryId = $categoryData['CategoryId']; |
||
113 | $category->attributes = []; |
||
114 | |||
115 | foreach ($categoryData['Attribute'] as $attributeData) { |
||
116 | $attribute = new Attribute; |
||
117 | $attribute->attributeId = $attributeData['AttributeId']; |
||
118 | $attribute->value = $attributeData['Value']; |
||
119 | |||
120 | if (isset($attributeData['DataType'])) { |
||
121 | $attribute->dataType = $attributeData['DataType']; |
||
122 | } |
||
123 | |||
124 | $category->attributes[] = $attribute; |
||
125 | } |
||
126 | |||
127 | $response->categories[] = $category; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | View Code Duplication | if (isset($responseData['AssociatedAdvice'])) { |
|
132 | foreach ($responseData['AssociatedAdvice'] as $associatedAdviceData) { |
||
133 | $associatedAdvice = new AssociatedAdvice; |
||
134 | $associatedAdvice->id = $associatedAdviceData['Id']; |
||
135 | |||
136 | foreach ($associatedAdviceData['AttributeAssignment'] as $attributeAssignmentData) { |
||
137 | $associatedAdvice->attributeAssignments[] = self::parseAttributeAssignmentData($attributeAssignmentData); |
||
138 | } |
||
139 | |||
140 | $response->associatedAdvices[] = $associatedAdvice; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | View Code Duplication | if (isset($responseData['Obligations'])) { |
|
145 | foreach ($responseData['Obligations'] as $obligationData) { |
||
146 | $obligation = new Obligation; |
||
147 | $obligation->id = $obligationData['Id']; |
||
148 | |||
149 | foreach ($obligationData['AttributeAssignment'] as $attributeAssignmentData) { |
||
150 | $obligation->attributeAssignments[] = self::parseAttributeAssignmentData($attributeAssignmentData); |
||
151 | } |
||
152 | |||
153 | $response->obligations[] = $obligation; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | if (isset($responseData['PolicyIdentifier'])) { |
||
158 | $response->policyIdentifier = new PolicyIdentifier; |
||
159 | |||
160 | View Code Duplication | if (isset($responseData['PolicyIdentifier']['PolicySetIdReference'])) { |
|
161 | foreach ($responseData['PolicyIdentifier']['PolicySetIdReference'] as $policySetIdReferenceData) { |
||
162 | $policySetIdReference = new PolicySetIdReference; |
||
163 | $policySetIdReference->version = $policySetIdReferenceData['Version']; |
||
164 | $policySetIdReference->id = $policySetIdReferenceData['Id']; |
||
165 | $response->policyIdentifier->policySetIdReference[] = $policySetIdReference; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | View Code Duplication | if (isset($responseData['PolicyIdentifier']['PolicyIdReference'])) { |
|
170 | foreach ($responseData['PolicyIdentifier']['PolicyIdReference'] as $policyIdReferenceData) { |
||
171 | $policyIdReference = new PolicyIdReference; |
||
172 | $policyIdReference->version = $policyIdReferenceData['Version']; |
||
173 | $policyIdReference->id = $policyIdReferenceData['Id']; |
||
174 | $response->policyIdentifier->policyIdReference[] = $policyIdReference; |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $response->decision = $responseData['Decision']; |
||
180 | |||
181 | return $response; |
||
182 | } |
||
183 | |||
201 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.