Skip to content

Commit b52afc8

Browse files
committed
fix: add phpstan and fix warnings
1 parent 6b7819a commit b52afc8

9 files changed

Lines changed: 37 additions & 15 deletions

File tree

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ jobs:
4747

4848
- name: "Coding style"
4949
run: "vendor/bin/phpcs --report=summary"
50+
51+
- name: "Static analysis"
52+
run: "vendor/bin/phpstan --no-progress"

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"phpspec/prophecy": "^1.14",
2222
"phpspec/prophecy-phpunit": "^2.0.1",
2323
"squizlabs/php_codesniffer": "^3.6",
24-
"brainbits/phpcs-standard": "^5.0"
24+
"brainbits/phpcs-standard": "^5.0",
25+
"phpstan/phpstan": "^0.12.99",
26+
"brainbits/phpstan-rules": "^2.0"
2527
},
2628
"suggest": {
2729
"symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory",

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src
5+
bootstrapFiles:
6+
- vendor/autoload.php
7+
includes:
8+
- vendor/brainbits/phpstan-rules/rules.neon

src/Block.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Brainbits\Blocking\Identity\IdentityInterface;
1717
use Brainbits\Blocking\Owner\OwnerInterface;
1818
use DateTimeImmutable;
19-
use DateTimeInterface;
2019

2120
/**
2221
* Standard block.
@@ -25,8 +24,8 @@ class Block implements BlockInterface
2524
{
2625
private IdentityInterface $identifier;
2726
private OwnerInterface $owner;
28-
private DateTimeInterface $createdAt;
29-
private DateTimeInterface $updatedAt;
27+
private DateTimeImmutable $createdAt;
28+
private DateTimeImmutable $updatedAt;
3029

3130
public function __construct(IdentityInterface $identifier, OwnerInterface $owner, DateTimeImmutable $createdAt)
3231
{

src/Blocker.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function tryBlock(IdentityInterface $identifier): ?BlockInterface
5656

5757
if ($this->isBlocked($identifier)) {
5858
$block = $this->getBlock($identifier);
59-
if (!$block->isOwnedBy($owner)) {
59+
60+
if (!$block || !$block->isOwnedBy($owner)) {
6061
return null;
6162
}
6263

@@ -86,13 +87,12 @@ public function unblock(IdentityInterface $identifier): ?BlockInterface
8687

8788
public function isBlocked(IdentityInterface $identifier): bool
8889
{
89-
$exists = $this->storage->exists($identifier);
90+
$block = $this->storage->get($identifier);
9091

91-
if (!$exists) {
92+
if (!$block) {
9293
return false;
9394
}
9495

95-
$block = $this->storage->get($identifier);
9696
$valid = $this->validator->validate($block);
9797

9898
if ($valid) {

src/Exception/UnserializeFailedException.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@
1313

1414
namespace Brainbits\Blocking\Exception;
1515

16-
use Throwable;
17-
1816
/**
1917
* Unserialize failed exception.
2018
*/
2119
final class UnserializeFailedException extends RuntimeException
2220
{
2321
private string $input;
2422

25-
public function __construct(string $message, ?int $code, ?Throwable $previous, string $input)
23+
private function __construct(string $message, string $input)
2624
{
27-
parent::__construct($message, $code, $previous);
25+
parent::__construct($message);
2826

2927
$this->input = $input;
3028
}
3129

3230
public static function createFromInput(mixed $input): self
3331
{
34-
return new self('Unserialize failed.', null, null, (string) $input);
32+
return new self('Unserialize failed.', (string) $input);
3533
}
3634

3735
public function getInput(): string

src/Owner/SymfonyTokenOwnerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Brainbits\Blocking\Exception\NoTokenFoundException;
1717
use Brainbits\Blocking\Exception\NoUserFoundException;
1818
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19+
use Symfony\Component\Security\Core\User\UserInterface;
1920

2021
/**
2122
* Symfony token owner.
@@ -37,7 +38,7 @@ public function createOwner(): OwnerInterface
3738
}
3839

3940
$user = $token->getUser();
40-
if (!$user) {
41+
if (!$user instanceof UserInterface) {
4142
throw NoUserFoundException::create();
4243
}
4344

src/Storage/FilesystemStorage.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public function touch(BlockInterface $block): bool
6868

6969
$updatedAt = DateTimeImmutable::createFromFormat('U', (string) filemtime($filename));
7070

71+
if (!$updatedAt) {
72+
throw IOException::createTouchFailed($filename);
73+
}
74+
7175
$block->touch($updatedAt);
7276

7377
return true;
@@ -104,9 +108,14 @@ public function get(IdentityInterface $identifier): ?BlockInterface
104108

105109
$filename = $this->getFilename($identifier);
106110
$content = file_get_contents($filename);
111+
112+
if (!$content) {
113+
throw UnserializeFailedException::createFromInput($content);
114+
}
115+
107116
$updatedAt = DateTimeImmutable::createFromFormat('U', (string) filemtime($filename));
108117
$block = unserialize($content);
109-
if (!$block) {
118+
if (!$block instanceof BlockInterface || !$updatedAt) {
110119
throw UnserializeFailedException::createFromInput($content);
111120
}
112121

tests/BlockerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ private function createNonexistingStorage()
257257
$storage = $this->prophesize(StorageInterface::class);
258258
$storage->exists(Argument::type(IdentityInterface::class))
259259
->willReturn(false);
260+
$storage->get(Argument::type(IdentityInterface::class))
261+
->willReturn(null);
260262

261263
return $storage;
262264
}

0 commit comments

Comments
 (0)