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 |
||
80 | public function unserialize(string $input): JWS |
||
81 | { |
||
82 | $data = json_decode($input, true); |
||
83 | if (!is_array($data) || !array_key_exists('signatures', $data)) { |
||
84 | throw new \InvalidArgumentException('Unsupported input.'); |
||
85 | } |
||
86 | |||
87 | $isPayloadEncoded = null; |
||
88 | $rawPayload = array_key_exists('payload', $data) ? $data['payload'] : null; |
||
89 | $signatures = []; |
||
90 | foreach ($data['signatures'] as $signature) { |
||
91 | if (!is_array($signature) || !array_key_exists('signature', $signature)) { |
||
92 | throw new \InvalidArgumentException('Unsupported input.'); |
||
93 | } |
||
94 | $encodedProtectedHeaders = array_key_exists('protected', $signature) ? $signature['protected'] : null; |
||
95 | $protectedHeaders = null !== $encodedProtectedHeaders ? json_decode(Base64Url::decode($encodedProtectedHeaders), true) : []; |
||
96 | $signatures[] = [ |
||
97 | 'signature' => Base64Url::decode($signature['signature']), |
||
98 | 'protected' => $protectedHeaders, |
||
99 | 'encoded_protected' => $encodedProtectedHeaders, |
||
100 | 'header' => array_key_exists('header', $signature) ? $signature['header'] : [], |
||
101 | ]; |
||
102 | if (null === $isPayloadEncoded) { |
||
103 | $isPayloadEncoded = self::isPayloadEncoded($protectedHeaders); |
||
104 | } |
||
105 | if ($this->isPayloadEncoded($protectedHeaders) !== $isPayloadEncoded) { |
||
106 | throw new \InvalidArgumentException('Foreign payload encoding detected.'); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | if (null === $rawPayload) { |
||
111 | $payload = null; |
||
112 | } else { |
||
113 | $payload = false === $isPayloadEncoded ? $rawPayload : Base64Url::decode($rawPayload); |
||
114 | } |
||
115 | $jws = JWS::create($payload, $rawPayload); |
||
116 | foreach ($signatures as $signature) { |
||
117 | $jws = $jws->addSignature( |
||
118 | $signature['signature'], |
||
119 | $signature['protected'], |
||
120 | $signature['encoded_protected'], |
||
121 | $signature['header'] |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | return $jws; |
||
126 | } |
||
127 | |||
149 |