Skip to content

Commit b9e343c

Browse files
Nyholmoprypkhantc
andauthored
Support for PHP 81 (#542)
* fix: PHP 8.1 support (#1) * fixes Co-authored-by: oprypkhantc <54406427+oprypkhantc@users.noreply.github.com>
1 parent 594f58b commit b9e343c

File tree

8 files changed

+43
-58
lines changed

8 files changed

+43
-58
lines changed

src/Manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public function parseIncludes($includes)
176176

177177
foreach ($includes as $include) {
178178
list($includeName, $allModifiersStr) = array_pad(explode(':', $include, 2), 2, '');
179-
list($allModifiersStr, $subRelations) = array_pad(explode('.', $allModifiersStr, 2), 2, null);
179+
$a = $allModifiersStr ? explode('.', $allModifiersStr, 2) : [''];
180+
list($allModifiersStr, $subRelations) = array_pad($a, 2, null);
180181

181182
// Trim it down to a cool level of recursion
182183
$includeName = $this->trimToAcceptableRecursionLevel($includeName);

src/ParamBag.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public function __unset($key)
105105
*
106106
* @return bool
107107
*/
108-
#[\ReturnTypeWillChange]
109-
public function offsetExists($key)
108+
public function offsetExists($key): bool
110109
{
111110
return $this->__isset($key);
112111
}
@@ -134,8 +133,7 @@ public function offsetGet($key)
134133
*
135134
* @return void
136135
*/
137-
#[\ReturnTypeWillChange]
138-
public function offsetSet($key, $value)
136+
public function offsetSet($key, $value): void
139137
{
140138
throw new \LogicException('Modifying parameters is not permitted');
141139
}
@@ -149,8 +147,7 @@ public function offsetSet($key, $value)
149147
*
150148
* @return void
151149
*/
152-
#[\ReturnTypeWillChange]
153-
public function offsetUnset($key)
150+
public function offsetUnset($key): void
154151
{
155152
throw new \LogicException('Modifying parameters is not permitted');
156153
}
@@ -160,8 +157,7 @@ public function offsetUnset($key)
160157
*
161158
* @return \ArrayIterator
162159
*/
163-
#[\ReturnTypeWillChange]
164-
public function getIterator()
160+
public function getIterator(): \ArrayIterator
165161
{
166162
return new \ArrayIterator($this->params);
167163
}

src/Scope.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ public function toArray()
295295
/**
296296
* @return mixed
297297
*/
298-
#[\ReturnTypeWillChange]
299-
public function jsonSerialize()
298+
public function jsonSerialize(): array
300299
{
301300
return $this->toArray();
302301
}

test/ManagerTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace League\Fractal\Test;
22

3+
use InvalidArgumentException;
34
use League\Fractal\Manager;
45
use League\Fractal\ParamBag;
56
use League\Fractal\Resource\Collection;
@@ -17,23 +18,19 @@ public function testParseIncludeSelfie()
1718
$this->assertInstanceOf(get_class($manager), $manager->parseIncludes(['foo']));
1819
}
1920

20-
/**
21-
* @expectedException \InvalidArgumentException
22-
* @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given
23-
*/
2421
public function testInvalidParseInclude()
2522
{
23+
$this->expectExceptionObject(new InvalidArgumentException('The parseIncludes() method expects a string or an array. NULL given'));
24+
2625
$manager = new Manager();
2726

2827
$manager->parseIncludes(null);
2928
}
3029

31-
/**
32-
* @expectedException \InvalidArgumentException
33-
* @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given
34-
*/
3530
public function testIceTParseInclude()
3631
{
32+
$this->expectExceptionObject(new InvalidArgumentException('The parseIncludes() method expects a string or an array. integer given'));
33+
3734
$manager = new Manager();
3835

3936
$manager->parseIncludes(99);
@@ -99,23 +96,19 @@ public function testParseExcludeSelfie()
9996
$this->assertInstanceOf(get_class($manager), $manager->parseExcludes(['foo']));
10097
}
10198

102-
/**
103-
* @expectedException \InvalidArgumentException
104-
* @expectedExceptionMessage The parseExcludes() method expects a string or an array. NULL given
105-
*/
10699
public function testInvalidParseExclude()
107100
{
101+
$this->expectExceptionObject(new InvalidArgumentException('The parseExcludes() method expects a string or an array. NULL given'));
102+
108103
$manager = new Manager();
109104

110105
$manager->parseExcludes(null);
111106
}
112107

113-
/**
114-
* @expectedException \InvalidArgumentException
115-
* @expectedExceptionMessage The parseExcludes() method expects a string or an array. integer given
116-
*/
117108
public function testIceTParseExclude()
118109
{
110+
$this->expectExceptionObject(new InvalidArgumentException('The parseExcludes() method expects a string or an array. integer given'));
111+
119112
$manager = new Manager();
120113

121114
$manager->parseExcludes(99);

test/ParamBagTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace League\Fractal\Test;
22

33
use League\Fractal\ParamBag;
4+
use LogicException;
45
use PHPUnit\Framework\TestCase;
56

67
class ParamBagTest extends TestCase
@@ -32,23 +33,19 @@ public function testArrayAccess()
3233
$this->assertNull($params['totallymadeup']);
3334
}
3435

35-
/**
36-
* @expectedException \LogicException
37-
* @expectedExceptionMessage Modifying parameters is not permitted
38-
*/
3936
public function testArrayAccessSetFails()
4037
{
38+
$this->expectExceptionObject(new LogicException('Modifying parameters is not permitted'));
39+
4140
$params = new ParamBag(['foo' => 'bar']);
4241

4342
$params['foo'] = 'someothervalue';
4443
}
4544

46-
/**
47-
* @expectedException \LogicException
48-
* @expectedExceptionMessage Modifying parameters is not permitted
49-
*/
5045
public function testArrayAccessUnsetFails()
5146
{
47+
$this->expectExceptionObject(new LogicException('Modifying parameters is not permitted'));
48+
5249
$params = new ParamBag(['foo' => 'bar']);
5350

5451
unset($params['foo']);
@@ -64,23 +61,19 @@ public function testObjectAccess()
6461
$this->assertTrue(isset($params->foo));
6562
}
6663

67-
/**
68-
* @expectedException \LogicException
69-
* @expectedExceptionMessage Modifying parameters is not permitted
70-
*/
7164
public function testObjectAccessSetFails()
7265
{
66+
$this->expectExceptionObject(new LogicException('Modifying parameters is not permitted'));
67+
7368
$params = new ParamBag(['foo' => 'bar']);
7469

7570
$params->foo = 'someothervalue';
7671
}
7772

78-
/**
79-
* @expectedException \LogicException
80-
* @expectedExceptionMessage Modifying parameters is not permitted
81-
*/
8273
public function testObjectAccessUnsetFails()
8374
{
75+
$this->expectExceptionObject(new LogicException('Modifying parameters is not permitted'));
76+
8477
$params = new ParamBag(['foo' => 'bar']);
8578

8679
unset($params->foo);

test/ScopeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace League\Fractal\Test;
22

3+
use InvalidArgumentException;
34
use League\Fractal\Manager;
45
use League\Fractal\Pagination\Cursor;
56
use League\Fractal\Resource\Collection;
@@ -222,12 +223,11 @@ public function testIsExcluded()
222223
$this->assertTrue($scope->isExcluded('baz.bart'));
223224
}
224225

225-
/**
226-
* @expectedException \InvalidArgumentException
227-
*/
228226
public function testScopeRequiresConcreteImplementation()
229227
{
230-
$manager = new Manager();
228+
$this->expectException(InvalidArgumentException::class);
229+
230+
$manager = new Manager();
231231
$manager->parseIncludes('book');
232232

233233
$resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [
@@ -382,11 +382,11 @@ public function testRunAppropriateTransformerWithCollection()
382382

383383
/**
384384
* @covers \League\Fractal\Scope::executeResourceTransformers
385-
* @expectedException \InvalidArgumentException
386-
* @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection
387385
*/
388386
public function testCreateDataWithClassFuckKnows()
389387
{
388+
$this->expectExceptionObject(new InvalidArgumentException('Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection'));
389+
390390
$manager = new Manager();
391391

392392
$transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();

test/Serializer/JsonApiSerializerTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace League\Fractal\Test\Serializer;
22

3+
use InvalidArgumentException;
34
use League\Fractal\Manager;
45
use League\Fractal\Resource\Collection;
56
use League\Fractal\Resource\Item;
@@ -1827,13 +1828,11 @@ public function testSerializingCollectionResourceWithLinksForHasManyRelationship
18271828
$this->assertSame($expectedJson, $scope->toJson());
18281829
}
18291830

1830-
/**
1831-
* @expectedException \InvalidArgumentException
1832-
* @expectedExceptionMessage JSON API resource objects MUST have a valid id
1833-
*/
18341831
public function testExceptionThrownIfResourceHasNoId()
18351832
{
1836-
$bookData = [
1833+
$this->expectExceptionObject(new InvalidArgumentException('JSON API resource objects MUST have a valid id'));
1834+
1835+
$bookData = [
18371836
'title' => 'Foo',
18381837
'year' => '1991',
18391838
];

test/TransformerAbstractTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace League\Fractal\Test;
22

3+
use BadMethodCallException;
4+
use Exception;
35
use League\Fractal\Manager;
46
use League\Fractal\Resource\Collection;
57
use League\Fractal\Resource\Item;
@@ -97,10 +99,11 @@ public function testProcessEmbeddedResourcesNoDefaultIncludes()
9799
/**
98100
* @covers \League\Fractal\TransformerAbstract::processIncludedResources
99101
* @covers \League\Fractal\TransformerAbstract::callIncludeMethod
100-
* @expectedException \BadMethodCallException
101102
*/
102103
public function testProcessEmbeddedResourcesInvalidAvailableEmbed()
103104
{
105+
$this->expectException(BadMethodCallException::class);
106+
104107
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
105108

106109
$manager = new Manager();
@@ -116,10 +119,11 @@ public function testProcessEmbeddedResourcesInvalidAvailableEmbed()
116119
/**
117120
* @covers \League\Fractal\TransformerAbstract::processIncludedResources
118121
* @covers \League\Fractal\TransformerAbstract::callIncludeMethod
119-
* @expectedException \BadMethodCallException
120122
*/
121123
public function testProcessEmbeddedResourcesInvalidDefaultEmbed()
122124
{
125+
$this->expectException(BadMethodCallException::class);
126+
123127
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
124128

125129
$manager = new Manager();
@@ -228,11 +232,11 @@ public function testProcessIncludedAvailableResourcesEmptyEmbed()
228232

229233
/**
230234
* @covers \League\Fractal\TransformerAbstract::callIncludeMethod
231-
* @expectedException \Exception
232-
* @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook().
233235
*/
234236
public function testCallEmbedMethodReturnsCrap()
235237
{
238+
$this->expectExceptionObject(new Exception('Invalid return value from League\Fractal\TransformerAbstract::includeBook().'));
239+
236240
$manager = new Manager();
237241
$manager->parseIncludes('book');
238242
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]');

0 commit comments

Comments
 (0)