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.

CustomFieldsTrait::getCustomField()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace prgTW\BaseCRM\Service\Behavior;
4
5
use Instantiator\Exception\InvalidArgumentException;
6
use prgTW\BaseCRM\Resource\CustomField;
7
use prgTW\BaseCRM\Resource\CustomFieldsCollection;
8
use prgTW\BaseCRM\Resource\DetachedResource;
9
use prgTW\BaseCRM\Resource\LazyLoadedResource;
10
use prgTW\BaseCRM\Service\Enum\CustomFields;
11
12
/**
13
 * @property-read array $data
14
 */
15
trait CustomFieldsTrait
16
{
17
	/**
18
	 * @param string $name
19
	 *
20
	 * @return bool
21
	 *
22
	 * @throws \InvalidArgumentException when name is not string
23
	 */
24
	public function hasCustomField($name)
25
	{
26
		if (false === is_string($name))
27
		{
28
			throw new InvalidArgumentException('fieldName is not string');
29
		}
30
31
		if ($this instanceof LazyLoadedResource)
32
		{
33
			$this->lazyLoadIfNecessary();
0 ignored issues
show
Bug introduced by
The method lazyLoadIfNecessary() cannot be called from this context as it is declared protected in class prgTW\BaseCRM\Resource\LazyLoadedResource.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
34
		}
35
36
		$key = $this->getCustomFieldsKey();
37
38
		if (false === array_key_exists($key, $this->data))
39
		{
40
			return false;
41
		}
42
43
		return array_key_exists($name, $this->data[$key]);
44
	}
45
46
	/**
47
	 * @param string $name
48
	 *
49
	 * @return null|CustomField
50
	 * @throws \InvalidArgumentException when name is not string
51
	 */
52
	public function getCustomField($name)
53
	{
54
		if (false === is_string($name))
55
		{
56
			throw new InvalidArgumentException('fieldName is not string');
57
		}
58
59
		if (false === $this->hasCustomField($name))
60
		{
61
			return null;
62
		}
63
64
		$key = $this->getCustomFieldsKey();
65
66
		return new CustomField($name, $this->data[$key][$name]);
67
	}
68
69
	/**
70
	 * @param string $name
71
	 * @param mixed  $value
72
	 */
73
	public function setCustomField($name, $value)
74
	{
75
		if ($this instanceof LazyLoadedResource)
76
		{
77
			$this->lazyLoadIfNecessary();
0 ignored issues
show
Bug introduced by
The method lazyLoadIfNecessary() cannot be called from this context as it is declared protected in class prgTW\BaseCRM\Resource\LazyLoadedResource.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
78
		}
79
80
		$customField             = new CustomField($name, $value);
81
		$key                     = $this->getCustomFieldsKey();
82
		$this->data[$key][$name] = $customField->getData();
83
	}
84
85
	/**
86
	 * @return null|CustomFieldsCollection
87
	 */
88
	public function getCustomFields()
89
	{
90
		if ($this instanceof LazyLoadedResource)
91
		{
92
			$this->lazyLoadIfNecessary();
0 ignored issues
show
Bug introduced by
The method lazyLoadIfNecessary() cannot be called from this context as it is declared protected in class prgTW\BaseCRM\Resource\LazyLoadedResource.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
93
		}
94
95
		$key = $this->getCustomFieldsKey();
96
97
		if (false === array_key_exists($key, $this->data))
98
		{
99
			return new CustomFieldsCollection([]);
100
		}
101
102
		$customFields = [];
103
		foreach (array_keys($this->data[$key]) as $fieldName)
104
		{
105
			$customFields[$fieldName] = $this->getCustomField($fieldName);
106
		}
107
108
		return new CustomFieldsCollection($customFields);
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	protected function getCustomFieldsKey()
115
	{
116
		return $this instanceof DetachedResource ? CustomFields::KEY_CREATING : CustomFields::KEY_FETCHING;
117
	}
118
}
119