Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use readonly class annotation where possible #593

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Storageless/Http/ClientFingerprint/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace PSR7Sessions\Storageless\Http\ClientFingerprint;

/** @immutable */
final class Configuration
/** @psalm-immutable */
final readonly class Configuration
{
/** @var list<Source> */
private readonly array $sources;
private array $sources;

/**
* @param list<Source> ...$sources
Expand Down
4 changes: 2 additions & 2 deletions src/Storageless/Http/ClientFingerprint/RemoteAddr.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use function array_key_exists;
use function is_string;

/** @immutable */
final class RemoteAddr implements Source
/** @psalm-immutable */
final readonly class RemoteAddr implements Source
{
private const SERVER_PARAM_NAME = 'REMOTE_ADDR';

Expand Down
10 changes: 5 additions & 5 deletions src/Storageless/Http/ClientFingerprint/SameOriginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
/**
* @internal
*
* @immutable
* @psalm-immutable
*/
final class SameOriginRequest implements Constraint
final readonly class SameOriginRequest implements Constraint
{
public const CLAIM = 'fp';

/** @var list<Source> */
private readonly array $sources;
private array $sources;
/** @var non-empty-string */
private readonly string $currentRequestFingerprint;
private string $currentRequestFingerprint;

public function __construct(
private readonly Configuration $configuration,
private Configuration $configuration,
ServerRequestInterface $serverRequest,
) {
$this->sources = $this->configuration->sources();
Expand Down
4 changes: 2 additions & 2 deletions src/Storageless/Http/ClientFingerprint/UserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Psr\Http\Message\ServerRequestInterface;

/** @immutable */
final class UserAgent implements Source
/** @psalm-immutable */
final readonly class UserAgent implements Source
{
public function extractFrom(ServerRequestInterface $request): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Storageless/Http/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Lcobucci\JWT\Configuration as JwtConfig;
use PSR7Sessions\Storageless\Http\ClientFingerprint\Configuration as FingerprintConfig;

/** @immutable */
/** @psalm-immutable */
final class Configuration
{
private JwtConfig $jwtConfiguration;
Expand Down
6 changes: 3 additions & 3 deletions src/Storageless/Http/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@

use function sprintf;

/** @immutable */
final class SessionMiddleware implements MiddlewareInterface
/** @psalm-immutable */
final readonly class SessionMiddleware implements MiddlewareInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful: readonly != @immutable. @immutable implies readonly + no side-effects.

Copy link
Member Author

@Slamdunk Slamdunk Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a sad naming choice from Psalm that hunted me since its introduction.
I know no other tool that treats readonly differently from @immutable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't know of other tools that enforce immutability right now, which is why I keep sticking to psalm (for now) :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation restored with the more specific @psalm-immutable name

{
public const SESSION_CLAIM = 'session-data';
public const SESSION_ATTRIBUTE = 'session';

public function __construct(
private readonly Configuration $config,
private Configuration $config,
) {
}

Expand Down