Skip to content

Commit fa5f520

Browse files
committed
fixed static analysis Neo4j namespace
1 parent c74fbc2 commit fa5f520

23 files changed

+199
-182
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"nyholm/psr7": "^1.3",
5050
"nyholm/psr7-server": "^1.0",
5151
"kriswallsmith/buzz": "^1.2",
52-
"vimeo/psalm": "4.26.0",
52+
"vimeo/psalm": "^4.27",
5353
"friendsofphp/php-cs-fixer": "3.8.0",
5454
"psalm/plugin-phpunit": "^0.15.1",
5555
"monolog/monolog": "^2.2",

src/Basic/Driver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function verifyConnectivity(): bool
5656

5757
/**
5858
* @param string|UriInterface $uri
59-
* @pure
6059
*/
6160
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null): self
6261
{

src/Bolt/BoltDriver.php

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
namespace Laudis\Neo4j\Bolt;
1515

1616
use Exception;
17-
use function extension_loaded;
1817
use function is_string;
1918
use Laudis\Neo4j\Authentication\Authenticate;
20-
use Laudis\Neo4j\Common\SingleThreadedSemaphore;
21-
use Laudis\Neo4j\Common\SysVSemaphore;
19+
use Laudis\Neo4j\BoltFactory;
20+
use Laudis\Neo4j\Common\SemaphoreFactory;
2221
use Laudis\Neo4j\Common\Uri;
2322
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2423
use Laudis\Neo4j\Contracts\DriverInterface;
@@ -81,46 +80,11 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
8180

8281
$configuration ??= DriverConfiguration::default();
8382
$authenticate ??= Authenticate::fromUrl($uri);
84-
$sslFactory = new SslConfigurationFactory();
85-
86-
// Because interprocess switching of connections between PHP sessions is impossible,
87-
// we have to build a key to limit the amount of open connections, potentially between ALL sessions.
88-
// because of this we have to settle on a configuration basis to limit the connection pool,
89-
// not on an object basis.
90-
// The combination is between the server and the user agent as it most closely resembles an "application"
91-
// connecting to a server. The application thus supports multiple authentication methods, but they have
92-
// to be shared between the same connection pool.
93-
$key = $uri->getHost().':'.($uri->getPort() ?? '').':'.$configuration->getUserAgent();
94-
95-
if (extension_loaded('ext-sysvsem')) {
96-
$semaphore = SysVSemaphore::create($key, $configuration->getMaxPoolSize());
97-
} else {
98-
$semaphore = SingleThreadedSemaphore::create($key, $configuration->getMaxPoolSize());
99-
}
100-
101-
$pool = new ConnectionPool(
102-
$semaphore,
103-
SystemWideConnectionFactory::getInstance(),
104-
new ConnectionRequestData(
105-
$uri,
106-
$authenticate,
107-
$configuration->getUserAgent(),
108-
$sslFactory->create($uri, $configuration->getSslConfiguration())
109-
)
110-
);
111-
112-
if ($formatter !== null) {
113-
return new self(
114-
$uri,
115-
$pool,
116-
$formatter
117-
);
118-
}
11983

12084
return new self(
12185
$uri,
122-
$pool,
123-
OGMFormatter::create(),
86+
ConnectionPool::create($uri, $authenticate, $configuration),
87+
$formatter ?? OGMFormatter::create(),
12488
);
12589
}
12690

src/Bolt/ConnectionPool.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,54 @@
1414
namespace Laudis\Neo4j\Bolt;
1515

1616
use Generator;
17-
use Laudis\Neo4j\Contracts\ConnectionFactoryInterface;
17+
use Laudis\Neo4j\BoltFactory;
18+
use Laudis\Neo4j\Common\SemaphoreFactory;
19+
use Laudis\Neo4j\Contracts\AuthenticateInterface;
1820
use Laudis\Neo4j\Contracts\ConnectionInterface;
1921
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2022
use Laudis\Neo4j\Contracts\SemaphoreInterface;
2123
use Laudis\Neo4j\Databags\ConnectionRequestData;
24+
use Laudis\Neo4j\Databags\DriverConfiguration;
2225
use Laudis\Neo4j\Databags\SessionConfiguration;
23-
use Bolt\protocol\V3;
24-
use Laudis\Neo4j\Bolt\Connection;
2526
use function method_exists;
2627
use function microtime;
28+
use Psr\Http\Message\UriInterface;
2729
use function shuffle;
2830

