Skip to content

Commit

Permalink
Updating to symfony 6 and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderVerkuil committed Feb 20, 2024
1 parent 9cac010 commit 690299a
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 82 deletions.
33 changes: 18 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"php": "^8.0",
"jean85/pretty-package-versions": "^1.0|^2.0",
"posthog/posthog-php": "^3.0",
"symfony/config": "^5.4.0|^6.0",
"symfony/console": "^5.4.0|^6.0",
"symfony/dependency-injection": "^5.4.0|^6.0",
"symfony/event-dispatcher": "^5.4.0|^6.0",
"symfony/http-kernel": "^5.4.0|^6.0",
"symfony/config": "^6.0|^7.0",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/event-dispatcher": "^6.0|^7.0",
"symfony/http-kernel": "^6.0|^7.0",
"symfony/polyfill-php80": "^1.28",
"symfony/security-core": "^5.4.0|^6.0",
"symfony/security-http": "^5.4.0|^6.0"
"symfony/security-core": "^6.0|^7.0",
"symfony/security-http": "^6.0|^7.0"
},
"license": "MIT",
"autoload": {
Expand Down Expand Up @@ -45,15 +44,16 @@
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^8.5.14|^9.3.9|^10.4",
"symfony/browser-kit": "^5.4.0|^6.0",
"symfony/cache": "^5.4.0|^6.0",
"symfony/dom-crawler": "^5.4.0|^6.0",
"symfony/framework-bundle": "^5.4.0|^6.0",
"symfony/http-client": "^5.4.0|^6.0",
"symfony/browser-kit": "^6.0|^7.0",
"symfony/cache": "^6.0|^7.0",
"symfony/console": "^6.0|^7.0",
"symfony/dom-crawler": "^6.0|^7.0",
"symfony/framework-bundle": "^6.0|^7.0",
"symfony/http-client": "^6.0|^7.0",
"symfony/monolog-bundle": "^3.4",
"symfony/phpunit-bridge": "^5.4.0|^6.0",
"symfony/process": "^5.4.0|^6.0",
"symfony/yaml": "^5.4.0|^6.0",
"symfony/phpunit-bridge": "^6.0|^7.0",
"symfony/process": "^6.0|^7.0",
"symfony/yaml": "^6.0|^7.0",
"vimeo/psalm": "^5.15"
},
"scripts": {
Expand All @@ -63,6 +63,9 @@
"phpcs": [
"vendor/bin/php-cs-fixer fix --verbose --diff --dry-run"
],
"phpcs:fix": [
"vendor/bin/php-cs-fixer fix"
],
"phpstan": [
"vendor/bin/phpstan analyse"
],
Expand Down
77 changes: 51 additions & 26 deletions src/Adapter/PostHogAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

use PostHog\Client;
use PostHog\PostHog as PH;
use PostHog\PostHogBundle\Exception\NotInitializedException;
use PostHog\PostHogBundle\Model\AliasMessage;
use PostHog\PostHogBundle\Model\GroupIdentifyMessage;
use PostHog\PostHogBundle\Model\IdentifyMessage;
use PostHog\PostHogBundle\Model\Message;
use PostHog\PostHogBundle\PostHogInterface;

class PostHogAdapter
class PostHogAdapter implements PostHogInterface
{
private Client $client;

Expand All @@ -18,66 +22,87 @@ public function __construct(Client $client)
PH::init(client: $this->client);
}

public function capture(array $message): bool
public function capture(Message $message): bool
{
return PH::capture($message);
return $this->client->capture($message->toArray());
}

public function identify(array $message): bool
public function identify(IdentifyMessage $message): bool
{
return PH::identify($message);
return $this->client->identify($message->toArray());
}

public function groupIdentify(array $message): bool
public function groupIdentify(GroupIdentifyMessage $message): bool
{
return PH::groupIdentify($message);
return $this->capture(new Message(
event: '$groupidentify',
distinctId: sprintf('$%s_%s', $message->groupType, $message->groupKey),
properties: [
'$group_type' => $message->groupType,
'$group_key' => $message->groupKey,
'$group_set' => $message->groupProperties,
],
));
}

public function isFeatureEnabled(string $key, string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false, bool $sendFeatureFlagEvents = true): null|bool
/**
* @throws \Exception
*/
public function isFeatureEnabled(string $key, string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false, bool $sendFeatureFlagEvents = true): bool|null
{
return PH::isFeatureEnabled($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return $this->client->isFeatureEnabled($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
}

public function getFeatureFlag(string $key, string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false, bool $sendFeatureFlagEvents = true): null|bool|string
/**
* @throws \Exception
*/
public function getFeatureFlag(string $key, string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false, bool $sendFeatureFlagEvents = true): bool|string|null
{
return PH::getFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return $this->client->getFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
}

public function getAllFlags(string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false)
/**
* @throws \Exception
*/
public function getAllFlags(string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = [], bool $onlyEvaluateLocally = false): array

Check failure on line 67 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnTypeCoercion

src/Adapter/PostHogAdapter.php:67:168: MixedReturnTypeCoercion: The declared return type 'array<array-key, string>' for PostHog\PostHogBundle\Adapter\PostHogAdapter::getAllFlags is more specific than the inferred return type 'array<array-key, mixed>' (see https://psalm.dev/197)
{
return PH::getAllFlags($distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally);
return $this->client->getAllFlags($distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally);

Check failure on line 69 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnTypeCoercion

src/Adapter/PostHogAdapter.php:69:16: MixedReturnTypeCoercion: The type 'array<array-key, mixed>' is more general than the declared return type 'array<array-key, string>' for PostHog\PostHogBundle\Adapter\PostHogAdapter::getAllFlags (see https://psalm.dev/197)
}

/**
* @throws \Exception
*/
public function fetchFeatureVariants(string $distinctId, array $groups = []): array

Check failure on line 75 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnTypeCoercion

src/Adapter/PostHogAdapter.php:75:83: MixedReturnTypeCoercion: The declared return type 'array<array-key, string>' for PostHog\PostHogBundle\Adapter\PostHogAdapter::fetchFeatureVariants is more specific than the inferred return type 'array<array-key, mixed>' (see https://psalm.dev/197)
{
return PH::fetchFeatureVariants($distinctId, $groups);
return $this->client->fetchFeatureVariants($distinctId, $groups);

Check failure on line 77 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnTypeCoercion

src/Adapter/PostHogAdapter.php:77:16: MixedReturnTypeCoercion: The type 'array<array-key, mixed>' is more general than the declared return type 'array<array-key, string>' for PostHog\PostHogBundle\Adapter\PostHogAdapter::fetchFeatureVariants (see https://psalm.dev/197)
}

public function alias(array $message): bool
public function alias(AliasMessage $message): bool
{
return PH::alias($message);
return $this->client->alias($message->toArray());

Check failure on line 82 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

src/Adapter/PostHogAdapter.php:82:37: MixedArgument: Argument 1 of PostHog\Client::alias cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
}

public function raw(array $message): bool
public function raw(array $message): mixed
{
return PH::raw($message);
return $this->client->raw($message);
}

/**
* @return bool
*/
public function flush(): bool
{
$result = PH::flush();
$result = $this->client->flush();

if (is_bool($result)) {
if (\is_bool($result)) {

Check failure on line 94 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

RedundantConditionGivenDocblockType

src/Adapter/PostHogAdapter.php:94:13: RedundantConditionGivenDocblockType: Docblock-defined type bool for $result is always bool (see https://psalm.dev/156)
return $result;
}

if (is_string($result)) {
if (\is_string($result)) {

Check failure on line 98 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unreachable statement - code above always terminates.

Check failure on line 98 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

TypeDoesNotContainType

src/Adapter/PostHogAdapter.php:98:13: TypeDoesNotContainType: Type never for $result is never string (see https://psalm.dev/056)
$decoded = json_decode($result, true);

Check failure on line 99 in src/Adapter/PostHogAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

NoValue

src/Adapter/PostHogAdapter.php:99:36: NoValue: All possible types for this argument were invalidated - This may be dead code (see https://psalm.dev/179)

return array_key_exists('status', $decoded);
if (!\is_array($decoded)) {
return true;
}

return \array_key_exists('status', $decoded);
}

return false;
Expand Down
6 changes: 6 additions & 0 deletions src/Client/Options/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ class Options
private bool $debug = false;
private int $timeout = 10000;

/**
* @var array<string, mixed>
*/
private array $consumerOptions = [];

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
$options = $this->consumerOptions;
Expand Down
24 changes: 15 additions & 9 deletions src/Command/PostHogTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace PostHog\PostHogBundle\Command;

use PostHog\PostHogBundle\Adapter\PostHogAdapter;
use PostHog\PostHogBundle\Model\IdentifyMessage;
use PostHog\PostHogBundle\Model\Message;
use PostHog\PostHogBundle\PostHogBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -13,21 +15,25 @@ class PostHogTestCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$posthog = PostHogAdapter::getInstance();
$posthog = PostHogBundle::getInstance();

$output->writeln('Identifying...');
$whoami = shell_exec('whoami');

Check failure on line 21 in src/Command/PostHogTestCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

ForbiddenCode

src/Command/PostHogTestCommand.php:21:19: ForbiddenCode: Unsafe shell_exec (see https://psalm.dev/002)
$posthog->identify(['distinctId' => $whoami]);
if (!\is_string($whoami)) {
return self::FAILURE;
}
$posthog->identify(new IdentifyMessage($whoami));

$output->writeln('Sending test message...');

$captured = $posthog->capture([
'distinctId' => $whoami,
'event' => 'test-event'
])
return $posthog->capture(new Message(
event: 'test_event',
distinctId: $whoami,
properties: [
'test' => 'test',
],
))
? self::SUCCESS
: self::FAILURE;

return $captured;
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('enabled')->defaultValue(false)->end()
->scalarNode('user_prefix')->defaultValue('user')->end()
->end();

return $builder;
}
}
6 changes: 4 additions & 2 deletions src/DependencyInjection/PostHogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function getXsdValidationBasePath(): string
return __DIR__ . '/../../config/schema/posthog-1.0.xsd';
}

/**
* @param array<array-key, mixed>&array{options: array<array-key, mixed>} $mergedConfig
*/
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
Expand All @@ -35,7 +38,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
}

/**
* @param array<string, mixed> $config
* @param array<string, mixed>&array{options: array<array-key, mixed>} $config
*/
private function registerConfiguration(ContainerBuilder $container, array $config): void
{
Expand All @@ -46,7 +49,6 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
->addMethodCall('setSdkIdentifier', [PostHogBundle::SDK_IDENTIFIER])
->addMethodCall('setSdkVersion', [PostHogBundle::SDK_VERSION])
->addMethodCall('setApiKey', [$options['key']])
->addMethodCall('setOptions', [new Reference()])
;

$container
Expand Down
33 changes: 15 additions & 18 deletions src/EventListener/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PostHog\PostHogBundle\EventListener;

use PostHog\PostHog;
use PostHog\PostHogBundle\Model\IdentifyMessage;
use PostHog\PostHogBundle\PostHogInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -17,15 +17,10 @@

final class LoginListener
{
private PostHogInterface $postHog;
private ?TokenStorageInterface $tokenStorage;

public function __construct(
PostHogInterface $postHog,
?TokenStorageInterface $tokenStorage
private PostHogInterface $postHog,
private ?TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
$this->postHog = $postHog;
}

/**
Expand Down Expand Up @@ -70,19 +65,21 @@ private function updateUserContext(TokenInterface $token): void
return;
}

$message = [
'distinctId' => $this->getUserIdentifier($token->getUser()),
];
$identifier = $this->getUserIdentifier($token->getUser());

if (null === $identifier) {
return;
}

$message = new IdentifyMessage($identifier);

$impersonatorUser = $this->getImpersonatorUser($token);

if (null !== $impersonatorUser) {
$message['$set'] = [
'impersonator_username' => $impersonatorUser,
];
$message->set['impersonator_username'] = $impersonatorUser;
}

PostHog::identify($message);
$this->postHog->identify($message);
}

private function isTokenAuthenticated(TokenInterface $token): bool
Expand All @@ -94,15 +91,15 @@ private function isTokenAuthenticated(TokenInterface $token): bool
return null !== $token->getUser();
}

private function getUserIdentifier($user): ?string
private function getUserIdentifier(mixed $user): ?string
{
if ($user instanceof UserInterface) {
if (method_exists($user, 'getUserIdentifier')) {
return $user->getUserIdentifier();
}

if (method_exists($user, 'getUsername')) {
return $user->getUsername();
return (string) $user->getUsername();
}
}

Expand Down Expand Up @@ -132,7 +129,7 @@ protected function isMainRequest(KernelEvent $event): bool
return $event->isMainRequest();
}
if (method_exists($event, 'isMasterRequest')) {
return $event->isMasterRequest();
return (bool) $event->isMasterRequest();
}

return true;
Expand Down
2 changes: 0 additions & 2 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PostHog\PostHogBundle\EventListener;

use PostHog\PostHogBundle\Adapter\PostHogAdapter;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -13,7 +12,6 @@ final class RequestListener
{
public function __construct()
{

}

public function handleKernelRequestEvent(RequestEvent $event): void
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/NotInitializedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace PostHog\PostHogBundle\Exception;

use Throwable;

class NotInitializedException extends \Exception
{
public function __construct(int $code = 0, ?Throwable $previous = null)
public function __construct(int $code = 0, ?\Throwable $previous = null)
{
parent::__construct('Client is not initialized, please initialze first.', $code, $previous);
}
Expand Down
Loading

0 comments on commit 690299a

Please sign in to comment.