GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 62af7f...70b15e )
by Joni
04:01
created

RSASSAPKCS1Algorithm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 15.28 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 6
dl 11
loc 72
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromPublicKey() 0 3 1
A fromPrivateKey() 0 3 1
A fromJWK() 11 11 3
A headerParameters() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace JWX\JWS\Algorithm;
4
5
use JWX\JWA\JWA;
6
use JWX\JWK\JWK;
7
use JWX\JWK\RSA\RSAPrivateKeyJWK;
8
use JWX\JWK\RSA\RSAPublicKeyJWK;
9
use JWX\JWT\Header\Header;
10
use JWX\JWT\Parameter\AlgorithmParameter;
11
12
13
/**
14
 * Base class for algorithms implementing signature with PKCS #1.
15
 *
16
 * @link https://tools.ietf.org/html/rfc7518#section-3.3
17
 */
18
abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm
19
{
20
	/**
21
	 * Mapping from algorithm name to class name.
22
	 *
23
	 * @internal
24
	 *
25
	 * @var array
26
	 */
27
	const MAP_ALGO_TO_CLASS = array(
28
		/* @formatter:off */
29
		JWA::ALGO_RS256 => RS256Algorithm::class,
30
		JWA::ALGO_RS384 => RS384Algorithm::class,
31
		JWA::ALGO_RS512 => RS512Algorithm::class
32
		/* @formatter:on */
33
	);
34
	
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param RSAPublicKeyJWK $pub_key
39
	 * @param RSAPrivateKeyJWK $priv_key
40
	 */
41 10
	protected function __construct(RSAPublicKeyJWK $pub_key, 
42
			RSAPrivateKeyJWK $priv_key = null) {
43 10
		$this->_publicKey = $pub_key;
44 10
		$this->_privateKey = $priv_key;
45 10
	}
46
	
47
	/**
48
	 * Initialize from a public key.
49
	 *
50
	 * @param RSAPublicKeyJWK $jwk
51
	 * @return self
52
	 */
53 2
	public static function fromPublicKey(RSAPublicKeyJWK $jwk) {
54 2
		return new static($jwk);
55
	}
56
	
57
	/**
58
	 * Initialize from a private key.
59
	 *
60
	 * @param RSAPrivateKeyJWK $jwk
61
	 * @return self
62
	 */
63 8
	public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) {
64 8
		return new static($jwk->publicKey(), $jwk);
65
	}
66
	
67
	/**
68
	 *
69
	 * @param JWK $jwk
70
	 * @param Header $header
71
	 * @throws \UnexpectedValueException
72
	 * @return RSASSAPKCS1Algorithm
73
	 */
74 5 View Code Duplication
	public static function fromJWK(JWK $jwk, Header $header) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
75 5
		$alg = JWA::deriveAlgorithmName($header, $jwk);
76 5
		if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
77 1
			throw new \UnexpectedValueException("Unsupported algorithm '$alg'.");
78
		}
79 4
		$cls = self::MAP_ALGO_TO_CLASS[$alg];
80 4
		if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) {
81 2
			return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk));
82
		}
83 2
		return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk));
84
	}
85
	
86 1
	public function headerParameters() {
87 1
		return array(AlgorithmParameter::fromAlgorithm($this));
88
	}
89
}
90