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.

BaseResource::dehydrate()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 33
Code Lines 15

Duplication

Lines 5
Ratio 15.15 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 5
loc 33
rs 8.439
cc 6
eloc 15
nc 5
nop 1
1
<?php
2
3
namespace prgTW\BaseCRM\Resource;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use prgTW\BaseCRM\Exception\ResourceException;
7
8
abstract class BaseResource
9
{
10
	const ENDPOINT_APP    = 'https://app.futuresimple.com';
11
	const ENDPOINT_COMMON = 'https://common.futuresimple.com';
12
	const ENDPOINT_CORE   = 'https://core.futuresimple.com';
13
	const ENDPOINT_CRM    = 'https://crm.futuresimple.com';
14
	const ENDPOINT_LEADS  = 'https://leads.futuresimple.com';
15
	const ENDPOINT_SALES  = 'https://sales.futuresimple.com';
16
	const ENDPOINT_TAGS   = 'https://tags.futuresimple.com';
17
18
	const PREFIX = 'api/v1';
19
20
	/** @var array */
21
	protected $data = null;
22
23
	/**
24
	 * @param string $resourceClassName Fully classified class name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $resourceClassName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
25
	 *
26
	 * @return string
27
	 */
28
	protected function getClassNameOnly($resourceClassName = null)
29
	{
30
		$name = null === $resourceClassName ? static::class : $resourceClassName;
31
		$name = explode('\\', $name);
32
		$name = end($name);
33
34
		return $name;
35
	}
36
37
	/**
38
	 * @param string $resourceClassName Fully classified class name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $resourceClassName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
	 *
40
	 * @return string
41
	 */
42
	public function getResourceName($resourceClassName = null)
43
	{
44
		$name = $this->getClassNameOnly($resourceClassName);
45
46
		return Inflector::tableize($name);
47
	}
48
49
	/**
50
	 * @param string $resourceClassName Fully classified class name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $resourceClassName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
	 *
52
	 * @return string
53
	 */
54
	protected function getChildResourceName($resourceClassName = null)
55
	{
56
		$resourceName = $this->getResourceName($resourceClassName);
57
		$singular     = Inflector::singularize($resourceName);
58
59
		return $singular;
60
	}
61
62
	/**
63
	 * @param array $data
64
	 */
65
	protected function hydrate(array $data)
66
	{
67
		$this->data = [];
68 View Code Duplication
		foreach ($data as $key => $value)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
		{
70
			$setter = sprintf('set%s', ucfirst(Inflector::camelize($key)));
71
			if (method_exists($this, $setter))
72
			{
73
				$this->$setter($value);
74
			}
75
			else
76
			{
77
				$this->data[$key] = $value;
78
			}
79
		}
80
81
		$this->postHydrate($data);
82
	}
83
84
	/**
85
	 * @param array $fieldNames
86
	 *
87
	 * @throws ResourceException when dehydration has been stopped
88
	 * @return array
89
	 */
90
	protected function dehydrate(array $fieldNames = [])
91
	{
92
		$result = $this->preDehydrate($fieldNames);
93
		if (false === $result)
94
		{
95
			//@codeCoverageIgnoreStart
96
			throw new ResourceException('Dehydration has been stopped');
97
			//@codeCoverageIgnoreEnd
98
		}
99
100
		if (is_array($result))
101
		{
102
			$fieldNames = $result;
103
		}
104
105
		$data = [];
106
		if ([] === $fieldNames)
107
		{
108
			$data = $this->data;
109
		}
110
		else
111
		{
112 View Code Duplication
			foreach ($fieldNames as $fieldName)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
			{
114
				$getter           = sprintf('get%s', ucfirst(Inflector::camelize($fieldName)));
115
				$data[$fieldName] = method_exists($this, $getter) ? $this->$getter() : $this->data[$fieldName];
116
			}
117
		}
118
119
		$this->postDehydrate($data);
120
121
		return $data;
122
	}
123
124
	/**
125
	 * @param string $name
126
	 *
127
	 * @return bool
128
	 */
129 View Code Duplication
	public function __isset($name)
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...
130
	{
131
		$issetter = sprintf('isset%s', ucfirst(Inflector::camelize($name)));
132
		if (method_exists($this, $issetter))
133
		{
134
			return $this->$issetter();
135
		}
136
137
		$key = Inflector::tableize($name);
138
		if (array_key_exists($key, $this->data))
139
		{
140
			return true;
141
		}
142
143
		return isset($this->$name);
144
	}
145
146
	/**
147
	 * @param string $name
148
	 *
149
	 * @return mixed
150
	 */
151 View Code Duplication
	public function __get($name)
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...
152
	{
153
		$getter = sprintf('get%s', ucfirst(Inflector::camelize($name)));
154
		if (method_exists($this, $getter))
155
		{
156
			return $this->$getter();
157
		}
158
159
		$key = Inflector::tableize($name);
160
		if (array_key_exists($key, $this->data))
161
		{
162
			return $this->data[$key];
163
		}
164
165
		return $this->$name;
166
	}
167
168
	/**
169
	 * @param string $name
170
	 * @param mixed  $value
171
	 */
172
	public function __set($name, $value)
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...
173
	{
174
		$setter = sprintf('set%s', ucfirst(Inflector::camelize($name)));
175
		if (method_exists($this, $setter))
176
		{
177
			return $this->$setter($value);
178
		}
179
180
		$key              = Inflector::tableize($name);
181
		$this->data[$key] = $value;
182
	}
183
184
	/**
185
	 * @param array $data
186
	 *
187
	 * @codeCoverageIgnore
188
	 */
189
	protected function postHydrate(array $data)
190
	{
191
192
	}
193
194
	/**
195
	 * @param array $fieldNames
196
	 *
197
	 * @return array|bool False: stop dehydration, array: new field names
0 ignored issues
show
Documentation introduced by
Should the return type not be array|boolean|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
198
	 *
199
	 * @codeCoverageIgnore
200
	 */
201
	protected function preDehydrate(array $fieldNames)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldNames is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
	{
203
204
	}
205
206
	/**
207
	 * @param array $data
208
	 *
209
	 * @codeCoverageIgnore
210
	 */
211
	protected function postDehydrate(array &$data)
212
	{
213
214
	}
215
}
216