Skip to content

Commit 8007a00

Browse files
authored
Use newer PHP syntax, add types (Case 177753) (#14)
Use constructor property promotion, add property types, readonly modifier and return types where it would not result in a BC break (e.g. refrain from adding return types to the public interface). Update URLs for external references.
1 parent 57d8544 commit 8007a00

File tree

7 files changed

+29
-63
lines changed

7 files changed

+29
-63
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"name": "webfactory GmbH",
1515
"email": "[email protected]",
16-
"homepage": "http://www.webfactory.de",
16+
"homepage": "https://www.webfactory.de",
1717
"role": "Developer"
1818
}
1919
],

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd">
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd">
55
<testsuites>
66
<testsuite name="Library Test Suite">
77
<directory>tests</directory>

src/NotModified/Attribute/ReplaceWithNotModifiedResponse.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,17 @@
2424
#[Attribute(Attribute::TARGET_METHOD)]
2525
final class ReplaceWithNotModifiedResponse
2626
{
27-
/** @var array */
28-
private $parameters;
29-
3027
/** @var LastModifiedDeterminator[] */
31-
private $lastModifiedDeterminators;
32-
33-
/** @var ContainerInterface */
34-
private $container;
28+
private array $lastModifiedDeterminators;
29+
private ContainerInterface $container;
30+
private ?DateTime $lastModified = null;
3531

36-
/** @var DateTime|null */
37-
private $lastModified;
38-
39-
public function __construct(array $parameters)
40-
{
41-
$this->parameters = $parameters;
32+
public function __construct(
33+
private readonly array $parameters,
34+
) {
4235
}
4336

44-
/**
45-
* @return DateTime|null
46-
*/
47-
public function determineLastModified(Request $request)
37+
public function determineLastModified(Request $request): ?DateTime
4838
{
4939
$this->initialiseLastModifiedDeterminators();
5040

@@ -58,12 +48,12 @@ public function determineLastModified(Request $request)
5848
return $this->lastModified;
5949
}
6050

61-
public function setContainer(ContainerInterface $container)
51+
public function setContainer(ContainerInterface $container): void
6252
{
6353
$this->container = $container;
6454
}
6555

66-
private function initialiseLastModifiedDeterminators()
56+
private function initialiseLastModifiedDeterminators(): void
6757
{
6858
if (0 === count($this->parameters)) {
6959
throw new RuntimeException('The attribute '.get_class($this).' has to be parametrised with LastModifiedDeterminators.');

src/NotModified/EventListener.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,18 @@
2424
*/
2525
final class EventListener
2626
{
27-
/** @var ContainerInterface */
28-
private $container;
29-
3027
/**
3128
* Maps (master and sub) requests to their corresponding last modified date. This date is determined by the
3229
* ReplaceWithNotModifiedResponse annotation of the corresponding controller's action.
33-
*
34-
* @var SplObjectStorage
3530
*/
36-
private $lastModified;
31+
private SplObjectStorage $lastModified;
3732

38-
/**
39-
* @var bool Symfony kernel.debug mode
40-
*/
41-
private $debug;
42-
43-
public function __construct(ContainerInterface $container, bool $debug = false)
44-
{
45-
$this->container = $container;
33+
public function __construct(
34+
private readonly ContainerInterface $container,
35+
// Symfony kernel.debug mode
36+
private readonly bool $debug = false,
37+
) {
4638
$this->lastModified = new SplObjectStorage();
47-
$this->debug = $debug;
4839
}
4940

5041
/**
@@ -53,7 +44,7 @@ public function __construct(ContainerInterface $container, bool $debug = false)
5344
* header in the request, replace the determined controller action with a minimal action that just returns an
5445
* "empty" response with a 304 Not Modified HTTP status code.
5546
*/
56-
public function onKernelController(ControllerEvent $event)
47+
public function onKernelController(ControllerEvent $event): void
5748
{
5849
$annotation = $this->findAnnotation($event->getController());
5950
if (!$annotation) {
@@ -89,7 +80,7 @@ public function onKernelController(ControllerEvent $event)
8980
* If a last modified date was determined for the current (master or sub) request, set it to the response so the
9081
* client can use it for the "If-Modified-Since" header in subsequent requests.
9182
*/
92-
public function onKernelResponse(ResponseEvent $event)
83+
public function onKernelResponse(ResponseEvent $event): void
9384
{
9485
$request = $event->getRequest();
9586
$response = $event->getResponse();
@@ -101,10 +92,8 @@ public function onKernelResponse(ResponseEvent $event)
10192

10293
/**
10394
* @param $controllerCallable callable PHP callback pointing to the method to reflect on.
104-
*
105-
* @return ?ReplaceWithNotModifiedResponse The annotation, if found. Null otherwise.
10695
*/
107-
private function findAnnotation(callable $controllerCallable)
96+
private function findAnnotation(callable $controllerCallable): ?ReplaceWithNotModifiedResponse
10897
{
10998
if (!is_array($controllerCallable)) {
11099
return null;

src/NotModified/services.xml

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

33
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns="http://symfony.com/schema/dic/services"
5-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66
<services>
77
<service id="Webfactory\HttpCacheBundle\NotModified\EventListener" class="Webfactory\HttpCacheBundle\NotModified\EventListener" public="true">
88
<argument type="service" id="service_container" />

tests/NotModified/Attribute/ReplaceWithNotModifiedResponseTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,14 @@ final class FakeLastModifiedDeterminatorWithoutInterface
117117

118118
final class MyLastModifedDeterminator implements LastModifiedDeterminator
119119
{
120-
/** @var DateTime */
121-
private $lastModified;
120+
private DateTime $lastModified;
122121

123122
public function __construct(?DateTime $lastModified = null)
124123
{
125124
$this->lastModified = $lastModified ?: DateTime::createFromFormat('U', time());
126125
}
127126

128-
public function getLastModified(Request $request)
127+
public function getLastModified(Request $request): DateTime
129128
{
130129
return $this->lastModified;
131130
}

tests/NotModified/EventListenerTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,15 @@ final class EventListenerTest extends TestCase
3333
{
3434
/**
3535
* System under test.
36-
*
37-
* @var EventListener
3836
*/
39-
private $eventListener;
40-
41-
/** @var ContainerInterface|MockObject */
42-
private $container;
43-
44-
/** @var ControllerEvent|MockObject */
45-
private $filterControllerEvent;
46-
47-
/** @var Request */
48-
private $request;
49-
50-
/** @var Response */
51-
private $response;
37+
private EventListener $eventListener;
5238

39+
private ContainerInterface&MockObject $container;
40+
private ControllerEvent|MockObject $filterControllerEvent;
41+
private Request $request;
42+
private Response $response;
5343
private $callable;
54-
55-
/** @var KernelInterface|MockObject */
56-
private $kernel;
44+
private KernelInterface&MockObject $kernel;
5745

5846
protected function setUp(): void
5947
{

0 commit comments

Comments
 (0)