Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use newer PHP syntax, add types (Case 177753) #14

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "webfactory GmbH",
"email": "[email protected]",
"homepage": "http://www.webfactory.de",
"homepage": "https://www.webfactory.de",
"role": "Developer"
}
],
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd">
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd">
<testsuites>
<testsuite name="Library Test Suite">
<directory>tests</directory>
Expand Down
28 changes: 9 additions & 19 deletions src/NotModified/Attribute/ReplaceWithNotModifiedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,17 @@
#[Attribute(Attribute::TARGET_METHOD)]
final class ReplaceWithNotModifiedResponse
{
/** @var array */
private $parameters;

/** @var LastModifiedDeterminator[] */
private $lastModifiedDeterminators;

/** @var ContainerInterface */
private $container;
private array $lastModifiedDeterminators;
private ContainerInterface $container;
private ?DateTime $lastModified = null;

/** @var DateTime|null */
private $lastModified;

public function __construct(array $parameters)
{
$this->parameters = $parameters;
public function __construct(
private readonly array $parameters,
) {
}

/**
* @return DateTime|null
*/
public function determineLastModified(Request $request)
public function determineLastModified(Request $request): ?DateTime
{
$this->initialiseLastModifiedDeterminators();

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

public function setContainer(ContainerInterface $container)
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

private function initialiseLastModifiedDeterminators()
private function initialiseLastModifiedDeterminators(): void
{
if (0 === count($this->parameters)) {
throw new RuntimeException('The attribute '.get_class($this).' has to be parametrised with LastModifiedDeterminators.');
Expand Down
29 changes: 9 additions & 20 deletions src/NotModified/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,18 @@
*/
final class EventListener
{
/** @var ContainerInterface */
private $container;

/**
* Maps (master and sub) requests to their corresponding last modified date. This date is determined by the
* ReplaceWithNotModifiedResponse annotation of the corresponding controller's action.
*
* @var SplObjectStorage
*/
private $lastModified;
private SplObjectStorage $lastModified;

/**
* @var bool Symfony kernel.debug mode
*/
private $debug;

public function __construct(ContainerInterface $container, bool $debug = false)
{
$this->container = $container;
public function __construct(
private readonly ContainerInterface $container,
// Symfony kernel.debug mode
private readonly bool $debug = false,
) {
$this->lastModified = new SplObjectStorage();
$this->debug = $debug;
}

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

/**
* @param $controllerCallable callable PHP callback pointing to the method to reflect on.
*
* @return ?ReplaceWithNotModifiedResponse The annotation, if found. Null otherwise.
*/
private function findAnnotation(callable $controllerCallable)
private function findAnnotation(callable $controllerCallable): ?ReplaceWithNotModifiedResponse
{
if (!is_array($controllerCallable)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/NotModified/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Webfactory\HttpCacheBundle\NotModified\EventListener" class="Webfactory\HttpCacheBundle\NotModified\EventListener" public="true">
<argument type="service" id="service_container" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,14 @@ final class FakeLastModifiedDeterminatorWithoutInterface

final class MyLastModifedDeterminator implements LastModifiedDeterminator
{
/** @var DateTime */
private $lastModified;
private DateTime $lastModified;

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

public function getLastModified(Request $request)
public function getLastModified(Request $request): DateTime
{
return $this->lastModified;
}
Expand Down
24 changes: 6 additions & 18 deletions tests/NotModified/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,15 @@ final class EventListenerTest extends TestCase
{
/**
* System under test.
*
* @var EventListener
*/
private $eventListener;

/** @var ContainerInterface|MockObject */
private $container;

/** @var ControllerEvent|MockObject */
private $filterControllerEvent;

/** @var Request */
private $request;

/** @var Response */
private $response;
private EventListener $eventListener;

private ContainerInterface&MockObject $container;
private ControllerEvent|MockObject $filterControllerEvent;
private Request $request;
private Response $response;
private $callable;

/** @var KernelInterface|MockObject */
private $kernel;
private KernelInterface&MockObject $kernel;

protected function setUp(): void
{
Expand Down
Loading