From a0ce2833669b850bbecca6fb1f86e6b068c1ca40 Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 25 Jan 2024 13:52:02 +0100 Subject: [PATCH] Corona: Add types to request class properties Reviewed at https://reviews.lunr.nl/r/1103/ --- src/Lunr/Corona/Request.php | 31 ++++++++------------ src/Lunr/Corona/Tests/RequestGetDataTest.php | 14 ++++++--- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Lunr/Corona/Request.php b/src/Lunr/Corona/Request.php index 66e21212..23a199da 100644 --- a/src/Lunr/Corona/Request.php +++ b/src/Lunr/Corona/Request.php @@ -39,62 +39,62 @@ class Request * Stored $_POST values * @var array */ - protected $post; + protected readonly array $post; /** * Stored $_GET values * @var array */ - protected $get; + protected readonly array $get; /** * Stored $_COOKIE values * @var array */ - protected $cookie; + protected readonly array $cookie; /** * Stored $_SERVER values * @var array */ - protected $server; + protected readonly array $server; /** * Request property data * * @var array */ - protected $request; + protected readonly array $request; /** * Stored $_FILES values * @var array> */ - protected $files; + protected readonly array $files; /** * Stored php://input values * @var string */ - protected $raw_data; + protected string $raw_data; /** * Stored command line arguments * @var array */ - protected $cli_args; + protected readonly array $cli_args; /** * Shared instance of the request parser. * @var RequestParserInterface */ - protected $parser; + protected readonly RequestParserInterface $parser; /** * The request values to mock. * @var array */ - private $mock; + private array $mock; /** * Constructor. @@ -122,15 +122,8 @@ public function __construct($parser) */ public function __destruct() { - unset($this->post); - unset($this->get); - unset($this->server); - unset($this->cookie); - unset($this->request); - unset($this->files); - unset($this->parser); - unset($this->mock); - unset($this->raw_data); + // Intentionally not unsetting $this->mock and $this->raw_data, since + // that may break access to mocked request values during PHP shutdown. } /** diff --git a/src/Lunr/Corona/Tests/RequestGetDataTest.php b/src/Lunr/Corona/Tests/RequestGetDataTest.php index 3f6ab9a4..6f67e25d 100644 --- a/src/Lunr/Corona/Tests/RequestGetDataTest.php +++ b/src/Lunr/Corona/Tests/RequestGetDataTest.php @@ -196,9 +196,12 @@ public function testGetAllOptionsReturnsArray($keys): void $values[] = 'value'; } - $this->set_reflection_property_value('cli_args', array_combine($keys, $values)); + $class = $this->reflection->newInstanceWithoutConstructor(); - $return = $this->class->get_all_options(); + $this->reflection->getProperty('cli_args') + ->setValue($class, array_combine($keys, $values)); + + $return = $class->get_all_options(); $this->assertEquals($keys, $return); } @@ -213,9 +216,12 @@ public function testGetAllOptionsReturnsArray($keys): void */ public function testGetOptionDataReturnsValueForValidKey($value): void { - $this->set_reflection_property_value('cli_args', [ 'a' => $value ]); + $class = $this->reflection->newInstanceWithoutConstructor(); + + $this->reflection->getProperty('cli_args') + ->setValue($class, [ 'a' => $value ]); - $result = $this->class->get_option_data('a'); + $result = $class->get_option_data('a'); $this->assertEquals($value, $result); }