Skip to content

Commit

Permalink
readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 6, 2024
1 parent c779293 commit c2b2908
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 94 deletions.
9 changes: 3 additions & 6 deletions src/Bridges/HttpDI/HttpExtension.php
Expand Up @@ -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,
) {
}


Expand Down
12 changes: 4 additions & 8 deletions src/Bridges/HttpDI/SessionExtension.php
Expand Up @@ -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,
) {
}


Expand Down
12 changes: 4 additions & 8 deletions src/Http/Context.php
Expand Up @@ -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,
) {
}


Expand Down
8 changes: 4 additions & 4 deletions src/Http/FileUpload.php
Expand Up @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions src/Http/Request.php
Expand Up @@ -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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/Http/Session.php
Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/Http/SessionSection.php
Expand Up @@ -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,
) {
}


Expand Down
8 changes: 4 additions & 4 deletions src/Http/UserStorage.php
Expand Up @@ -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,
) {
}


Expand Down
72 changes: 25 additions & 47 deletions tests/Http/FileUpload.getSanitizedName.phpt
Expand Up @@ -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'));
});

0 comments on commit c2b2908

Please sign in to comment.