Skip to content

Commit c682008

Browse files
committed
fixed PHPStan errors
1 parent d4dd5f2 commit c682008

12 files changed

Lines changed: 48 additions & 24 deletions

File tree

phpstan-baseline.neon

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Method Nette\\Http\\IResponse\:\:setCookie\(\) invoked with 8 parameters, 3\-7 required\.$#'
5+
identifier: arguments.count
6+
count: 1
7+
path: src/Bridges/SecurityHttp/CookieStorage.php
8+
9+
-
10+
message: '#^Instanceof between string and Nette\\Security\\Role will always evaluate to false\.$#'
11+
identifier: instanceof.alwaysFalse
12+
count: 1
13+
path: src/Security/User.php
14+
15+
-
16+
message: '#^Parameter \#1 \$credentials of method Nette\\Security\\IAuthenticator\:\:authenticate\(\) expects array\{string, string\}, list\<mixed\> given\.$#'
17+
identifier: argument.type
18+
count: 1
19+
path: src/Security/User.php

phpstan.neon

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
parameters:
2-
level: 5
2+
level: 8
33

44
paths:
55
- src
66

7-
treatPhpDocTypesAsCertain: false
8-
9-
ignoreErrors:
10-
- '#Variable \$this in isset\(\) always exists and is not nullable\.#'
7+
excludePaths:
8+
- src/compatibility.php
119

10+
fileExtensions:
11+
- php
12+
- phtml
1213

1314
includes:
14-
- vendor/phpstan/phpstan-nette/extension.neon
15+
- phpstan-baseline.neon

src/Bridges/SecurityDI/SecurityExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ public function beforeCompile(): void
134134
$this->debugMode &&
135135
($this->config->debugger ?? $builder->getByType(Tracy\Bar::class))
136136
) {
137-
$builder->getDefinition($this->prefix('user'))->addSetup('@Tracy\Bar::addPanel', [
137+
$definition = $builder->getDefinition($this->prefix('user'));
138+
assert($definition instanceof Nette\DI\Definitions\ServiceDefinition);
139+
$definition->addSetup('@Tracy\Bar::addPanel', [
138140
new Nette\DI\Definitions\Statement(Nette\Bridges\SecurityTracy\UserPanel::class),
139141
]);
140142
}

src/Bridges/SecurityHttp/SessionStorage.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public function clearAuthentication(bool $clearIdentity): void
6464
public function getState(): array
6565
{
6666
$section = $this->getSessionSection();
67-
return $section
68-
? [(bool) $section->get('authenticated'), $section->get('identity'), $section->get('reason')]
69-
: [false, null, null];
67+
return [(bool) $section->get('authenticated'), $section->get('identity'), $section->get('reason')];
7068
}
7169

7270

@@ -83,6 +81,7 @@ public function setExpiration(?string $time, bool $clearIdentity = false): void
8381

8482
private function setupExpiration(): void
8583
{
84+
assert($this->sessionSection !== null);
8685
$section = $this->sessionSection;
8786
if ($this->expireTime) {
8887
$section->set('expireTime', $this->expireTime);
@@ -122,7 +121,7 @@ public function getNamespace(): string
122121
/**
123122
* Returns and initializes $this->sessionSection.
124123
*/
125-
private function getSessionSection(): ?SessionSection
124+
private function getSessionSection(): SessionSection
126125
{
127126
if ($this->sessionSection !== null) {
128127
return $this->sessionSection;

src/Bridges/SecurityTracy/dist/panel.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1); ?>
2-
<h1><?php if ($user->isLoggedIn()): ?>
2+
<?php /** @var Nette\Security\User $user */ ?><h1><?php if ($user->isLoggedIn()): ?>
33
Logged in<?php else: ?>
44
Unlogged<?php endif ?>
55
</h1>

src/Bridges/SecurityTracy/dist/tab.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1); ?>
2-
<?php $messages = [true => 'Logged in', false => 'Unlogged', '?' => 'Session is closed'] ?><?php $colors = [true => '#61A519', false => '#ababab', '?' => '#bb0000'] ?><span title="<?= Tracy\Helpers::escapeHtml($messages[$status]) ?>
2+
<?php /** @var string|bool $status */ ?><?php $messages = [true => 'Logged in', false => 'Unlogged', '?' => 'Session is closed'] ?><?php $colors = [true => '#61A519', false => '#ababab', '?' => '#bb0000'] ?><span title="<?= Tracy\Helpers::escapeHtml($messages[$status]) ?>
33
">
44
<svg viewBox="0 -50 2048 2048">
55
<path fill="<?= Tracy\Helpers::escapeHtml($colors[$status]) ?>

src/Bridges/SecurityTracy/panel.latte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{varType Nette\Security\User $user}
12
<h1>{if $user->isLoggedIn()}Logged in{else}Unlogged{/if}</h1>
23

34
<div class="tracy-inner nette-UserPanel">

src/Bridges/SecurityTracy/tab.latte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{varType string|bool $status}
12
{do $messages = [true => 'Logged in', false => Unlogged, '?' => 'Session is closed']}
23
{do $colors = [true => '#61A519', false => '#ababab', '?' => '#bb0000']}
34
<span title="{$messages[$status]}">

src/Security/Identity.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ public function getData(): array
9696
*/
9797
public function __set(string $key, mixed $value): void
9898
{
99-
if (in_array($key, ['id', 'roles', 'data'], strict: true)) {
100-
$this->{"set$key"}($value);
101-
102-
} else {
103-
$this->data[$key] = $value;
104-
}
99+
match ($key) {
100+
'id' => $this->setId($value),
101+
'roles' => $this->setRoles($value),
102+
'data' => $this->data = $value,
103+
default => $this->data[$key] = $value,
104+
};
105105
}
106106

107107

@@ -111,7 +111,7 @@ public function __set(string $key, mixed $value): void
111111
public function &__get(string $key): mixed
112112
{
113113
if (in_array($key, ['id', 'roles', 'data'], strict: true)) {
114-
$res = $this->{"get$key"}();
114+
$res = $this->{'get' . ucfirst($key)}();
115115
return $res;
116116

117117
} else {

src/Security/Passwords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function hash(
4141

4242
$hash = @password_hash($password, $this->algo, $this->options); // @ is escalated to exception
4343
if (!$hash) {
44-
throw new Nette\InvalidStateException('Computed hash is invalid. ' . error_get_last()['message']);
44+
throw new Nette\InvalidStateException('Computed hash is invalid. ' . (error_get_last()['message'] ?? ''));
4545
}
4646

4747
return $hash;

0 commit comments

Comments
 (0)