2931
/**
30-
* @implements ConnectionPoolInterface<array{0: V3, 1: Connection}>
32+
* @implements ConnectionPoolInterface<BoltConnection>
3133
*/
3234
final class ConnectionPool implements ConnectionPoolInterface
3335
{
3436
private SemaphoreInterface $semaphore;
35-
/** @var list<ConnectionInterface<array{0: V3, 1: Connection}>> */
37+
/** @var list<BoltConnection> */
3638
private array $activeConnections = [];
37-
/** @var ConnectionFactoryInterface<array{0: V3, 1: Connection}> */
38-
private ConnectionFactoryInterface $factory;
39+
private BoltFactory $factory;
3940
private ConnectionRequestData $data;
4041

41-
/**
42-
* @param ConnectionFactoryInterface<array{0: V3, 1: Connection}> $factory
43-
*/
44-
public function __construct(SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory, ConnectionRequestData $data)
42+
public function __construct(SemaphoreInterface $semaphore, BoltFactory $factory, ConnectionRequestData $data)
4543
{
4644
$this->semaphore = $semaphore;
4745
$this->factory = $factory;
4846
$this->data = $data;
4947
}
5048

49+
public static function create(UriInterface $uri, AuthenticateInterface $auth, DriverConfiguration $conf): self
50+
{
51+
$semaphore = SemaphoreFactory::getInstance()->create($uri, $conf);
52+
53+
return new self(
54+
$semaphore,
55+
BoltFactory::create(),
56+
new ConnectionRequestData(
57+
$uri,
58+
$auth,
59+
$conf->getUserAgent(),
60+
$conf->getSslConfiguration()
61+
)
62+
);
63+
}
64+
5165
public function acquire(SessionConfiguration $config): Generator
5266
{
5367
$generator = $this->semaphore->wait();
@@ -96,7 +110,7 @@ public function release(ConnectionInterface $connection): void
96110
}
97111

98112
/**
99-
* @return ConnectionInterface<array{0: V3, 1: Connection}>|null
113+
* @return BoltConnection|null
100114
*/
101115
private function returnAnyAvailableConnection(SessionConfiguration $config): ?ConnectionInterface
102116
{

src/Bolt/Session.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use Bolt\error\MessageException;
1717
use Exception;
18+
use Laudis\Neo4j\Common\GeneratorHelper;
1819
use Laudis\Neo4j\Common\TransactionHelper;
19-
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2020
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2121
use Laudis\Neo4j\Contracts\FormatterInterface;
2222
use Laudis\Neo4j\Contracts\SessionInterface;
@@ -31,7 +31,6 @@
3131
use Laudis\Neo4j\Exception\Neo4jException;
3232
use Laudis\Neo4j\Neo4j\Neo4jConnectionPool;
3333
use Laudis\Neo4j\Types\CypherList;
34-
use Psr\Http\Message\UriInterface;
3534

3635
/**
3736
* A session using bolt connections.
@@ -156,13 +155,15 @@ private function beginInstantTransaction(
156155
private function acquireConnection(TransactionConfiguration $config, SessionConfiguration $sessionConfig): BoltConnection
157156
{
158157
$connection = $this->pool->acquire($sessionConfig);
158+
/** @var BoltConnection $connection */
159+
$connection = GeneratorHelper::getReturnFromGenerator($connection);
159160

160161
// We try and let the server do the timeout management.
161-
// Since the client should not run indefinitely, we just multiply the client side by two, just in case
162+
// Since the client should not run indefinitely, we just add the client side by two, just in case
162163
$timeout = $config->getTimeout();
163164
if ($timeout) {
164165
$timeout = ($timeout < 30) ? 30 : $timeout;
165-
$connection->setTimeout($timeout * 2);
166+
$connection->setTimeout($timeout + 2);
166167
}
167168

168169
return $connection;

src/Bolt/SystemWideConnectionFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
namespace Laudis\Neo4j\Bolt;
1515

1616
use function extension_loaded;
17+
use Laudis\Neo4j\Contracts\BasicConnectionFactoryInterface;
1718

1819
/**
1920
* Singleton connection factory based on the installed extensions.
2021
*/
21-
class SystemWideConnectionFactory
22+
class SystemWideConnectionFactory implements BasicConnectionFactoryInterface
2223
{
2324
/**
2425
* @var SocketConnectionFactory|StreamConnectionFactory

src/Bolt/UriConfiguration.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ final class UriConfiguration
2323
private ?float $timeout;
2424

2525
/**
26-
* @param string $host
27-
* @param int|null $port
2826
* @param ''|'s'|'ssc' $sslLevel
29-
* @param array $sslConfiguration
30-
* @param float|null $timeout
3127
*/
3228
public function __construct(string $host, ?int $port, string $sslLevel, array $sslConfiguration, ?float $timeout)
3329
{

src/BoltFactory.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
namespace Laudis\Neo4j;
1515

1616
use Bolt\Bolt;
17-
use Bolt\protocol\V3;
17+
use Laudis\Neo4j\Bolt\SystemWideConnectionFactory;
1818
use function explode;
1919
use Laudis\Neo4j\Bolt\BoltConnection;
20-
use Laudis\Neo4j\Bolt\Connection;
2120
use Laudis\Neo4j\Bolt\ProtocolFactory;
2221
use Laudis\Neo4j\Bolt\SslConfigurationFactory;
2322
use Laudis\Neo4j\Bolt\UriConfiguration;
2423
use Laudis\Neo4j\Common\ConnectionConfiguration;
2524
use Laudis\Neo4j\Contracts\BasicConnectionFactoryInterface;
26-
use Laudis\Neo4j\Contracts\ConnectionFactoryInterface;
2725
use Laudis\Neo4j\Contracts\ConnectionInterface;
2826
use Laudis\Neo4j\Databags\ConnectionRequestData;
2927
use Laudis\Neo4j\Databags\DatabaseInfo;
@@ -33,10 +31,8 @@
3331

3432
/**
3533
* Small wrapper around the bolt library to easily guarantee only bolt version 3 and up will be created and authenticated.
36-
*
37-
* @implements ConnectionFactoryInterface<array{0: V3, 1: Connection}>
3834
*/
39-
final class BoltFactory implements ConnectionFactoryInterface
35+
final class BoltFactory
4036
{
4137
private BasicConnectionFactoryInterface $connectionFactory;
4238
private ProtocolFactory $protocolFactory;
@@ -52,7 +48,12 @@ public function __construct(BasicConnectionFactoryInterface $connectionFactory,
5248
$this->sslConfigurationFactory = $sslConfigurationFactory;
5349
}
5450

55-
public function createConnection(ConnectionRequestData $data, SessionConfiguration $sessionConfig): ConnectionInterface
51+
public static function create(): self
52+
{
53+
return new self(SystemWideConnectionFactory::getInstance(), new ProtocolFactory(), new SslConfigurationFactory());
54+
}
55+
56+
public function createConnection(ConnectionRequestData $data, SessionConfiguration $sessionConfig): BoltConnection
5657
{
5758
[$sslLevel, $sslConfig] = $this->sslConfigurationFactory->create($data->getUri(), $data->getSslConfig());
5859

@@ -89,7 +90,7 @@ public function canReuseConnection(ConnectionInterface $connection, ConnectionRe
8990
$connection->getUserAgent() === $data->getUserAgent();
9091
}
9192

92-
public function reuseConnection(ConnectionInterface $connection, SessionConfiguration $sessionConfig): ConnectionInterface
93+
public function reuseConnection(BoltConnection $connection, SessionConfiguration $sessionConfig): BoltConnection
9394
{
9495
[$protocol, $connectionImpl] = $connection->getImplementation();
9596

src/Client.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ private function decideAlias(?string $alias): string
143143
}
144144

145145
/**
146-
* @psalm-mutation-free
147-
*
148146
* @param array<string, DriverSetup> $driverSetups
149147
* @param FormatterInterface<ResultFormat> $formatter
150148
*

src/Common/Cache.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Neo4j PHP Client and Driver package.
57
*

0 commit comments

Comments
 (0)