Test Failed
Pull Request — main (#64)
by Lode
08:15
created

ProfileAliasManager::getKeyword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace alsvanzelf\jsonapi\helpers;
4
5
use alsvanzelf\jsonapi\exceptions\InputException;
6
use alsvanzelf\jsonapi\helpers\Validator;
7
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
8
use alsvanzelf\jsonapi\objects\ProfileLinkObject;
9
10
abstract class ProfileAliasManager {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ProfileAliasManager
Loading history...
11
	/** @var array */
12
	private $aliasMapping = [];
13
	/** @var array */
14
	private $keywordMapping = [];
15
	
16
	/**
17
	 * ProfileInterface
18
	 */
19
	
20
	/**
0 ignored issues
show
Coding Style introduced by
Parameter $aliases should have a doc-comment as per coding-style.
Loading history...
21
	 * @inheritDoc
22
	 */
23
	public function __construct(array $aliases=[]) {
24
		$officialKeywords = $this->getOfficialKeywords();
25
		if ($officialKeywords === []) {
26
			return;
27
		}
28
		
29
		$this->keywordMapping = array_combine($officialKeywords, $officialKeywords);
30
		if ($aliases === []) {
31
			return;
32
		}
33
		
34
		foreach ($aliases as $keyword => $alias) {
35
			if ($alias === $keyword) {
36
				throw new InputException('an alias should be different from its keyword');
37
			}
38
			if (in_array($keyword, $officialKeywords, $strict=true) === false) {
39
				throw new InputException('unknown keyword "'.$keyword.'" to alias');
40
			}
41
			Validator::checkMemberName($alias);
42
			
43
			$this->keywordMapping[$keyword] = $alias;
44
		}
45
		
46
		$this->aliasMapping = $aliases;
47
	}
48
	
49
	/**
0 ignored issues
show
Coding Style introduced by
Parameter $keyword should have a doc-comment as per coding-style.
Loading history...
50
	 * @inheritDoc
51
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
	public function getKeyword($keyword) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
		if (isset($this->keywordMapping[$keyword]) === false) {
54
			throw new InputException('unknown keyword "'.$keyword.'"');
55
		}
56
		
57
		return $this->keywordMapping[$keyword];
58
	}
59
	
60
	/**
61
	 * @inheritDoc
62
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
63
	abstract public function getOfficialKeywords();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
64
	
65
	/**
66
	 * @inheritDoc
67
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
68
	abstract public function getOfficialLink();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
69
	
70
	/**
71
	 * @inheritDoc
72
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73
	public function getAliasedLink() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
74
		if ($this->aliasMapping === []) {
75
			return $this->getOfficialLink();
76
		}
77
		
78
		return new ProfileLinkObject($this->getOfficialLink(), $this->aliasMapping);
79
	}
80
}
81