Skip to content

Commit 3131e01

Browse files
committed
corrected typing via psalm
1 parent 7a72eb2 commit 3131e01

File tree

9 files changed

+89
-30
lines changed

9 files changed

+89
-30
lines changed

DependencyInjection/Configuration.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public function getConfigTreeBuilder(): TreeBuilder
3636
{
3737
$treeBuilder = new TreeBuilder('neo4j');
3838

39-
/* @phpstan-ignore-next-line */
39+
/*
40+
* @psalm-suppress all
41+
* @phpstan-ignore-next-line
42+
*/
4043
$treeBuilder->getRootNode()->children()
4144
->arrayNode('profiling')
4245
->addDefaultsIfNotSet()

EventHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Neo4j\Neo4jBundle;
44

5+
use Laudis\Neo4j\Databags\Statement;
6+
use Laudis\Neo4j\Databags\SummarizedResult;
57
use Laudis\Neo4j\Exception\Neo4jException;
68
use Laudis\Neo4j\Types\CypherList;
9+
use Laudis\Neo4j\Types\CypherMap;
710
use Neo4j\Neo4jBundle\Events\FailureEvent;
811
use Neo4j\Neo4jBundle\Events\PostRunEvent;
912
use Neo4j\Neo4jBundle\Events\PreRunEvent;
@@ -18,7 +21,13 @@ public function __construct(?EventDispatcherInterface $dispatcher)
1821
$this->dispatcher = $dispatcher;
1922
}
2023

21-
public function handle(callable $runHandler, iterable $statements): ?CypherList
24+
/**
25+
* @param iterable<Statement> $statements
26+
* @param callable():CypherList<SummarizedResult<CypherList<CypherMap<mixed>>>> $runHandler
27+
*
28+
* @return CypherList<SummarizedResult<CypherList<CypherMap<mixed>>>>
29+
*/
30+
public function handle(callable $runHandler, iterable $statements): CypherList
2231
{
2332
if (null === $this->dispatcher) {
2433
return $runHandler();
@@ -38,6 +47,6 @@ public function handle(callable $runHandler, iterable $statements): ?CypherList
3847
}
3948
}
4049

41-
return null;
50+
return new CypherList();
4251
}
4352
}

EventSubscriber/LoggerSubscriber.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(QueryLogger $queryLogger)
2222
/**
2323
* {@inheritdoc}
2424
*/
25-
public static function getSubscribedEvents()
25+
public static function getSubscribedEvents(): array
2626
{
2727
return [
2828
PreRunEvent::EVENT_ID => 'onPreRun',
@@ -31,21 +31,21 @@ public static function getSubscribedEvents()
3131
];
3232
}
3333

34-
public function onPreRun(PreRunEvent $event)
34+
public function onPreRun(PreRunEvent $event): void
3535
{
3636
foreach ($event->getStatements() as $statement) {
3737
$this->queryLogger->record($statement);
3838
}
3939
}
4040

41-
public function onPostRun(PostRunEvent $event)
41+
public function onPostRun(PostRunEvent $event): void
4242
{
4343
foreach ($event->getResults() as $result) {
4444
$this->queryLogger->finish($result);
4545
}
4646
}
4747

48-
public function onFailure(FailureEvent $event)
48+
public function onFailure(FailureEvent $event): void
4949
{
5050
$this->queryLogger->logException($event->getException());
5151
}

Events/PostRunEvent.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66
use Laudis\Neo4j\Types\CypherList;
77
use Symfony\Contracts\EventDispatcher\Event;
88

