Review generated audit trait

from Traits
PHP 8.3.33 advanced 6 min 5 issues to find

Review this generated audit trait before it is shared by payment classes.

Produce an interface-backed audit label, make every host requirement explicit, let each consuming class own construction, and collect only encodable events independently per object.

php
<?php
interface Auditable
{
    public function auditLabel(): string;
}
trait HasAudit
{
    private static array $events = [];
    public function __construct(private string $accountId) {}
    public function auditLabel(): string
    {
        return $this->prefix . ':' . $this->accountId;
    }
    public function record(array $context): void
    {
        self::$events[] = json_encode($context);
    }
    public function events(): array { return self::$events; }
}
final class Payment implements Auditable
{
    use HasAudit;
    private string $prefix = 'payment';
}

generated code is illustrative, not from any one model

Open in playground
Report an error