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

fix: Introduce Event::fromArray #25

Closed
wants to merge 3 commits into from
Closed
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
70 changes: 58 additions & 12 deletions src/Amplitude/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,86 @@

use RuntimeException;

/**
* @phpstan-type Payload array{
* event_type: string,
* event_properties?: array<mixed>|null,
* user_properties?: array<mixed>|null,
* user_id?: string|null,
* device_id?: string|null,
* insert_id?: string|null,
* time?: int|null
* }
*/
class Event
{
public ?string $eventType = null;
public ?string $eventType;
/**
* @var ?array<mixed>
* @var array<mixed>|null
*/
public ?array $eventProperties = null;
public ?array $eventProperties;
/**
* @var ?array<mixed>
* @var array<mixed>|null
*/
public ?array $userProperties = null;
public ?string $userId = null;
public ?string $deviceId = null;
public ?string $insertId = null;
public ?int $time = null;
public ?array $userProperties;
public ?string $userId;
public ?string $deviceId;
public ?string $insertId;
public ?int $time;

public function __construct(string $eventType)
/**
* @param array<mixed>|null $eventProperties
* @param array<mixed>|null $userProperties
*/
public function __construct(
string $eventType,
?array $eventProperties = null,
?array $userProperties = null,
?string $userId = null,
?string $deviceId = null,
?string $insertId = null,
?int $time = null
)
{
$this->eventType = $eventType;
$this->eventProperties = $eventProperties;
$this->userProperties = $userProperties;
$this->userId = $userId;
$this->deviceId = $deviceId;
$this->insertId = $insertId;
$this->time = $time;
}

/**
* @return array<string, mixed>
* @return Payload
*/
public function toArray(): array
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested update:

public function toArray(): array
    {
        return array_filter([
            'event_type' => $this->eventType,
            'event_properties' => $this->eventProperties,
            'user_properties' => $this->userProperties,
            'user_id' => $this->userId,
            'device_id' => $this->deviceId,
            'insert_id' => $this->insertId,
            'time' => $this->time,
        ], fn($value) => $value !== null);
    }

Copy link
Author

Choose a reason for hiding this comment

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

When you don't pass a callable to array_filter, it does filter anything that is null. So there is no point in providing this callable, as it does the same, but only being explicit.

{
return array_filter([

Check failure on line 62 in src/Amplitude/Event.php

View workflow job for this annotation

GitHub Actions / test (7.4, ubuntu-latest)

Method AmplitudeExperiment\Amplitude\Event::toArray() should return array{event_type: string, event_properties?: array|null, user_properties?: array|null, user_id?: string|null, device_id?: string|null, insert_id?: string|null, time?: int|null} but returns array{event_type?: non-falsy-string, event_properties?: non-empty-array, user_properties?: non-empty-array, user_id?: non-falsy-string, device_id?: non-falsy-string, insert_id?: non-falsy-string, time?: int<min, -1>|int<1, max>}.

Check failure on line 62 in src/Amplitude/Event.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest)

Method AmplitudeExperiment\Amplitude\Event::toArray() should return array{event_type: string, event_properties?: array|null, user_properties?: array|null, user_id?: string|null, device_id?: string|null, insert_id?: string|null, time?: int|null} but returns array{event_type?: non-falsy-string, event_properties?: non-empty-array, user_properties?: non-empty-array, user_id?: non-falsy-string, device_id?: non-falsy-string, insert_id?: non-falsy-string, time?: int<min, -1>|int<1, max>}.

Check failure on line 62 in src/Amplitude/Event.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest)

Method AmplitudeExperiment\Amplitude\Event::toArray() should return array{event_type: string, event_properties?: array|null, user_properties?: array|null, user_id?: string|null, device_id?: string|null, insert_id?: string|null, time?: int|null} but returns array{event_type?: non-falsy-string, event_properties?: non-empty-array, user_properties?: non-empty-array, user_id?: non-falsy-string, device_id?: non-falsy-string, insert_id?: non-falsy-string, time?: int<min, -1>|int<1, max>}.

Check failure on line 62 in src/Amplitude/Event.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest)

Method AmplitudeExperiment\Amplitude\Event::toArray() should return array{event_type: string, event_properties?: array|null, user_properties?: array|null, user_id?: string|null, device_id?: string|null, insert_id?: string|null, time?: int|null} but returns array{event_type?: non-falsy-string, event_properties?: non-empty-array, user_properties?: non-empty-array, user_id?: non-falsy-string, device_id?: non-falsy-string, insert_id?: non-falsy-string, time?: int<min, -1>|int<1, max>}.

Check failure on line 62 in src/Amplitude/Event.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest)

Method AmplitudeExperiment\Amplitude\Event::toArray() should return array{event_type: string, event_properties?: array|null, user_properties?: array|null, user_id?: string|null, device_id?: string|null, insert_id?: string|null, time?: int|null} but returns array{event_type?: non-falsy-string, event_properties?: non-empty-array, user_properties?: non-empty-array, user_id?: non-falsy-string, device_id?: non-falsy-string, insert_id?: non-falsy-string, time?: int<min, -1>|int<1, max>}.
'event_type' => $this->eventType,
'event_properties' => $this->eventProperties,
'user_properties' => $this->userProperties,
'user_id' => $this->userId,
'device_id' => $this->deviceId,
'insert_id' => $this->insertId,
'time' => $this->time]);
'time' => $this->time
]);
}

/**
* @param Payload $data
*/
public static function fromArray(array $data) : self
{
return new self(
$data['event_type'],
$data['event_properties'] ?? null,
$data['user_properties'] ?? null,
$data['user_id'] ?? null,
$data['device_id'] ?? null,
$data['insert_id'] ?? null,
$data['time'] ?? null
);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Amplitude/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AmplitudeExperiment\Test\Amplitude;

use AmplitudeExperiment\Amplitude\Event;
use PHPUnit\Framework\TestCase;

final class EventTest extends TestCase
{
public function testToAndFromArray(): void
{
$event = new Event(
'eventType',
['eventProperty' => 'eventValue'],
['userProperty' => 'userValue'],
'userId',
'deviceId',
'insertId',
1234567890
);

$this->assertEquals(
[
'event_type' => 'eventType',
'event_properties' => ['eventProperty' => 'eventValue'],
'user_properties' => ['userProperty' => 'userValue'],
'user_id' => 'userId',
'device_id' => 'deviceId',
'insert_id' => 'insertId',
'time' => 1234567890
],
$event->toArray()
);

$this->assertEquals(
$event,
Event::fromArray($event->toArray())
);
}
}
Loading