Skip to content

Commit 2f62d05

Browse files
authored
Merge pull request #225 from clue-labs/php8-attribute
Improve test suite and add dummy `#[PHP8]` attribute
2 parents eecba13 + 9fbb308 commit 2f62d05

4 files changed

Lines changed: 34 additions & 145 deletions

File tree

tests/ContainerTest.php

Lines changed: 17 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ public function __invoke(): ResponseInterface
804804
}
805805
};
806806

807-
$fn = null;
807+
$fn = $data = null;
808808
$fn = #[PHP8] fn(mixed $data = 42) => new Response(200, [], (string) json_encode($data)); // @phpstan-ignore-line
809809
$container = new Container([
810810
ResponseInterface::class => $fn,
@@ -1269,17 +1269,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesUnknow
12691269
$request = new ServerRequest('GET', 'http://example.com/');
12701270

12711271
$controller = new class(new \stdClass()) {
1272-
/** @var \stdClass */
1273-
private $data;
1274-
12751272
public function __construct(\stdClass $data)
12761273
{
1277-
$this->data = $data;
1278-
}
1279-
1280-
public function __invoke(ServerRequestInterface $request): Response
1281-
{
1282-
return new Response(200, [], (string) json_encode($this->data));
1274+
assert($data instanceof \stdClass);
12831275
}
12841276
};
12851277

@@ -1301,17 +1293,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesRecurs
13011293
$request = new ServerRequest('GET', 'http://example.com/');
13021294

13031295
$controller = new class(new \stdClass()) {
1304-
/** @var \stdClass */
1305-
private $data;
1306-
13071296
public function __construct(\stdClass $data)
13081297
{
1309-
$this->data = $data;
1310-
}
1311-
1312-
public function __invoke(ServerRequestInterface $request): Response
1313-
{
1314-
return new Response(200, [], (string) json_encode($this->data));
1298+
assert($data instanceof \stdClass);
13151299
}
13161300
};
13171301

@@ -1333,17 +1317,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesString
13331317
$request = new ServerRequest('GET', 'http://example.com/');
13341318

13351319
$controller = new class('') {
1336-
/** @var string */
1337-
private $data;
1338-
13391320
public function __construct(string $stdClass)
13401321
{
1341-
$this->data = $stdClass;
1342-
}
1343-
1344-
public function __invoke(ServerRequestInterface $request): Response
1345-
{
1346-
return new Response(200, [], (string) json_encode($this->data));
1322+
assert(is_string($stdClass));
13471323
}
13481324
};
13491325

@@ -1367,17 +1343,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesVariab
13671343
$request = new ServerRequest('GET', 'http://example.com/');
13681344

13691345
$controller = new class(new \stdClass()) {
1370-
/** @var \stdClass */
1371-
private $data;
1372-
13731346
public function __construct(\stdClass $data)
13741347
{
1375-
$this->data = $data;
1376-
}
1377-
1378-
public function __invoke(ServerRequestInterface $request): Response
1379-
{
1380-
return new Response(200, [], (string) json_encode($this->data));
1348+
assert($data instanceof \stdClass);
13811349
}
13821350
};
13831351

@@ -1402,17 +1370,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesObject
14021370
$request = new ServerRequest('GET', 'http://example.com/');
14031371

14041372
$controller = new class(new \stdClass()) {
1405-
/** @var \stdClass */
1406-
private $data;
1407-
14081373
public function __construct(\stdClass $data)
14091374
{
1410-
$this->data = $data;
1411-
}
1412-
1413-
public function __invoke(ServerRequestInterface $request): Response
1414-
{
1415-
return new Response(200, [], (string) json_encode($this->data));
1375+
assert($data instanceof \stdClass);
14161376
}
14171377
};
14181378

@@ -1435,17 +1395,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesString
14351395
$request = new ServerRequest('GET', 'http://example.com/');
14361396

14371397
$controller = new class(new \stdClass()) {
1438-
/** @var \stdClass */
1439-
private $data;
1440-
14411398
public function __construct(\stdClass $data)
14421399
{
1443-
$this->data = $data;
1444-
}
1445-
1446-
public function __invoke(ServerRequestInterface $request): Response
1447-
{
1448-
return new Response(200, [], (string) json_encode($this->data));
1400+
assert($data instanceof \stdClass);
14491401
}
14501402
};
14511403

@@ -1468,17 +1420,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesIntVar
14681420
$request = new ServerRequest('GET', 'http://example.com/');
14691421

14701422
$controller = new class(new \stdClass()) {
1471-
/** @var \stdClass */
1472-
private $data;
1473-
14741423
public function __construct(\stdClass $data)
14751424
{
1476-
$this->data = $data;
1477-
}
1478-
1479-
public function __invoke(ServerRequestInterface $request): Response
1480-
{
1481-
return new Response(200, [], (string) json_encode($this->data));
1425+
assert($data instanceof \stdClass);
14821426
}
14831427
};
14841428

@@ -1501,17 +1445,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesFloatV
15011445
$request = new ServerRequest('GET', 'http://example.com/');
15021446

15031447
$controller = new class(new \stdClass()) {
1504-
/** @var \stdClass */
1505-
private $data;
1506-
15071448
public function __construct(\stdClass $data)
15081449
{
1509-
$this->data = $data;
1510-
}
1511-
1512-
public function __invoke(ServerRequestInterface $request): Response
1513-
{
1514-
return new Response(200, [], (string) json_encode($this->data));
1450+
assert($data instanceof \stdClass);
15151451
}
15161452
};
15171453

@@ -1534,17 +1470,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesBoolVa
15341470
$request = new ServerRequest('GET', 'http://example.com/');
15351471

15361472
$controller = new class(new \stdClass()) {
1537-
/** @var \stdClass */
1538-
private $data;
1539-
15401473
public function __construct(\stdClass $data)
15411474
{
1542-
$this->data = $data;
1543-
}
1544-
1545-
public function __invoke(ServerRequestInterface $request): Response
1546-
{
1547-
return new Response(200, [], (string) json_encode($this->data));
1475+
assert($data instanceof \stdClass);
15481476
}
15491477
};
15501478

@@ -1567,17 +1495,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
15671495
$request = new ServerRequest('GET', 'http://example.com/');
15681496

15691497
$controller = new class(new \stdClass()) {
1570-
/** @var \stdClass */
1571-
private $data;
1572-
15731498
public function __construct(\stdClass $data)
15741499
{
1575-
$this->data = $data;
1576-
}
1577-
1578-
public function __invoke(ServerRequestInterface $request): Response
1579-
{
1580-
return new Response(200, [], (string) json_encode($this->data));
1500+
assert($data instanceof \stdClass);
15811501
}
15821502
};
15831503

@@ -1597,17 +1517,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesNullab
15971517
$request = new ServerRequest('GET', 'http://example.com/');
15981518

15991519
$controller = new class(new \stdClass()) {
1600-
/** @var ?\stdClass */
1601-
private $data;
1602-
16031520
public function __construct(?\stdClass $data)
16041521
{
1605-
$this->data = $data;
1606-
}
1607-
1608-
public function __invoke(ServerRequestInterface $request): Response
1609-
{
1610-
return new Response(200, [], (string) json_encode($this->data));
1522+
assert($data instanceof \stdClass);
16111523
}
16121524
};
16131525

@@ -1627,17 +1539,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
16271539
$request = new ServerRequest('GET', 'http://example.com/');
16281540

16291541
$controller = new class(new \stdClass()) {
1630-
/** @var \stdClass */
1631-
private $data;
1632-
16331542
public function __construct(\stdClass $data)
16341543
{
1635-
$this->data = $data;
1636-
}
1637-
1638-
public function __invoke(ServerRequestInterface $request): Response
1639-
{
1640-
return new Response(200, [], (string) json_encode($this->data));
1544+
assert($data instanceof \stdClass);
16411545
}
16421546
};
16431547

@@ -1657,17 +1561,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
16571561
$request = new ServerRequest('GET', 'http://example.com/');
16581562

16591563
$controller = new class(new \stdClass()) {
1660-
/** @var \stdClass */
1661-
private $data;
1662-
16631564
public function __construct(\stdClass $data)
16641565
{
1665-
$this->data = $data;
1666-
}
1667-
1668-
public function __invoke(ServerRequestInterface $request): Response
1669-
{
1670-
return new Response(200, [], (string) json_encode($this->data));
1566+
assert($data instanceof \stdClass);
16711567
}
16721568
};
16731569

@@ -1687,17 +1583,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesNullab
16871583
$request = new ServerRequest('GET', 'http://example.com/');
16881584

16891585
$controller = new class(new \stdClass()) {
1690-
/** @var ?\stdClass */
1691-
private $data;
1692-
16931586
public function __construct(?\stdClass $data)
16941587
{
1695-
$this->data = $data;
1696-
}
1697-
1698-
public function __invoke(ServerRequestInterface $request): Response
1699-
{
1700-
return new Response(200, [], (string) json_encode($this->data));
1588+
assert($data instanceof \stdClass);
17011589
}
17021590
};
17031591

@@ -1717,17 +1605,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassM
17171605
$request = new ServerRequest('GET', 'http://example.com/');
17181606

17191607
$controller = new class(new \stdClass()) {
1720-
/** @var \stdClass */
1721-
private $data;
1722-
17231608
public function __construct(\stdClass $data)
17241609
{
1725-
$this->data = $data;
1726-
}
1727-
1728-
public function __invoke(ServerRequestInterface $request): Response
1729-
{
1730-
return new Response(200, [], (string) json_encode($this->data));
1610+
assert($data instanceof \stdClass);
17311611
}
17321612
};
17331613

@@ -1747,17 +1627,9 @@ public function testCallableReturnsCallableThatThrowsWhenConstructorWithoutFacto
17471627
$request = new ServerRequest('GET', 'http://example.com/');
17481628

17491629
$controller = new class('Alice') {
1750-
/** @var string */
1751-
private $data;
1752-
17531630
public function __construct(string $name)
17541631
{
1755-
$this->data = $name;
1756-
}
1757-
1758-
public function __invoke(ServerRequestInterface $request): Response
1759-
{
1760-
return new Response(200, [], (string) json_encode($this->data));
1632+
assert(is_string($name));
17611633
}
17621634
};
17631635

tests/Fixtures/InvalidConstructorIntersection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FrameworkX\Tests\Fixtures;
44

5+
use FrameworkX\Tests\PHP8;
6+
57
/** PHP 8.1+ **/
68
class InvalidConstructorIntersection
79
{

tests/Fixtures/InvalidConstructorUnion.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FrameworkX\Tests\Fixtures;
44

5+
use FrameworkX\Tests\PHP8;
6+
57
/** PHP 8.0+ **/
68
class InvalidConstructorUnion
79
{

tests/PHP8.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace FrameworkX\Tests;
4+
5+
/** Dummy attribute used to comment out code for PHP < 8 to ensure compatibility across test matrix */
6+
#[\Attribute]
7+
class PHP8
8+
{
9+
public function __construct()
10+
{
11+
assert(\PHP_VERSION_ID >= 80000);
12+
}
13+
}

0 commit comments

Comments
 (0)