Skip to content

Commit 03c8c62

Browse files
committed
fixed PHPStan errors
1 parent ab30fe2 commit 03c8c62

12 files changed

Lines changed: 79 additions & 36 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;
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
<?php declare(strict_types=1); ?>
2-
<h1><?php if ($user->isLoggedIn()): ?>
3-
Logged in<?php else: ?>
4-
Unlogged<?php endif ?>
5-
</h1>
1+
<?php declare(strict_types=1);
62

7-
<div class="tracy-inner nette-UserPanel">
8-
<?php if ($user->getIdentity()): ?> <?= Tracy\Dumper::toHtml($user->getIdentity(), [Tracy\Dumper::LIVE => true]) ?>
3+
/** @var Nette\Security\User $user */
4+
echo '<h1>';
5+
if ($user->isLoggedIn()) /* pos 2:5 */ {
6+
echo 'Logged in';
7+
} else /* pos 2:38 */ {
8+
echo 'Unlogged';
9+
}
10+
echo '</h1>
911
10-
<?php else: ?> <p>no identity</p>
11-
<?php endif ?></div>
12+
<div class="tracy-inner nette-UserPanel">
13+
';
14+
if ($user->getIdentity()) /* pos 5:2 */ {
15+
echo ' ';
16+
echo Tracy\Dumper::toHtml($user->getIdentity(), [Tracy\Dumper::LIVE => true]) /* pos 6:3 */;
17+
echo "\n";
18+
} else /* pos 7:2 */ {
19+
echo ' <p>no identity</p>
20+
';
21+
}
22+
echo '</div>
23+
';
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
<?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]) ?>
3-
">
1+
<?php declare(strict_types=1);
2+
3+
/** @var string|bool $status */
4+
$messages = [true => 'Logged in', false => 'Unlogged', '?' => 'Session is closed'] /* pos 2:1 */;
5+
$colors = [true => '#61A519', false => '#ababab', '?' => '#bb0000'] /* pos 3:1 */;
6+
echo '<span';
7+
echo ($ʟ_tmp = ($messages[$status])) === null ? '' : ' title="' . Tracy\Helpers::escapeHtml($ʟ_tmp) . '"' /* pos 4:15 */;
8+
echo '>
49
<svg viewBox="0 -50 2048 2048">
5-
<path fill="<?= Tracy\Helpers::escapeHtml($colors[$status]) ?>
6-
" d="m1615 1803.5c-122 17-246 7-369 8-255 1-510 3-765-1-136-2-266-111-273-250-11-192 11-290.5 115-457.5 62-100 192-191 303-147 110 44 201 130 321 149 160 25 317-39 446-130 82-58 200-9 268 51 157 173 186.8 275.49 184 484.49-1.9692 147.11-108.91 271.41-230 293zm-144-1226.5c0 239-208 447-447 447s-447-208-447-447 208-447 447-447c240 1 446 207 447 447z"/>
10+
<path';
11+
echo ($ʟ_tmp = ($colors[$status])) === null ? '' : ' fill="' . Tracy\Helpers::escapeHtml($ʟ_tmp) . '"' /* pos 6:16 */;
12+
echo ' d="m1615 1803.5c-122 17-246 7-369 8-255 1-510 3-765-1-136-2-266-111-273-250-11-192 11-290.5 115-457.5 62-100 192-191 303-147 110 44 201 130 321 149 160 25 317-39 446-130 82-58 200-9 268 51 157 173 186.8 275.49 184 484.49-1.9692 147.11-108.91 271.41-230 293zm-144-1226.5c0 239-208 447-447 447s-447-208-447-447 208-447 447-447c240 1 446 207 447 447z"></path>
713
</svg>
814
</span>
15+
';

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
@@ -40,7 +40,7 @@ public function hash(
4040

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

4646
return $hash;

0 commit comments

Comments
 (0)