Skip to content

Commit 7f95263

Browse files
authored
Merge pull request #80 from lode/improving-linting
2 parents a3f291b + 5fa5254 commit 7f95263

Some content is hidden

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

47 files changed

+329
-292
lines changed

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: test
2+
on:
3+
pull_request:
4+
paths:
5+
- "**.php"
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
if: github.event.pull_request.draft == false
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: '8.3'
18+
19+
- name: Setup Composer
20+
uses: ramsey/composer-install@v3
21+
with:
22+
composer-options: "--no-scripts"
23+
24+
- name: Run phpunit
25+
run: ./vendor/bin/phpunit

examples/bootstrap_examples.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
use alsvanzelf\jsonapi\Document;
44
use alsvanzelf\jsonapi\ResourceDocument;
55
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
6+
use alsvanzelf\jsonapi\interfaces\HasAttributesInterface;
7+
use alsvanzelf\jsonapi\interfaces\HasExtensionMembersInterface;
68
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
79
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
810
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
11+
use alsvanzelf\jsonapi\objects\ResourceObject;
912

1013
ini_set('display_errors', 1);
1114
error_reporting(-1);
@@ -55,7 +58,7 @@ class ExampleDataset {
5558

5659
public static function getRecord($type, $id) {
5760
if (!isset(self::$records[$type][$id])) {
58-
throw new Exception('sorry, we have a limited dataset');
61+
throw new \Exception('sorry, we have a limited dataset');
5962
}
6063

6164
return self::$records[$type][$id];
@@ -121,6 +124,10 @@ public function getNamespace() {
121124
*/
122125

123126
public function setVersion(ResourceInterface $resource, $version) {
127+
if ($resource instanceof HasExtensionMembersInterface === false) {
128+
throw new \Exception('resource doesn\'t have extension members');
129+
}
130+
124131
if ($resource instanceof ResourceDocument) {
125132
$resource->getResource()->addExtensionMember($this, 'id', $version);
126133
}
@@ -143,9 +150,12 @@ public function getOfficialLink() {
143150
* optionally helpers for the specific profile
144151
*/
145152

153+
/**
154+
* @param ResourceInterface&HasAttributesInterface $resource
155+
*/
146156
public function setTimestamps(ResourceInterface $resource, ?\DateTimeInterface $created=null, ?\DateTimeInterface $updated=null) {
147-
if ($resource instanceof ResourceIdentifierObject) {
148-
throw new Exception('cannot add attributes to identifier objects');
157+
if ($resource instanceof HasAttributesInterface === false) {
158+
throw new \Exception('cannot add attributes to identifier objects');
149159
}
150160

151161
$timestamps = [];
@@ -156,6 +166,6 @@ public function setTimestamps(ResourceInterface $resource, ?\DateTimeInterface $
156166
$timestamps['updated'] = $updated->format(\DateTime::ISO8601);
157167
}
158168

159-
$resource->add('timestamps', $timestamps);
169+
$resource->addAttribute('timestamps', $timestamps);
160170
}
161171
}

examples/errors_exception_native.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
try {
16-
throw new Exception('unknown user', 404);
16+
throw new \Exception('unknown user', 404);
1717
}
1818
catch (Exception $e) {
1919
$options = [

examples/relationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
$custom_relation = [
7979
'data' => ['cus' => 'tom'],
8080
];
81-
$jsonapi->add_relation('custom', $custom_relation);
81+
$jsonapi->addRelationship('custom', $custom_relation);
8282

8383
/**
8484
* sending the response

phpstan.bonus.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ includes:
33
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
44

55
parameters:
6-
level: 9
6+
level: 10
77

88
treatPhpDocTypesAsCertain: true
99

phpstan.neon

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
parameters:
22
# slowly increase
3-
level: 1
3+
level: 4
44
paths:
55
- src/
66
- tests/
@@ -14,9 +14,11 @@ parameters:
1414
- src/resource.php
1515
- src/response.php
1616

17+
treatPhpDocTypesAsCertain: false
18+
1719
strictRules:
1820
allRules: false
19-
21+
2022
reportUnmatchedIgnoredErrors: true
2123
ignoreErrors:
2224
# add cases to ignore because they are too much work for now

script/fix

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ if test "$HAS_TYPE" = 1; then
8181
$CONSOLE_PREFIX ./vendor/bin/phpcbf $EXTRA_ARGUMENTS $FILE_FILTER
8282

8383
elif test "$TYPE" = "rector"; then
84-
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
8584
$CONSOLE_PREFIX ./vendor/bin/rector process $EXTRA_ARGUMENTS $FILE_FILTER
8685

8786
else
@@ -93,7 +92,6 @@ else
9392
echo -e "${T_BOLD}Running fixes on the changed files (not only git-diff) between the base and the head branch${T_RESET}\n"
9493

9594
echo "${T_INFO}Fixing refactors (rector)${T_RESET}"
96-
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
9795
$CONSOLE_PREFIX ./vendor/bin/rector process $FILE_FILTER || true
9896

9997
echo "${T_INFO}Fixing coding standards (phpcs)${T_RESET}"

script/lint

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ if test "$HAS_TYPE" = 1; then
120120
fi
121121

122122
elif test "$TYPE" = "rector"; then
123-
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
124123
$CONSOLE_PREFIX ./vendor/bin/rector process --dry-run $EXTRA_ARGUMENTS $FILE_FILTER
125124

126125
else
@@ -161,7 +160,6 @@ else
161160
echo -e '\n'
162161

163162
echo "${T_INFO}Checking refactors (rector)${T_RESET}"
164-
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
165163
$CONSOLE_PREFIX ./vendor/bin/rector process --memory-limit=4g --dry-run $FILE_FILTER 2> /dev/null \
166164
&& echo "${T_SUCCESS}Success${T_RESET}"
167165

src/CollectionDocument.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
1010
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface;
1111
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
12-
use alsvanzelf\jsonapi\objects\ResourceObject;
1312
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
13+
use alsvanzelf\jsonapi\objects\ResourceObject;
1414

1515
/**
1616
* this document is a set of Resources
@@ -63,9 +63,6 @@ public function add($type, $id, array $attributes=[]) {
6363
}
6464
}
6565

66-
/**
67-
* @inheritDoc
68-
*/
6966
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) {
7067
if ($previousHref !== null) {
7168
$this->addLink('prev', $previousHref);
@@ -115,9 +112,6 @@ public function addResource(ResourceInterface $resource, array $options=[]) {
115112
* DocumentInterface
116113
*/
117114

118-
/**
119-
* @inheritDoc
120-
*/
121115
public function toArray() {
122116
$array = parent::toArray();
123117

@@ -133,9 +127,6 @@ public function toArray() {
133127
* ResourceContainerInterface
134128
*/
135129

136-
/**
137-
* @inheritDoc
138-
*/
139130
public function getContainedResources() {
140131
return $this->resources;
141132
}

src/DataDocument.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public function addIncludedResourceObject(ResourceObject ...$resourceObjects) {
5858
* DocumentInterface
5959
*/
6060

61-
/**
62-
* @inheritDoc
63-
*/
6461
public function toArray() {
6562
$array = parent::toArray();
6663

0 commit comments

Comments
 (0)