Skip to content

Commit 888b2c7

Browse files
authored
Merge pull request #87 from lode/increase-coverage
2 parents d2f48a9 + d994b81 commit 888b2c7

File tree

97 files changed

+826
-519
lines changed

Some content is hidden

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

97 files changed

+826
-519
lines changed

examples/atomic_operations_extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use alsvanzelf\jsonapi\objects\ResourceObject;
66
use alsvanzelf\jsonapi\extensions\AtomicOperationsDocument;
77

8-
require 'bootstrap_examples.php';
8+
require __DIR__.'/bootstrap_examples.php';
99

1010
/**
1111
* use the atomic operations extension as extension to the document

examples/bootstrap_examples.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
1111
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
1212
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
13-
use alsvanzelf\jsonapi\objects\ResourceObject;
1413

1514
ini_set('display_errors', 1);
1615
error_reporting(-1);
1716

1817
require_once __DIR__.'/../vendor/autoload.php';
1918

2019
class ExampleDataset {
21-
private static $records = [
20+
/** @var array<string, array<int, array<string, string|int>>> */
21+
private static array $records = [
2222
'articles' => [
2323
1 => [
2424
'title' => 'JSON:API paints my bikeshed!',
@@ -58,34 +58,43 @@ class ExampleDataset {
5858
],
5959
];
6060

61-
public static function getRecord($type, $id) {
62-
if (!isset(self::$records[$type][$id])) {
61+
/**
62+
* @return array<string, string|int>
63+
*/
64+
public static function getRecord(string $type, int $id): array {
65+
if (isset(self::$records[$type][$id]) === false) {
6366
throw new \Exception('sorry, we have a limited dataset');
6467
}
6568

6669
return self::$records[$type][$id];
6770
}
6871

69-
public static function getEntity($type, $id) {
72+
public static function getEntity(string $type, int $id): ExampleUser {
7073
$record = self::getRecord($type, $id);
7174

7275
$user = new ExampleUser($id);
7376
foreach ($record as $key => $value) {
74-
$user->$key = $value;
77+
$user->$key = $value; // @phpstan-ignore property.dynamicName
7578
}
7679

7780
return $user;
7881
}
7982

80-
public static function findRecords($type) {
83+
/**
84+
* @return array<array<string, string|int>>
85+
*/
86+
public static function findRecords(string $type): array {
8187
return self::$records[$type];
8288
}
8389

84-
public static function findEntities($type) {
85-
$records = self::findRecords($type);
86-
$entities = [];
90+
/**
91+
* @return ExampleUser[]
92+
*/
93+
public static function findEntities(string $type): array {
94+
$recordIds = array_keys(self::findRecords($type));
95+
$entities = [];
8796

88-
foreach ($records as $id => $record) {
97+
foreach ($recordIds as $id) {
8998
$entities[$id] = self::getEntity($type, $id);
9099
}
91100

@@ -94,15 +103,15 @@ public static function findEntities($type) {
94103
}
95104

96105
class ExampleUser {
97-
public $name;
98-
public $heads;
99-
public $unknown;
106+
public ?string $name = null;
107+
public null|int|string $heads = null;
108+
public mixed $unknown = null;
100109

101110
public function __construct(
102-
public $id,
111+
public int $id,
103112
) {}
104113

105-
function getCurrentLocation() {
114+
public function getCurrentLocation(): string {
106115
return 'Earth';
107116
}
108117
}
@@ -124,11 +133,7 @@ public function getNamespace(): string {
124133
* optionally helpers for the specific extension
125134
*/
126135

127-
public function setVersion(ResourceInterface $resource, $version) {
128-
if ($resource instanceof HasExtensionMembersInterface === false) {
129-
throw new \Exception('resource doesn\'t have extension members');
130-
}
131-
136+
public function setVersion(ResourceInterface & HasExtensionMembersInterface $resource, string $version): void {
132137
if ($resource instanceof ResourceDocument) {
133138
$resource->getResource()->addExtensionMember($this, 'id', $version);
134139
}
@@ -151,14 +156,11 @@ public function getOfficialLink(): string {
151156
* optionally helpers for the specific profile
152157
*/
153158

154-
/**
155-
* @param ResourceInterface&HasAttributesInterface $resource
156-
*/
157-
public function setTimestamps(ResourceInterface $resource, ?\DateTimeInterface $created=null, ?\DateTimeInterface $updated=null) {
158-
if ($resource instanceof HasAttributesInterface === false) {
159-
throw new \Exception('cannot add attributes to identifier objects');
160-
}
161-
159+
public function setTimestamps(
160+
ResourceInterface & HasAttributesInterface $resource,
161+
?\DateTimeInterface $created=null,
162+
?\DateTimeInterface $updated=null,
163+
): void {
162164
$timestamps = [];
163165
if ($created !== null) {
164166
$timestamps['created'] = $created->format(\DateTime::ISO8601);

examples/collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use alsvanzelf\jsonapi\CollectionDocument;
66
use alsvanzelf\jsonapi\objects\ResourceObject;
77

8-
require 'bootstrap_examples.php';
8+
require __DIR__.'/bootstrap_examples.php';
99

1010
$users = ExampleDataset::findEntities('user');
1111

@@ -18,7 +18,7 @@
1818
foreach ($users as $user) {
1919
$resource = ResourceObject::fromObject($user, type: 'user', id: $user->id);
2020

21-
if ($user->id == 42) {
21+
if ($user->id === 42) {
2222
$ship = new ResourceObject('ship', 5);
2323
$ship->add('name', 'Heart of Gold');
2424
$resource->addRelationship('ship', $ship);

examples/collection_canonical.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use alsvanzelf\jsonapi\CollectionDocument;
66
use alsvanzelf\jsonapi\objects\ResourceObject;
77

8-
require 'bootstrap_examples.php';
8+
require __DIR__.'/bootstrap_examples.php';
99

1010
$articleRecords = ExampleDataset::findRecords('articles');
1111
$commentRecords = ExampleDataset::findRecords('comments');

examples/cursor_pagination_profile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use alsvanzelf\jsonapi\objects\ResourceObject;
77
use alsvanzelf\jsonapi\profiles\CursorPaginationProfile;
88

9-
require 'bootstrap_examples.php';
9+
require __DIR__.'/bootstrap_examples.php';
1010

1111
/**
1212
* use the cursor pagination profile as extension to the document

examples/errors_all_options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use alsvanzelf\jsonapi\ErrorsDocument;
66
use alsvanzelf\jsonapi\objects\ErrorObject;
77

8-
require 'bootstrap_examples.php';
8+
require __DIR__.'/bootstrap_examples.php';
99

1010
/**
1111
* setting all options

examples/errors_exception_native.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use alsvanzelf\jsonapi\ErrorsDocument;
66

7-
require 'bootstrap_examples.php';
7+
require __DIR__.'/bootstrap_examples.php';
88

99
/**
1010
* via an exception
@@ -17,12 +17,12 @@
1717
try {
1818
throw new \Exception('unknown user', 404);
1919
}
20-
catch (Exception $e) {
20+
catch (Exception $exception) {
2121
$options = [
2222
'includeExceptionTrace' => true,
2323
'includeExceptionPrevious' => true,
2424
];
25-
$document = ErrorsDocument::fromException($e, $options);
25+
$document = ErrorsDocument::fromException($exception, $options);
2626

2727
$options = [
2828
'prettyPrint' => true,

examples/extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use alsvanzelf\jsonapi\enums\ContentTypeEnum;
77
use alsvanzelf\jsonapi\helpers\Converter;
88

9-
require 'bootstrap_examples.php';
9+
require __DIR__.'/bootstrap_examples.php';
1010

1111
/**
1212
* use an extension extend the document with new members

examples/meta_only.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use alsvanzelf\jsonapi\MetaDocument;
66

7-
require 'bootstrap_examples.php';
7+
require __DIR__.'/bootstrap_examples.php';
88

99
/**
1010
* there are a few use-cases for sending meta-only responses

examples/null_values.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use alsvanzelf\jsonapi\objects\LinkObject;
88
use alsvanzelf\jsonapi\objects\RelationshipObject;
99

10-
require 'bootstrap_examples.php';
10+
require __DIR__.'/bootstrap_examples.php';
1111

1212
/**
1313
* tell that a value is non-existing

0 commit comments

Comments
 (0)