Skip to content

Commit

Permalink
Modernize code (#14)
Browse files Browse the repository at this point in the history
* Update phpstan dependency version

* Remove unused code

* Modernize code
  • Loading branch information
olivervogel authored Jan 5, 2025
1 parent 2dfe8f8 commit e4da42e
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require-dev": {
"phpunit/phpunit": "^10.0 || ^11.0",
"phpstan/phpstan": "^2",
"phpstan/phpstan": "^2.1",
"squizlabs/php_codesniffer": "^3.8",
"slevomat/coding-standard": "~8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" failOnWarning="true">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/Unit/</directory>
Expand Down
8 changes: 5 additions & 3 deletions src/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function __construct(protected array $parameters = [])
*/
public function __toString(): string
{
return implode(', ', array_map(function ($key, $value) {
return sprintf('%s="%s"', $key, $value);
}, array_keys($this->parameters), $this->parameters));
return implode(', ', array_map(
fn(mixed $key, mixed $value): string => sprintf('%s="%s"', $key, $value),
array_keys($this->parameters),
$this->parameters,
));
}
}
4 changes: 2 additions & 2 deletions src/Tokens/HttpAuthentification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function parse(): array
throw new AuthentificationException('Failed to parse token.');
}

if (strtolower(substr($value, 0, 5)) !== 'basic') {
if (strtolower(substr((string) $value, 0, 5)) !== 'basic') {
throw new AuthentificationException('Failed to parse token.');
}

$data = explode(':', base64_decode(substr($value, 6)));
$data = explode(':', base64_decode(substr((string) $value, 6)));

return [
'username' => $this->getArrayValue($data, 0),
Expand Down
6 changes: 3 additions & 3 deletions src/Tokens/HttpAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public function parse(): array
throw new AuthentificationException('Failed to parse token.');
}

if (strtolower(substr($value, 0, 6)) !== 'digest') {
if (strtolower(substr((string) $value, 0, 6)) !== 'digest') {
throw new AuthentificationException('Failed to parse token.');
}

preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $value, $matches, PREG_SET_ORDER);
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', (string) $value, $matches, PREG_SET_ORDER);

$properties = [];
foreach ($matches as $m) {
$key = $m[1];
$value = $m[2] ? $m[2] : $m[3];
$value = $m[2] ?: $m[3];
$properties[$key] = $value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Tokens/PhpAuthDigest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function parse(): array
throw new AuthentificationException('Failed to parse token.');
}

preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $value, $matches, PREG_SET_ORDER);
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', (string) $value, $matches, PREG_SET_ORDER);

$properties = [];
foreach ($matches as $m) {
$key = $m[1];
$value = $m[2] ? $m[2] : $m[3];
$value = $m[2] ?: $m[3];
$properties[$key] = $value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Tokens/RedirectHttpAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function parse(): array
throw new AuthentificationException('Failed to parse token.');
}

if (strtolower(substr($value, 0, 5)) !== 'basic') {
if (strtolower(substr((string) $value, 0, 5)) !== 'basic') {
throw new AuthentificationException('Failed to parse token.');
}

list($username, $password) = explode(':', base64_decode(substr($value, 6)));
[$username, $password] = explode(':', base64_decode(substr((string) $value, 6)));

return [
'username' => $username,
Expand Down
4 changes: 2 additions & 2 deletions src/Vaults/AbstractVault.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public function setCredentials(string $username, string $password): self
*/
protected function denyAccess(?string $message = null): void
{
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
$protocol = $_SERVER['SERVER_PROTOCOL'] ?: 'HTTP/1.1';
$message = empty($message) ? '<strong>' . $protocol . ' 401 Unauthorized</strong>' : $message;

header($protocol . ' 401 Unauthorized');
header('WWW-Authenticate: ' . $this->type()->value . ' ' . (string) $this->directive());
header('WWW-Authenticate: ' . $this->type()->value . ' ' . $this->directive());
exit($message);
}
}

0 comments on commit e4da42e

Please sign in to comment.