Skip to content

Commit ac70da0

Browse files
committed
replace simple type validation with data type declarations #11
1 parent e011f61 commit ac70da0

21 files changed

+137
-441
lines changed

src/AbstractFunction.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ public function __construct($name, array $params = null, IConfiguration $config
103103
* @param string $name
104104
* @throws InvalidArgumentException
105105
*/
106-
private function setName($name)
106+
private function setName(string $name)
107107
{
108-
if (!is_string($name) || empty(trim($name))) {
108+
if (trim($name) === '') {
109109
throw new InvalidArgumentException(
110110
'Missing or malformed SAP remote function name'
111111
);
112112
}
113-
$this->name = $name;
113+
$this->name = trim($name);
114114
}
115115

116116
/**
@@ -193,7 +193,7 @@ public function setApi(IApi $api): IFunction
193193
* @return array|bool|float|int|string
194194
* @throws InvalidArgumentException
195195
*/
196-
public function getParam($key)
196+
public function getParam(string $key)
197197
{
198198
return $this->get($key);
199199
}
@@ -209,12 +209,12 @@ public function getParams(): array
209209

210210
/**
211211
* Set a single SAP remote function call parameter.
212-
* @param string $key Name of the parameter to set.
212+
* @param string $key Name of the parameter to set.
213213
* @param bool|int|float|string|array $value Value of the parameter.
214214
* @return $this
215215
* @throws InvalidArgumentException
216216
*/
217-
public function setParam($key, $value): AbstractFunction
217+
public function setParam(string $key, $value): AbstractFunction
218218
{
219219
$this->set($key, $value);
220220
return $this;
@@ -282,7 +282,7 @@ public function jsonSerialize(): array
282282
* @throws IUnknownFunctionException
283283
* @throws InvalidArgumentException
284284
*/
285-
public static function jsonDecode($json): IJsonSerializable
285+
public static function jsonDecode(string $json): IJsonSerializable
286286
{
287287
$array = static::jsonToArray($json);
288288
if (

src/Api/Element.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Element extends JsonSerializable implements IElement
5050
* @param string $name API element name.
5151
* @throws InvalidArgumentException
5252
*/
53-
public function __construct($type, $name)
53+
public function __construct(string $type, string $name)
5454
{
5555
parent::__construct();
5656
$this->setType($type);
@@ -88,13 +88,8 @@ public function getName(): string
8888
* @param string $type
8989
* @throws InvalidArgumentException
9090
*/
91-
protected function setType($type)
91+
protected function setType(string $type)
9292
{
93-
if (!is_string($type)) {
94-
throw new InvalidArgumentException(
95-
'Expected API element type to be string!'
96-
);
97-
}
9893
if (!in_array($type, static::$allowedTypes, true)) {
9994
throw new InvalidArgumentException(sprintf(
10095
'Expected API element type to be in: %s!',
@@ -109,14 +104,14 @@ protected function setType($type)
109104
* @param string $name
110105
* @throws InvalidArgumentException
111106
*/
112-
protected function setName($name)
107+
protected function setName(string $name)
113108
{
114-
if (!is_string($name) || $name === '') {
109+
if (trim($name) === '') {
115110
throw new InvalidArgumentException(
116111
'Expected API element name to be string!'
117112
);
118113
}
119-
$this->set(self::JSON_NAME, $name);
114+
$this->set(self::JSON_NAME, trim($name));
120115
}
121116

122117
/**
@@ -169,25 +164,19 @@ public function cast($value)
169164
* @return Element
170165
* @throws InvalidArgumentException
171166
*/
172-
public static function fromArray($array): Element
167+
public static function fromArray(array $array): Element
173168
{
174169
static::fromArrayValidation($array);
175170
return new self($array[self::JSON_TYPE], $array[self::JSON_NAME]);
176171
}
177172

178173
/**
179174
* Validate the array for fromArray().
180-
* @param mixed $array
175+
* @param array $array
181176
* @throws InvalidArgumentException
182177
*/
183-
protected static function fromArrayValidation($array)
178+
protected static function fromArrayValidation(array $array)
184179
{
185-
if (!is_array($array)) {
186-
throw new InvalidArgumentException(sprintf(
187-
'Expected array, but got \'%s\'!',
188-
gettype($array)
189-
));
190-
}
191180
foreach (static::$allowedKeys as $key) {
192181
if (!array_key_exists($key, $array)) {
193182
throw new InvalidArgumentException(sprintf(
@@ -205,7 +194,7 @@ protected static function fromArrayValidation($array)
205194
* @return Element
206195
* @throws InvalidArgumentException
207196
*/
208-
public static function jsonDecode($json): IJsonSerializable
197+
public static function jsonDecode(string $json): IJsonSerializable
209198
{
210199
$array = static::jsonToArray($json);
211200
return static::fromArray($array);

src/Api/RemoteApi.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getTables(): array
7070
* @return array
7171
* @throws InvalidArgumentException
7272
*/
73-
protected function getValues($direction): array
73+
protected function getValues(string $direction): array
7474
{
7575
$result = [];
7676
foreach ($this->data as $value) {
@@ -89,14 +89,8 @@ protected function getValues($direction): array
8989
* @param array|null $values Array of remote API elements. Default: null
9090
* @throws InvalidArgumentException
9191
*/
92-
public function __construct($values = null)
92+
public function __construct(array $values = [])
9393
{
94-
if ($values === null) {
95-
$values = [];
96-
}
97-
if (!is_array($values)) {
98-
throw new InvalidArgumentException('Expected array of API values.');
99-
}
10094
foreach ($values as $value) {
10195
/**
10296
* Call type-specific constructors from the array.
@@ -111,7 +105,7 @@ public function __construct($values = null)
111105
* @return IValue
112106
* @throws InvalidArgumentException
113107
*/
114-
private function constructValue($value): IValue
108+
private function constructValue(array $value): IValue
115109
{
116110
if (!array_key_exists(Value::JSON_TYPE, $value)) {
117111
throw new InvalidArgumentException('API Value is missing type.');
@@ -131,19 +125,17 @@ private function constructValue($value): IValue
131125
* @return RemoteApi
132126
* @throws InvalidArgumentException
133127
*/
134-
public static function jsonDecode($json): IJsonSerializable
128+
public static function jsonDecode(string $json): IJsonSerializable
135129
{
136-
if (is_string($json)) {
137-
$array = json_decode($json, true);
138-
if (is_array($array)) {
139-
try {
140-
return new self($array);
141-
} catch (InvalidArgumentException $exception) {
142-
throw new InvalidArgumentException(sprintf(
143-
'Invalid JSON! %s',
144-
$exception->getMessage()
145-
));
146-
}
130+
$array = json_decode($json, true);
131+
if (is_array($array)) {
132+
try {
133+
return new self($array);
134+
} catch (InvalidArgumentException $exception) {
135+
throw new InvalidArgumentException(sprintf(
136+
'Invalid JSON! %s',
137+
$exception->getMessage()
138+
));
147139
}
148140
}
149141
throw new InvalidArgumentException(sprintf(

src/Api/Struct.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function getAllowedKeys(): array
5454
* @param array $members Array of Elements as the members of the struct.
5555
* @throws InvalidArgumentException
5656
*/
57-
public function __construct($name, $direction, $isOptional, $members)
57+
public function __construct(string $name, string $direction, bool $isOptional, array $members)
5858
{
5959
parent::__construct(self::TYPE_STRUCT, $name, $direction, $isOptional);
6060
$this->setMembers($members);
@@ -104,13 +104,8 @@ public function getMembers(): array
104104
* @param array $members
105105
* @throws InvalidArgumentException
106106
*/
107-
protected function setMembers($members)
107+
protected function setMembers(array $members)
108108
{
109-
if (!is_array($members)) {
110-
throw new InvalidArgumentException(
111-
'Expected API struct members to be in an array!'
112-
);
113-
}
114109
foreach ($members as $member) {
115110
if (!$member instanceof IElement) {
116111
throw new InvalidArgumentException(
@@ -128,7 +123,7 @@ protected function setMembers($members)
128123
* @return Struct
129124
* @throws InvalidArgumentException
130125
*/
131-
public static function fromArray($array): Struct
126+
public static function fromArray(array $array): Struct
132127
{
133128
static::fromArrayValidation($array);
134129
if ($array[self::JSON_TYPE] !== self::TYPE_STRUCT) {

src/Api/Table.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Table extends Value implements ITable
5353
* @param array $members Array of Elements as the columns of the table.
5454
* @throws InvalidArgumentException
5555
*/
56-
public function __construct($name, $direction, $isOptional, $members)
56+
public function __construct(string $name, string $direction, bool $isOptional, array $members)
5757
{
5858
parent::__construct(self::TYPE_TABLE, $name, $direction, $isOptional);
5959
$this->setMembers($members);
@@ -68,6 +68,14 @@ public function __construct($name, $direction, $isOptional, $members)
6868
*/
6969
public function cast($value): array
7070
{
71+
if (!is_array($value)) {
72+
throw new InvalidArgumentException(
73+
sprintf(
74+
'Expected table cast to be array, %s given!',
75+
gettype($value)
76+
)
77+
);
78+
}
7179
foreach ($value as &$row) {
7280
foreach ($this->getMembers() as $member) {
7381
/**
@@ -106,13 +114,8 @@ public function getMembers(): array
106114
* @param array $members
107115
* @throws InvalidArgumentException
108116
*/
109-
protected function setMembers($members)
117+
protected function setMembers(array $members)
110118
{
111-
if (!is_array($members)) {
112-
throw new InvalidArgumentException(
113-
'Expected API table members to be in an array!'
114-
);
115-
}
116119
foreach ($members as $member) {
117120
if (!$member instanceof IElement) {
118121
throw new InvalidArgumentException(
@@ -130,7 +133,7 @@ protected function setMembers($members)
130133
* @return Table
131134
* @throws InvalidArgumentException
132135
*/
133-
public static function fromArray($array): Table
136+
public static function fromArray(array $array): Table
134137
{
135138
static::fromArrayValidation($array);
136139
if ($array[self::JSON_DIRECTION] !== self::DIRECTION_TABLE) {

src/Api/Value.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class Value extends Element implements IValue
4040
* @param string $type Either string, int, float, bool or array
4141
* @param string $name API value name.
4242
* @param string $direction Either input, output or table.
43-
* @param bool $isOptional Is the API value optional?
43+
* @param bool $isOptional Is the API value optional?
4444
* @throws InvalidArgumentException
4545
*/
46-
public function __construct($type, $name, $direction, $isOptional)
46+
public function __construct(string $type, string $name, string $direction, bool $isOptional)
4747
{
4848
parent::__construct($type, $name);
4949
$this->setDirection($direction);
@@ -81,13 +81,8 @@ public function isOptional(): bool
8181
* @param string $direction
8282
* @throws InvalidArgumentException
8383
*/
84-
protected function setDirection($direction)
84+
protected function setDirection(string $direction)
8585
{
86-
if (!is_string($direction)) {
87-
throw new InvalidArgumentException(
88-
'Expected API value direction to be string!'
89-
);
90-
}
9186
if (!in_array($direction, static::$allowedDirections, true)) {
9287
throw new InvalidArgumentException(sprintf(
9388
'Expected API value direction to be in: %s!',
@@ -102,13 +97,8 @@ protected function setDirection($direction)
10297
* @param bool $isOptional
10398
* @throws InvalidArgumentException
10499
*/
105-
protected function setOptional($isOptional)
100+
protected function setOptional(bool $isOptional)
106101
{
107-
if (!is_bool($isOptional)) {
108-
throw new InvalidArgumentException(
109-
'Expected API value isOptional flag to be boolean!'
110-
);
111-
}
112102
$this->set(self::JSON_OPTIONAL, $isOptional);
113103
}
114104

@@ -118,7 +108,7 @@ protected function setOptional($isOptional)
118108
* @return Value
119109
* @throws InvalidArgumentException
120110
*/
121-
public static function fromArray($array): Value
111+
public static function fromArray(array $array): Value
122112
{
123113
static::fromArrayValidation($array);
124114
return new self(

src/Config/AbstractConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct($config = null)
5555
* @return ConfigTypeA|ConfigTypeB
5656
* @throws InvalidArgumentException
5757
*/
58-
public static function jsonDecode($json): IJsonSerializable
58+
public static function jsonDecode(string $json): IJsonSerializable
5959
{
6060
$config = static::jsonToArray($json);
6161
if (

0 commit comments

Comments
 (0)