Skip to content

Commit

Permalink
used native PHP 8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 20, 2023
1 parent 83fd5c6 commit fc42419
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/HttpDI/HttpExtension.php
Expand Up @@ -114,7 +114,7 @@ private function sendHeaders()
}

$value = self::buildPolicy($config->$key);
if (strpos($value, "'nonce'")) {
if (str_contains($value, "'nonce'")) {
$this->initialization->addBody('$cspNonce = base64_encode(random_bytes(16));');
$value = Nette\DI\ContainerBuilder::literal(
'str_replace(?, ? . $cspNonce, ?)',
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Context.php
Expand Up @@ -50,7 +50,7 @@ public function isModified(string|int|\DateTimeInterface|null $lastModified = nu
} elseif ($ifNoneMatch !== null) {
$etag = $this->response->getHeader('ETag');

if ($etag === null || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === false) {
if ($etag === null || !str_contains(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag)) {
return true;

} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Helpers.php
Expand Up @@ -54,6 +54,6 @@ public static function ipMatch(string $ip, string $mask): bool

public static function initCookie(IRequest $request, IResponse $response)
{
$response->setCookie(self::StrictCookieName, '1', 0, '/', null, null, true, IResponse::SameSiteStrict);
$response->setCookie(self::StrictCookieName, '1', 0, '/', sameSite: IResponse::SameSiteStrict);
}
}
8 changes: 4 additions & 4 deletions src/Http/RequestFactory.php
Expand Up @@ -159,7 +159,7 @@ private function getGetPostCookie(Url $url): array
$list[$key][$k] = (string) preg_replace('#[^' . self::ValidChars . ']+#u', '', $v);

} else {
throw new Nette\InvalidStateException(sprintf('Invalid value in $_POST/$_COOKIE in key %s, expected string, %s given.', "'$k'", gettype($v)));
throw new Nette\InvalidStateException(sprintf('Invalid value in $_POST/$_COOKIE in key %s, expected string, %s given.', "'$k'", get_debug_type($v)));
}
}
}
Expand Down Expand Up @@ -301,9 +301,9 @@ private function useForwardedProxy(Url $url): ?string

if (isset($proxyParams['for'])) {
$address = $proxyParams['for'][0];
$remoteAddr = strpos($address, '[') === false
? explode(':', $address)[0] // IPv4
: substr($address, 1, strpos($address, ']') - 1); // IPv6
$remoteAddr = str_contains($address, '[')
? substr($address, 1, strpos($address, ']') - 1) // IPv6
: explode(':', $address)[0]; // IPv4
}

if (isset($proxyParams['proto']) && count($proxyParams['proto']) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Expand Up @@ -229,7 +229,7 @@ public function __destruct()
{
if (
self::$fixIE
&& strpos($_SERVER['HTTP_USER_AGENT'] ?? '', 'MSIE ') !== false
&& str_contains($_SERVER['HTTP_USER_AGENT'] ?? '', 'MSIE ')
&& in_array($this->code, [400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505], true)
&& preg_match('#^text/html(?:;|$)#', (string) $this->getHeader('Content-Type'))
) {
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Url.php
Expand Up @@ -177,7 +177,7 @@ public function getDefaultPort(): ?int
public function setPath(string $path): static
{
$this->path = $path;
if ($this->host && substr($this->path, 0, 1) !== '/') {
if ($this->host && !str_starts_with($this->path, '/')) {
$this->path = '/' . $this->path;
}

Expand Down Expand Up @@ -366,7 +366,7 @@ final public function export(): array
*/
private static function idnHostToUnicode(string $host): string
{
if (strpos($host, '--') === false) { // host does not contain IDN
if (!str_contains($host, '--')) { // host does not contain IDN
return $host;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/UrlImmutable.php
Expand Up @@ -288,7 +288,7 @@ final public function export(): array

protected function build(): void
{
if ($this->host && substr($this->path, 0, 1) !== '/') {
if ($this->host && !str_starts_with($this->path, '/')) {
$this->path = '/' . $this->path;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Http/Request.detectLanguage.phpt
Expand Up @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php';

test('', function () {
$headers = ['Accept-Language' => 'en, cs'];
$request = new Http\Request(new Http\UrlScript, null, null, null, $headers);
$request = new Http\Request(new Http\UrlScript, headers: $headers);

Assert::same('en', $request->detectLanguage(['en', 'cs']));
Assert::same('en', $request->detectLanguage(['cs', 'en']));
Expand All @@ -24,7 +24,7 @@ test('', function () {

test('', function () {
$headers = ['Accept-Language' => 'da, en-gb;q=0.8, en;q=0.7'];
$request = new Http\Request(new Http\UrlScript, null, null, null, $headers);
$request = new Http\Request(new Http\UrlScript, headers: $headers);

Assert::same('en-gb', $request->detectLanguage(['en', 'en-gb']));
Assert::same('en', $request->detectLanguage(['en']));
Expand All @@ -33,15 +33,15 @@ test('', function () {

test('', function () {
$headers = [];
$request = new Http\Request(new Http\UrlScript, null, null, null, $headers);
$request = new Http\Request(new Http\UrlScript, headers: $headers);

Assert::null($request->detectLanguage(['en']));
});


test('', function () {
$headers = ['Accept-Language' => 'garbage'];
$request = new Http\Request(new Http\UrlScript, null, null, null, $headers);
$request = new Http\Request(new Http\UrlScript, headers: $headers);

Assert::null($request->detectLanguage(['en']));
});
4 changes: 2 additions & 2 deletions tests/Http/Request.getOrigin.phpt
Expand Up @@ -16,15 +16,15 @@ test('missing origin', function () {


test('opaque origin', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, [
$request = new Http\Request(new Http\UrlScript, headers: [
'Origin' => 'null',
]);
Assert::null($request->getOrigin());
});


test('normal origin', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, [
$request = new Http\Request(new Http\UrlScript, headers: [
'Origin' => 'https://nette.org',
]);
Assert::equal(new UrlImmutable('https://nette.org'), $request->getOrigin());
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Request.getRawBody.phpt
Expand Up @@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';


test('', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, null, null, null, null, fn() => 'raw body');
$request = new Http\Request(new Http\UrlScript, rawBodyCallback: fn() => 'raw body');

Assert::same('raw body', $request->getRawBody());
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Request.headers.phpt
Expand Up @@ -18,12 +18,12 @@ test('', function () {
});

test('', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, []);
$request = new Http\Request(new Http\UrlScript);
Assert::same([], $request->getHeaders());
});

test('', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, [
$request = new Http\Request(new Http\UrlScript, headers: [
'one' => '1',
'TWO' => '2',
'X-Header' => 'X',
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Request.invalidType.phpt
Expand Up @@ -19,7 +19,7 @@ test('invalid POST', function () {

Assert::exception(function () {
(new Http\RequestFactory)->fromGlobals();
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'int\', expected string, integer given.');
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'int\', expected string, int given.');
});


Expand All @@ -29,5 +29,5 @@ test('invalid COOKIE', function () {

Assert::exception(function () {
(new Http\RequestFactory)->fromGlobals();
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'0\', expected string, integer given.');
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'0\', expected string, int given.');
});

0 comments on commit fc42419

Please sign in to comment.