Conditions | 15 |
Paths | 239 |
Total Lines | 47 |
Code Lines | 33 |
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 |
||
72 | public function unserialize(string $input): JWS |
||
73 | { |
||
74 | $data = json_decode($input, true); |
||
75 | if (!is_array($data) || !array_key_exists('signatures', $data)) { |
||
76 | throw new \InvalidArgumentException('Unsupported input.'); |
||
77 | } |
||
78 | |||
79 | $isPayloadEncoded = null; |
||
80 | $rawPayload = array_key_exists('payload', $data) ? $data['payload'] : null; |
||
81 | $signatures = []; |
||
82 | foreach ($data['signatures'] as $signature) { |
||
83 | if (!is_array($signature) || !array_key_exists('signature', $signature)) { |
||
84 | throw new \InvalidArgumentException('Unsupported input.'); |
||
85 | } |
||
86 | $encodedProtectedHeaders = array_key_exists('protected', $signature) ? $signature['protected'] : null; |
||
87 | $protectedHeaders = null !== $encodedProtectedHeaders ? json_decode(Base64Url::decode($encodedProtectedHeaders), true) : []; |
||
88 | $signatures[] = [ |
||
89 | 'signature' => Base64Url::decode($signature['signature']), |
||
90 | 'protected' => $protectedHeaders, |
||
91 | 'encoded_protected' => $encodedProtectedHeaders, |
||
92 | 'header' => array_key_exists('header', $signature) ? $signature['header'] : [], |
||
93 | ]; |
||
94 | if (null === $isPayloadEncoded) { |
||
95 | $isPayloadEncoded = self::isPayloadEncoded($protectedHeaders); |
||
96 | } |
||
97 | if ($this->isPayloadEncoded($protectedHeaders) !== $isPayloadEncoded) { |
||
98 | throw new \InvalidArgumentException('Foreign payload encoding detected.'); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | if (null === $rawPayload) { |
||
103 | $payload = null; |
||
104 | } else { |
||
105 | $payload = false === $isPayloadEncoded ? $rawPayload : Base64Url::decode($rawPayload); |
||
106 | } |
||
107 | $jws = JWS::create($payload, $rawPayload); |
||
108 | foreach ($signatures as $signature) { |
||
109 | $jws = $jws->addSignature( |
||
110 | $signature['signature'], |
||
111 | $signature['protected'], |
||
112 | $signature['encoded_protected'], |
||
113 | $signature['header'] |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | return $jws; |
||
118 | } |
||
119 | |||
141 |