Skip to content

Commit eafbd39

Browse files
authored
Merge pull request #67 from Art4/improve-typed-properties
Improve for typed properties and return types
2 parents 78ecca1 + a51d753 commit eafbd39

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+660
-194
lines changed

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ jobs:
4949
run: composer install --no-progress --prefer-dist --optimize-autoloader
5050

5151
- name: Run PHPStan
52-
run: vendor/bin/phpstan.phar analyze src --level 6
52+
run: vendor/bin/phpstan.phar analyze src --level 8
5353

5454
- run: echo "🍏 This job's status is ${{ job.status }}."

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased](https://github.com/Art4/json-api-client/compare/1.0.0...v1.x)
99

10+
### Added
11+
12+
- Added type hints for parameters and return types in internal and final classes
13+
- New tests for improving backward compatibility in interfaces
14+
15+
### Changed
16+
17+
- Switched from Travis-CI to Github Actions
18+
1019
## [1.0.0 - 2021-03-05](https://github.com/Art4/json-api-client/compare/0.10.2...1.0.0)
1120

1221
### Added

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<testsuite name="all">
1010
<directory suffix="Test.php">tests/Unit/</directory>
1111
<directory suffix="Test.php">tests/Functional/</directory>
12+
<directory suffix="Test.php">tests/BC/</directory>
1213
</testsuite>
1314
<testsuite name="unit">
1415
<directory suffix="Test.php">tests/Unit/</directory>

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: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,14 @@ abstract class AbstractElement implements Accessable, Element
3232
{
3333
use AccessableTrait;
3434

35-
/**
36-
* @var \Art4\JsonApiClient\Manager
37-
*/
38-
private $manager;
35+
private Manager $manager;
3936

40-
/**
41-
* @var \Art4\JsonApiClient\Accessable
42-
*/
43-
private $parent;
37+
private Accessable $parent;
4438

4539
/**
4640
* Sets the manager and parent
4741
*
48-
* @param mixed $data The data for this Element
49-
* @param \Art4\JsonApiClient\Manager $manager The manager
50-
* @param \Art4\JsonApiClient\Accessable $parent The parent
42+
* @param mixed $data The data for this Element
5143
*/
5244
public function __construct($data, Manager $manager, Accessable $parent)
5345
{
@@ -59,33 +51,26 @@ public function __construct($data, Manager $manager, Accessable $parent)
5951

6052
/**
6153
* Returns the Manager
62-
*
63-
* @return \Art4\JsonApiClient\Manager
6454
*/
65-
protected function getManager()
55+
protected function getManager(): Manager
6656
{
6757
return $this->manager;
6858
}
6959

7060
/**
7161
* Get the parent
72-
*
73-
* @return \Art4\JsonApiClient\Accessable
7462
*/
75-
protected function getParent()
63+
protected function getParent(): Accessable
7664
{
7765
return $this->parent;
7866
}
7967

8068
/**
8169
* Create an element
8270
*
83-
* @param mixed $name
8471
* @param mixed $data
85-
*
86-
* @return \Art4\JsonApiClient\Accessable
8772
*/
88-
protected function create($name, $data)
73+
protected function create(string $name, $data): Accessable
8974
{
9075
return $this->getManager()->getFactory()->make(
9176
$name,
@@ -97,8 +82,6 @@ protected function create($name, $data)
9782
* Parse the data
9883
*
9984
* @param mixed $data
100-
*
101-
* @return void
10285
*/
103-
abstract protected function parse($data);
86+
abstract protected function parse($data): void;
10487
}

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/RequestStringInput.php

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

2020
namespace Art4\JsonApiClient\Input;
2121

22+
use Art4\JsonApiClient\Exception\InputException;
23+
2224
/**
2325
* Handles a http Request body as string
2426
*/
@@ -33,7 +35,7 @@ final class RequestStringInput implements Input, RequestInput
3335
*
3436
* @param string $string
3537
*
36-
* @throws \Art4\JsonApiClient\Exception\InputException if $string is not a string
38+
* @throws InputException if $string is not a string
3739
*/
3840
public function __construct($string)
3941
{
@@ -43,15 +45,19 @@ public function __construct($string)
4345
/**
4446
* Get the input as simple object
4547
*
46-
* This should be a native PH stdClass object, so Manager could
48+
* This should be a native PHP stdClass object, so Manager could
4749
* iterate over all public attributes
4850
*
49-
* @throws \Art4\JsonApiClient\Exception\InputException if something went wrong with the input
50-
*
51-
* @return \stdClass
51+
* @throws InputException if something went wrong with the input
5252
*/
53-
public function getAsObject()
53+
public function getAsObject(): \stdClass
5454
{
55-
return $this->decodeJson($this->rawString);
55+
$data = $this->decodeJson($this->rawString);
56+
57+
if (! $data instanceof \stdClass) {
58+
throw new InputException('JSON must contain an object (e.g. `{}`).');
59+
}
60+
61+
return $data;
5662
}
5763
}

0 commit comments

Comments
 (0)