9+
/**
10+
* @psalm-import-type OGMResults from \Laudis\Neo4j\Formatter\OGMFormatter
11+
*/
912
class PostRunEvent extends Event
1013
{
1114
public const EVENT_ID = 'neo4j.post_run';
1215

13-
/** @var CypherList<SummarizedResult> */
16+
/** @var CypherList<SummarizedResult<OGMResults>> */
1417
protected CypherList $results;
1518

1619
/**
17-
* @param CypherList<SummarizedResult> $results
20+
* @param CypherList<SummarizedResult<OGMResults>> $results
1821
*/
1922
public function __construct(CypherList $results)
2023
{
2124
$this->results = $results;
2225
}
2326

2427
/**
25-
* @return CypherList<SummarizedResult>
28+
* @return CypherList<SummarizedResult<OGMResults>>
2629
*/
2730
public function getResults(): CypherList
2831
{

Factory/ClientFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Laudis\Neo4j\ClientBuilder;
88
use Laudis\Neo4j\Contracts\ClientInterface;
9+
use Laudis\Neo4j\Databags\SummarizedResult;
10+
use Laudis\Neo4j\Formatter\OGMFormatter;
11+
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
12+
use Laudis\Neo4j\Types\CypherList;
13+
use Laudis\Neo4j\Types\CypherMap;
914
use Neo4j\Neo4jBundle\EventHandler;
1015
use Neo4j\Neo4jBundle\SymfonyClient;
1116
use function sprintf;
@@ -21,11 +26,12 @@ final class ClientFactory
2126
*/
2227
public function create(array $names, array $configs, ?EventDispatcherInterface $dispatcher): ClientInterface
2328
{
24-
$builder = ClientBuilder::create();
29+
$builder = ClientBuilder::create()->withFormatter(new SummarizedResultFormatter(OGMFormatter::create()));
2530
foreach ($names as $name) {
2631
$builder = $builder->withDriver($name, $this->getUrl($configs[$name]));
2732
}
2833

34+
/** @var ClientInterface<SummarizedResult<CypherList<CypherMap<mixed>>>> */
2935
$client = $builder->withDefaultDriver(reset($names))->build();
3036

3137
return new SymfonyClient($client, new EventHandler(null));

Neo4jBundle.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010
*/
1111
class Neo4jBundle extends Bundle
1212
{
13+
/** @var callable(string):void|null */
1314
private $autoloader;
1415

15-
public function getContainerExtension()
16+
public function getContainerExtension(): Neo4jExtension
1617
{
1718
return new Neo4jExtension();
1819
}
1920

20-
public function boot()
21+
public function boot(): void
2122
{
2223
// Register an autoloader for proxies to avoid issues when unserializing them when the OGM is used.
2324
if ($this->container->has('neo4j.entity_manager')) {
2425
// See https://github.com/symfony/symfony/pull/3419 for usage of references
2526
$container = &$this->container;
26-
$this->autoloader = function ($class) use (&$container) {
27-
if (0 === strpos($class, 'neo4j_ogm_proxy')) {
27+
$this->autoloader = static function (string $class) use (&$container): void {
28+
if (str_starts_with($class, 'neo4j_ogm_proxy')) {
2829
$cacheDir = $container->getParameter('kernel.cache_dir').DIRECTORY_SEPARATOR.'neo4j';
2930
$file = $cacheDir.DIRECTORY_SEPARATOR.$class.'.php';
3031
if (file_exists($file)) {
@@ -36,7 +37,7 @@ public function boot()
3637
}
3738
}
3839

39-
public function shutdown()
40+
public function shutdown(): void
4041
{
4142
if (null === $this->autoloader) {
4243
return;

SymfonyClient.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,41 @@
1212
use Laudis\Neo4j\Databags\TransactionConfiguration;
1313
use Laudis\Neo4j\Enum\AccessMode;
1414
use Laudis\Neo4j\Types\CypherList;
15+
use Laudis\Neo4j\Types\CypherMap;
1516

17+
/**
18+
* @implements ClientInterface<SummarizedResult<CypherList<CypherMap<mixed>>>|null>
19+
*/
1620
class SymfonyClient implements ClientInterface
1721
{
22+
/** @var ClientInterface<SummarizedResult<CypherList<CypherMap<mixed>>>> */
1823
private ClientInterface $client;
1924
private EventHandler $handler;
2025

26+
/**
27+
* @param ClientInterface<SummarizedResult<CypherList<CypherMap<mixed>>>> $client
28+
*/
2129
public function __construct(ClientInterface $client, EventHandler $handler)
2230
{
2331
$this->client = $client;
2432
$this->handler = $handler;
2533
}
2634

27-
public function run(string $query, iterable $parameters = [], ?string $alias = null): SummarizedResult
35+
public function run(string $statement, iterable $parameters = [], ?string $alias = null): ?SummarizedResult
2836
{
29-
return $this->runStatement(new Statement($query, $parameters), $alias);
37+
return $this->runStatement(new Statement($statement, $parameters), $alias);
3038
}
3139

32-
public function runStatement(Statement $statement, ?string $alias = null): SummarizedResult
40+
public function runStatement(Statement $statement, ?string $alias = null): ?SummarizedResult
3341
{
34-
return $this->runStatements([$statement], $alias)->first();
42+
$tbr = $this->runStatements([$statement], $alias);
43+
44+
return $tbr->isEmpty() ? null : $tbr->first();
3545
}
3646

47+
/**
48+
* @psalm-suppress InvalidReturnStatement
49+
*/
3750
public function runStatements(iterable $statements, ?string $alias = null): CypherList
3851
{
3952
return $this->handler->handle(fn () => $this->client->runStatements($statements, $alias), $statements);
@@ -42,11 +55,19 @@ public function runStatements(iterable $statements, ?string $alias = null): Cyph
4255
public function beginTransaction(?iterable $statements = null, ?string $alias = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
4356
{
4457
$tsx = new SymfonyTransaction($this->client->beginTransaction(null, $alias, $config), $this->handler);
45-
$this->handler->handle(fn () => $tsx->runStatements($statements ?? []), $statements ?? []);
58+
/**
59+
* @var callable():CypherList<SummarizedResult<CypherList<CypherMap<mixed>>>> $runHandler
60+
*/
61+
$runHandler = fn (): CypherList => $tsx->runStatements($statements ?? []);
62+
$this->handler->handle($runHandler, $statements ?? []);
4663

4764
return $tsx;
4865
}
4966

67+
/**
68+
* @psalm-mutation-free
69+
* @psalm-suppress InvalidReturnStatement
70+
*/
5071
public function getDriver(?string $alias): DriverInterface
5172
{
5273
return $this->client->getDriver($alias);
@@ -55,9 +76,9 @@ public function getDriver(?string $alias): DriverInterface
5576
public function writeTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
5677
{
5778
$sessionConfig = SessionConfiguration::default()->withAccessMode(AccessMode::READ());
58-
$session = $this->getDriver($alias)->createSession($sessionConfig);
79+
$session = $this->client->getDriver($alias)->createSession($sessionConfig);
5980

60-
TransactionHelper::retry(
81+
return TransactionHelper::retry(
6182
fn () => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler),
6283
$tsxHandler,
6384
$config ?? TransactionConfiguration::default()
@@ -67,9 +88,9 @@ public function writeTransaction(callable $tsxHandler, ?string $alias = null, ?T
6788
public function readTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
6889
{
6990
$sessionConfig = SessionConfiguration::default()->withAccessMode(AccessMode::WRITE());
70-
$session = $this->getDriver($alias)->createSession($sessionConfig);
91+
$session = $this->client->getDriver($alias)->createSession($sessionConfig);
7192

72-
TransactionHelper::retry(
93+
return TransactionHelper::retry(
7394
fn () => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler),
7495
$tsxHandler,
7596
$config ?? TransactionConfiguration::default()
@@ -78,6 +99,6 @@ public function readTransaction(callable $tsxHandler, ?string $alias = null, ?Tr
7899

79100
public function transaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
80101
{
81-
$this->writeTransaction($tsxHandler, $alias, $config);
102+
return $this->writeTransaction($tsxHandler, $alias, $config);
82103
}
83104
}

SymfonyTransaction.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,49 @@
66
use Laudis\Neo4j\Databags\Statement;
77
use Laudis\Neo4j\Databags\SummarizedResult;
88
use Laudis\Neo4j\Types\CypherList;
9+
use Laudis\Neo4j\Types\CypherMap;
910

11+
/**
12+
* @implements UnmanagedTransactionInterface<SummarizedResult<CypherList<CypherMap<mixed>>>|null>
13+
*/
1014
class SymfonyTransaction implements UnmanagedTransactionInterface
1115
{
16+
/** @var UnmanagedTransactionInterface<SummarizedResult<CypherList<CypherMap<mixed>>>> */
1217
private UnmanagedTransactionInterface $tsx;
1318
private EventHandler $handler;
1419

20+
/**
21+
* @param UnmanagedTransactionInterface<SummarizedResult<CypherList<CypherMap<mixed>>>> $tsx
22+
*/
1523
public function __construct(UnmanagedTransactionInterface $tsx, EventHandler $handler)
1624
{
1725
$this->tsx = $tsx;
1826
$this->handler = $handler;
1927
}
2028

21-
public function run(string $statement, iterable $parameters = []): SummarizedResult
29+
public function run(string $statement, iterable $parameters = []): ?SummarizedResult
2230
{
2331
return $this->runStatement(new Statement($statement, $parameters));
2432
}
2533

26-
public function runStatement(Statement $statement): SummarizedResult
34+
public function runStatement(Statement $statement): ?SummarizedResult
2735
{
28-
return $this->runStatements([$statement])->first();
36+
$result = $this->runStatements([$statement]);
37+
38+
return $result->isEmpty() ? null : $result->first();
2939
}
3040

41+
/**
42+
* @psalm-suppress InvalidReturnStatement
43+
*/
3144
public function runStatements(iterable $statements): CypherList
3245
{
3346
return $this->handler->handle(fn () => $this->tsx->runStatements($statements), $statements);
3447
}
3548

49+
/**
50+
* @psalm-suppress InvalidReturnStatement
51+
*/
3652
public function commit(iterable $statements = []): CypherList
3753
{
3854
return $this->handler->handle(fn () => $this->tsx->commit($statements), $statements);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"require": {
1818
"php": ">=7.4",
19-
"laudis/neo4j-php-client": "2.1.2",
19+
"laudis/neo4j-php-client": "^2.2",
2020
"symfony/dependency-injection": "^5.0",
2121
"symfony/framework-bundle": "^5.0",
2222
"symfony/http-kernel": "^5.0",

0 commit comments

Comments
 (0)