diff --git a/src/Bridges/HttpDI/HttpExtension.php b/src/Bridges/HttpDI/HttpExtension.php index 9e496451..07f19c33 100644 --- a/src/Bridges/HttpDI/HttpExtension.php +++ b/src/Bridges/HttpDI/HttpExtension.php @@ -18,12 +18,9 @@ */ class HttpExtension extends Nette\DI\CompilerExtension { - private bool $cliMode; - - - public function __construct(bool $cliMode = false) - { - $this->cliMode = $cliMode; + public function __construct( + private readonly bool $cliMode = false, + ) { } diff --git a/src/Bridges/HttpDI/SessionExtension.php b/src/Bridges/HttpDI/SessionExtension.php index c752533d..c81a34a3 100644 --- a/src/Bridges/HttpDI/SessionExtension.php +++ b/src/Bridges/HttpDI/SessionExtension.php @@ -19,14 +19,10 @@ */ class SessionExtension extends Nette\DI\CompilerExtension { - private bool $debugMode; - private bool $cliMode; - - - public function __construct(bool $debugMode = false, bool $cliMode = false) - { - $this->debugMode = $debugMode; - $this->cliMode = $cliMode; + public function __construct( + private readonly bool $debugMode = false, + private readonly bool $cliMode = false, + ) { } diff --git a/src/Http/Context.php b/src/Http/Context.php index fdbc27a5..a095c203 100644 --- a/src/Http/Context.php +++ b/src/Http/Context.php @@ -15,14 +15,10 @@ */ class Context { - private IRequest $request; - private IResponse $response; - - - public function __construct(IRequest $request, IResponse $response) - { - $this->request = $request; - $this->response = $response; + public function __construct( + private readonly IRequest $request, + private readonly IResponse $response, + ) { } diff --git a/src/Http/FileUpload.php b/src/Http/FileUpload.php index 9f5fa92c..5599639d 100644 --- a/src/Http/FileUpload.php +++ b/src/Http/FileUpload.php @@ -33,13 +33,13 @@ final class FileUpload /** @deprecated */ public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp']; - private string $name; - private string|null $fullPath; + private readonly string $name; + private readonly string|null $fullPath; private string|false|null $type = null; private string|false|null $extension = null; - private int $size; + private readonly int $size; private string $tmpName; - private int $error; + private readonly int $error; public function __construct(?array $value) diff --git a/src/Http/Request.php b/src/Http/Request.php index 553db3d1..6675fe4a 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -33,25 +33,26 @@ class Request implements IRequest { use Nette\SmartObject; - private array $headers; + private readonly array $headers; - /** @var ?callable */ - private $rawBodyCallback; + private readonly ?\Closure $rawBodyCallback; public function __construct( private UrlScript $url, - private array $post = [], - private array $files = [], - private array $cookies = [], + private readonly array $post = [], + private readonly array $files = [], + private readonly array $cookies = [], array $headers = [], - private string $method = 'GET', - private ?string $remoteAddress = null, + private readonly string $method = 'GET', + private readonly ?string $remoteAddress = null, private ?string $remoteHost = null, ?callable $rawBodyCallback = null, ) { $this->headers = array_change_key_case($headers, CASE_LOWER); - $this->rawBodyCallback = $rawBodyCallback; + $this->rawBodyCallback = $rawBodyCallback + ? \Closure::fromCallable($rawBodyCallback) + : null; } diff --git a/src/Http/Session.php b/src/Http/Session.php index 539e8330..81b69029 100644 --- a/src/Http/Session.php +++ b/src/Http/Session.php @@ -45,8 +45,8 @@ class Session 'gc_maxlifetime' => self::DefaultFileLifetime, // 3 hours ]; - private IRequest $request; - private IResponse $response; + private readonly IRequest $request; + private readonly IResponse $response; private ?\SessionHandlerInterface $handler = null; private bool $readAndClose = false; private bool $fileExists = true; diff --git a/src/Http/SessionSection.php b/src/Http/SessionSection.php index 1bce6fd3..f79b3025 100644 --- a/src/Http/SessionSection.php +++ b/src/Http/SessionSection.php @@ -18,17 +18,15 @@ class SessionSection implements \IteratorAggregate, \ArrayAccess { public bool $warnOnUndefined = false; - private Session $session; - private string $name; /** * Do not call directly. Use Session::getSection(). */ - public function __construct(Session $session, string $name) - { - $this->session = $session; - $this->name = $name; + public function __construct( + private readonly Session $session, + private readonly string $name, + ) { } diff --git a/src/Http/UserStorage.php b/src/Http/UserStorage.php index a557601a..bbcebfb8 100644 --- a/src/Http/UserStorage.php +++ b/src/Http/UserStorage.php @@ -21,13 +21,13 @@ class UserStorage implements Nette\Security\IUserStorage use Nette\SmartObject; private string $namespace = ''; - private Session $sessionHandler; + private SessionSection $sessionSection; - public function __construct(Session $sessionHandler) - { - $this->sessionHandler = $sessionHandler; + public function __construct( + private readonly Session $sessionHandler, + ) { } diff --git a/tests/Http/FileUpload.getSanitizedName.phpt b/tests/Http/FileUpload.getSanitizedName.phpt index 0b0c6cce..aa35fe27 100644 --- a/tests/Http/FileUpload.getSanitizedName.phpt +++ b/tests/Http/FileUpload.getSanitizedName.phpt @@ -12,54 +12,32 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -Assert::with(new FileUpload([]), function () { - $this->name = ''; - Assert::same('unknown', $this->getSanitizedName()); - - $this->name = '--'; - Assert::same('unknown', $this->getSanitizedName()); - - $this->name = 'foo'; - Assert::same('foo', $this->getSanitizedName()); - - $this->name = '.foo.'; - Assert::same('foo', $this->getSanitizedName()); - - $this->name = 'readme.txt'; - Assert::same('readme.txt', $this->getSanitizedName()); - - $this->name = './.image.png'; - Assert::same('image.png', $this->getSanitizedName()); - - $this->name = '../.image.png'; - Assert::same('image.png', $this->getSanitizedName()); - - $this->name = '..\.image.png\\'; - Assert::same('image.png', $this->getSanitizedName()); - - $this->name = '10+.+20.pdf'; - Assert::same('10.20.pdf', $this->getSanitizedName()); +function getSanitizedName(string $name, ?string $ext = null): string +{ + $file = new FileUpload(['name' => $name, 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE]); + Assert::with($file, fn() => $file->extension = $ext); + return $file->getSanitizedName(); +} + + +test('name', function () { + Assert::same('unknown', getSanitizedName('')); + Assert::same('unknown', getSanitizedName('--')); + Assert::same('foo', getSanitizedName('foo')); + Assert::same('foo', getSanitizedName('.foo.')); + Assert::same('readme.txt', getSanitizedName('readme.txt')); + Assert::same('image.png', getSanitizedName('./.image.png')); + Assert::same('image.png', getSanitizedName('../.image.png')); + Assert::same('image.png', getSanitizedName('..\.image.png\\')); + Assert::same('10.20.pdf', getSanitizedName('10+.+20.pdf')); }); -Assert::with(new FileUpload([]), function () { - $this->extension = 'jpeg'; - - $this->name = ''; - Assert::same('unknown.jpeg', $this->getSanitizedName()); - - $this->name = '--'; - Assert::same('unknown.jpeg', $this->getSanitizedName()); - - $this->name = 'foo'; - Assert::same('foo.jpeg', $this->getSanitizedName()); - - $this->name = 'foo.jpg'; - Assert::same('foo.jpeg', $this->getSanitizedName()); - - $this->name = 'foo.php'; - Assert::same('foo.jpeg', $this->getSanitizedName()); - - $this->name = './.image.png'; - Assert::same('image.jpeg', $this->getSanitizedName()); +test('name & extension', function () { + Assert::same('unknown.jpeg', getSanitizedName('', 'jpeg')); + Assert::same('unknown.jpeg', getSanitizedName('--', 'jpeg')); + Assert::same('foo.jpeg', getSanitizedName('foo', 'jpeg')); + Assert::same('foo.jpeg', getSanitizedName('foo.jpg', 'jpeg')); + Assert::same('foo.jpeg', getSanitizedName('foo.php', 'jpeg')); + Assert::same('image.jpeg', getSanitizedName('./.image.png', 'jpeg')); });