Skip to content

Commit 3eb15a8

Browse files
committed
Add typed parameters and return types in internal and final classes
1 parent af127a0 commit 3eb15a8

30 files changed

+55
-116
lines changed

src/Element.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ interface Element
2727
/**
2828
* Sets the manager and parent
2929
*
30-
* @param mixed $data The data for this Element
31-
* @param \Art4\JsonApiClient\Manager $manager The manager
32-
* @param \Art4\JsonApiClient\Accessable $parent The parent
30+
* @param mixed $data The data for this Element
3331
*/
3432
public function __construct($data, Manager $manager, Accessable $parent);
3533
}

src/Helper/AbstractElement.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ abstract class AbstractElement implements Accessable, Element
3939
/**
4040
* Sets the manager and parent
4141
*
42-
* @param mixed $data The data for this Element
43-
* @param \Art4\JsonApiClient\Manager $manager The manager
44-
* @param \Art4\JsonApiClient\Accessable $parent The parent
42+
* @param mixed $data The data for this Element
4543
*/
4644
public function __construct($data, Manager $manager, Accessable $parent)
4745
{
@@ -53,33 +51,26 @@ public function __construct($data, Manager $manager, Accessable $parent)
5351

5452
/**
5553
* Returns the Manager
56-
*
57-
* @return \Art4\JsonApiClient\Manager
5854
*/
59-
protected function getManager()
55+
protected function getManager(): Manager
6056
{
6157
return $this->manager;
6258
}
6359

6460
/**
6561
* Get the parent
66-
*
67-
* @return \Art4\JsonApiClient\Accessable
6862
*/
69-
protected function getParent()
63+
protected function getParent(): Accessable
7064
{
7165
return $this->parent;
7266
}
7367

7468
/**
7569
* Create an element
7670
*
77-
* @param mixed $name
7871
* @param mixed $data
79-
*
80-
* @return \Art4\JsonApiClient\Accessable
8172
*/
82-
protected function create($name, $data)
73+
protected function create(string $name, $data): Accessable
8374
{
8475
return $this->getManager()->getFactory()->make(
8576
$name,
@@ -91,8 +82,6 @@ protected function create($name, $data)
9182
* Parse the data
9283
*
9384
* @param mixed $data
94-
*
95-
* @return void
9685
*/
97-
abstract protected function parse($data);
86+
abstract protected function parse($data): void;
9887
}

src/Helper/AccessKey.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class AccessKey extends SplStack
3535
*
3636
* @return AccessKey<string>
3737
*/
38-
public static function create($key)
38+
public static function create($key): AccessKey
3939
{
4040
// Ignore arrays and objects
4141
if (is_object($key) or is_array($key)) {
@@ -61,14 +61,12 @@ public static function create($key)
6161
/**
6262
* @var string Raw key
6363
*/
64-
public $raw = '';
64+
public string $raw = '';
6565

6666
/**
6767
* Transforms the Key to a string
68-
*
69-
* @return string
7068
*/
71-
public function __toString()
69+
public function __toString(): string
7270
{
7371
return $this->raw;
7472
}

src/Helper/AccessableTrait.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,24 @@ trait AccessableTrait
3737
/**
3838
* Set a value
3939
*
40-
* @param string $key The Key
4140
* @param mixed $value The Value
42-
*
43-
* @return self
4441
*/
45-
protected function set($key, $value)
42+
final protected function set(string $key, $value): void
4643
{
4744
// Allow non-associative array for collections
4845
if ($key === '') {
4946
$this->data[] = $value;
5047
} else {
5148
$this->data[$key] = $value;
5249
}
53-
54-
return $this;
5550
}
5651

5752
/**
5853
* Returns the keys of all setted values
5954
*
6055
* @return array<string> Keys of all setted values
6156
*/
62-
public function getKeys()
57+
final public function getKeys()
6358
{
6459
return array_keys($this->data);
6560
}
@@ -71,7 +66,7 @@ public function getKeys()
7166
*
7267
* @return bool
7368
*/
74-
public function has($key)
69+
final public function has($key)
7570
{
7671
$key = $this->parseKey($key);
7772

@@ -90,7 +85,7 @@ public function has($key)
9085

9186
// #TODO Handle other objects and arrays
9287
if (! $value instanceof Accessable) {
93-
//throw new AccessException('The existance for the key "' . $key->raw . '" could\'nt be checked.');
88+
// throw new AccessException('The existance for the key "' . $key->raw . '" could\'nt be checked.');
9489
return false;
9590
}
9691

@@ -128,11 +123,11 @@ public function get($key)
128123
/**
129124
* Get a value by the key
130125
*
131-
* @param string $key The key of the value
126+
* @throws AccessException
132127
*
133128
* @return mixed The value
134129
*/
135-
private function getValue($key)
130+
private function getValue(string $key)
136131
{
137132
if (array_key_exists($key, $this->data)) {
138133
return $this->data[$key];
@@ -148,7 +143,7 @@ private function getValue($key)
148143
*
149144
* @return AccessKey<string> The parsed key
150145
*/
151-
private function parseKey($key)
146+
private function parseKey($key): AccessKey
152147
{
153148
if (is_object($key) and $key instanceof AccessKey) {
154149
return $key;

src/Helper/Parser.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Art4\JsonApiClient\Helper;
2121

22+
use Art4\JsonApiClient\Accessable;
2223
use Art4\JsonApiClient\Exception\Exception;
2324
use Art4\JsonApiClient\Input\RequestStringInput;
2425
use Art4\JsonApiClient\Input\ResponseStringInput;
@@ -31,29 +32,21 @@
3132
final class Parser
3233
{
3334
/**
34-
* @param string $jsonString
35-
*
36-
* @throws \Art4\JsonApiClient\Exception\ValidationException If $jsonString contains invalid JSON API
3735
* @throws \Art4\JsonApiClient\Exception\InputException if something went wrong with the input
38-
*
39-
* @return \Art4\JsonApiClient\Accessable
36+
* @throws \Art4\JsonApiClient\Exception\ValidationException If $jsonString contains invalid JSON API
4037
*/
41-
public static function parseResponseString($jsonString)
38+
public static function parseResponseString(string $jsonString): Accessable
4239
{
4340
$manager = new ErrorAbortManager(new Factory());
4441

4542
return $manager->parse(new ResponseStringInput($jsonString));
4643
}
4744

4845
/**
49-
* @param string $jsonString
50-
*
51-
* @throws \Art4\JsonApiClient\Exception\ValidationException If $jsonString contains invalid JSON API
5246
* @throws \Art4\JsonApiClient\Exception\InputException if something went wrong with the input
53-
*
54-
* @return \Art4\JsonApiClient\Accessable
47+
* @throws \Art4\JsonApiClient\Exception\ValidationException If $jsonString contains invalid JSON API
5548
*/
56-
public static function parseRequestString($jsonString)
49+
public static function parseRequestString(string $jsonString): Accessable
5750
{
5851
$manager = new ErrorAbortManager(new Factory());
5952

@@ -62,12 +55,8 @@ public static function parseRequestString($jsonString)
6255

6356
/**
6457
* Checks if a string is a valid JSON API response body
65-
*
66-
* @param string $jsonString
67-
*
68-
* @return bool true, if $jsonString contains valid JSON API, else false
6958
*/
70-
public static function isValidResponseString($jsonString)
59+
public static function isValidResponseString(string $jsonString): bool
7160
{
7261
try {
7362
static::parseResponseString($jsonString);
@@ -80,12 +69,8 @@ public static function isValidResponseString($jsonString)
8069

8170
/**
8271
* Checks if a string is a valid JSON API request body
83-
*
84-
* @param string $jsonString
85-
*
86-
* @return bool true, if $jsonString contains valid JSON API, else false
8772
*/
88-
public static function isValidRequestString($jsonString)
73+
public static function isValidRequestString(string $jsonString): bool
8974
{
9075
try {
9176
static::parseRequestString($jsonString);

src/Input/RequestInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace Art4\JsonApiClient\Input;
2121

2222
/**
23-
* Input Interface
23+
* Request Input Interface
2424
*/
2525
interface RequestInput
2626
{

src/Input/StringInputTrait.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ trait StringInputTrait
3535
* @param string $string
3636
*
3737
* @throws InputException if $string is not a string
38-
*
39-
* @return string
4038
*/
41-
public function prepareString($string)
39+
final public function prepareString($string): string
4240
{
4341
if (! is_string($string)) {
4442
throw new InputException(sprintf(
@@ -53,13 +51,11 @@ public function prepareString($string)
5351
/**
5452
* Decodes a json string
5553
*
56-
* @param string $jsonString
57-
*
5854
* @throws InputException if something went wrong with the input
5955
*
6056
* @return mixed
6157
*/
62-
protected function decodeJson($jsonString)
58+
final protected function decodeJson(string $jsonString)
6359
{
6460
$jsonErrors = [
6561
\JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',

src/Manager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ interface Manager
2929
/**
3030
* Parse the input
3131
*
32-
* @param \Art4\JsonApiClient\Input\Input $input
33-
*
3432
* @throws \Art4\JsonApiClient\Exception\InputException If $input contains invalid JSON API
3533
* @throws \Art4\JsonApiClient\Exception\ValidationException If $input contains invalid JSON API
3634
*

src/Manager/ErrorAbortManager.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Art4\JsonApiClient\Manager;
2121

22+
use Art4\JsonApiClient\Accessable;
2223
use Art4\JsonApiClient\Exception\InputException;
2324
use Art4\JsonApiClient\Exception\ValidationException;
2425
use Art4\JsonApiClient\Factory;
@@ -44,10 +45,6 @@ final class ErrorAbortManager implements Manager
4445

4546
/**
4647
* Create a Manager
47-
*
48-
* @param \Art4\JsonApiClient\Factory $factory
49-
*
50-
* @return object
5148
*/
5249
public function __construct(Factory $factory)
5350
{
@@ -57,14 +54,10 @@ public function __construct(Factory $factory)
5754
/**
5855
* Parse the input
5956
*
60-
* @param \Art4\JsonApiClient\Input\Input $input
61-
*
6257
* @throws \Art4\JsonApiClient\Exception\InputException If $input contains invalid JSON API
6358
* @throws \Art4\JsonApiClient\Exception\ValidationException If $input contains invalid JSON API
64-
*
65-
* @return \Art4\JsonApiClient\Accessable
6659
*/
67-
public function parse(Input $input)
60+
public function parse(Input $input): Accessable
6861
{
6962
// fill config
7063
$this->config = $this->default;
@@ -88,10 +81,8 @@ public function parse(Input $input)
8881

8982
/**
9083
* Get a factory from the manager
91-
*
92-
* @return \Art4\JsonApiClient\Factory
9384
*/
94-
public function getFactory()
85+
public function getFactory(): Factory
9586
{
9687
return $this->factory;
9788
}

src/Serializer/ArraySerializer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ public function __construct(array $params = [])
4646
/**
4747
* Convert data in an array
4848
*
49-
* @param \Art4\JsonApiClient\Accessable $data The data for serialization
50-
*
5149
* @return array<string, mixed>|null
5250
*/
53-
public function serialize(Accessable $data)
51+
public function serialize(Accessable $data): ?array
5452
{
5553
$fullArray = (bool) $this->config['recursive'];
5654

0 commit comments

Comments
 (0)