Skip to content

Commit b9aadc8

Browse files
committed
finished documenting source folder
1 parent 0826dcf commit b9aadc8

File tree

10 files changed

+141
-68
lines changed

10 files changed

+141
-68
lines changed

src/Bolt/BoltConfiguration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ final class BoltConfiguration implements ConfigInterface
5555
private $autoRouting;
5656

5757
/**
58+
* @psalm-mutation-free
59+
*
5860
* @param callable():string|?string $database
5961
* @param LazySSLContextOptions $sslContextOptions
6062
* @param callable():bool|bool $autoRouting
@@ -71,7 +73,7 @@ public function __construct($database = null, $sslContextOptions = null, $autoRo
7173
*
7274
* @see https://www.php.net/manual/en/context.ssl.php for ssl connections
7375
*
74-
* @psalm-immutable
76+
* @pure
7577
*
7678
* @return static
7779
*/

src/Bolt/BoltDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(
8282
* : self<OGMResults>
8383
* )
8484
*
85-
* @psalm-mutation-free
85+
* @pure
8686
*/
8787
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, ?float $socketTimeout = null, FormatterInterface $formatter = null): self
8888
{

src/Client.php

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Laudis\Neo4j;
1515

16-
use InvalidArgumentException;
1716
use Laudis\Neo4j\Contracts\ClientInterface;
1817
use Laudis\Neo4j\Contracts\DriverInterface;
1918
use Laudis\Neo4j\Contracts\FormatterInterface;
@@ -27,37 +26,32 @@
2726
use Laudis\Neo4j\Enum\AccessMode;
2827
use Laudis\Neo4j\Types\CypherList;
2928
use Laudis\Neo4j\Types\CypherMap;
30-
use function sprintf;
3129

3230
/**
33-
* @template T
31+
* A collection of drivers with methods to run queries though them.
3432
*
35-
* @implements ClientInterface<T>
33+
* @template ResultFormat
34+
*
35+
* @implements ClientInterface<ResultFormat>
3636
*/
3737
final class Client implements ClientInterface
3838
{
3939
private const DEFAULT_DRIVER_CONFIG = 'bolt://localhost:7687';
40-
41-
/** @var CypherMap<DriverSetup> */
42-
private CypherMap $driverSetups;
43-
/** @var array<string, DriverInterface<T>> */
40+
/** @var non-empty-array<string, DriverInterface<ResultFormat>> */
4441
private array $drivers;
45-
/** @var FormatterInterface<T> */
46-
private FormatterInterface $formatter;
47-
private DriverConfiguration $configuration;
42+
/** @psalm-readonly */
4843
private ?string $default;
4944

5045
/**
51-
* @param CypherMap<DriverSetup> $driverConfigurations
52-
* @param FormatterInterface<T> $formatter
46+
* @psalm-mutation-free
47+
*
48+
* @param CypherMap<DriverSetup> $driverSetups
49+
* @param FormatterInterface<ResultFormat> $formatter
5350
*/
54-
public function __construct(CypherMap $driverConfigurations, DriverConfiguration $configuration, FormatterInterface $formatter, ?string $default)
51+
public function __construct(CypherMap $driverSetups, DriverConfiguration $configuration, FormatterInterface $formatter, ?string $default)
5552
{
56-
$this->driverSetups = $driverConfigurations;
57-
$this->drivers = [];
58-
$this->formatter = $formatter;
59-
$this->configuration = $configuration;
6053
$this->default = $default;
54+
$this->drivers = $this->createDrivers($driverSetups, $formatter, $configuration);
6155
}
6256

6357
public function run(string $query, iterable $parameters = [], ?string $alias = null)
@@ -80,31 +74,20 @@ public function beginTransaction(?iterable $statements = null, ?string $alias =
8074
return $this->startSession($alias, SessionConfiguration::default())->beginTransaction($statements, $config);
8175
}
8276

77+
/**
78+
* @psalm-mutation-free
79+
*/
8380
public function getDriver(?string $alias): DriverInterface
8481
{
85-
$this->createDefaultDriverIfNeeded();
86-
8782
$alias = $this->decideAlias($alias);
8883

89-
if (!isset($this->drivers[$alias])) {
90-
if (!$this->driverSetups->hasKey($alias)) {
91-
$key = sprintf('The provided alias: "%s" was not found in the connection pool', $alias);
92-
throw new InvalidArgumentException($key);
93-
}
94-
95-
$setup = $this->driverSetups->get($alias);
96-
$uri = $setup->getUri();
97-
$timeout = $setup->getSocketTimeout();
98-
$driver = DriverFactory::create($uri, $this->configuration, $setup->getAuth(), $timeout, $this->formatter);
99-
100-
$this->drivers[$alias] = $driver;
101-
}
102-
10384
return $this->drivers[$alias];
10485
}
10586

10687
/**
107-
* @return SessionInterface<T>
88+
* @psalm-mutation-free
89+
*
90+
* @return SessionInterface<ResultFormat>
10891
*/
10992
private function startSession(?string $alias = null, SessionConfiguration $configuration = null): SessionInterface
11093
{
@@ -126,14 +109,9 @@ public function transaction(callable $tsxHandler, ?string $alias = null, ?Transa
126109
return $this->writeTransaction($tsxHandler, $alias, $config);
127110
}
128111

129-
private function createDefaultDriverIfNeeded(): void
130-
{
131-
if ($this->driverSetups->isEmpty() && count($this->drivers) === 0) {
132-
$driver = DriverFactory::create(self::DEFAULT_DRIVER_CONFIG, null, null, null, $this->formatter);
133-
$this->drivers['default'] = $driver;
134-
}
135-
}
136-
112+
/**
113+
* @psalm-mutation-free
114+
*/
137115
private function decideAlias(?string $alias): string
138116
{
139117
if ($alias !== null) {
@@ -144,10 +122,33 @@ private function decideAlias(?string $alias): string
144122
return $this->default;
145123
}
146124

147-
if ($this->driverSetups->count() > 0) {
148-
return $this->driverSetups->first()->getKey();
125+
return array_key_first($this->drivers);
126+
}
127+
128+
/**
129+
* @psalm-mutation-free
130+
*
131+
* @param CypherMap<DriverSetup> $driverSetups
132+
* @param FormatterInterface<ResultFormat> $formatter
133+
*
134+
* @return non-empty-array<string, DriverInterface<ResultFormat>>
135+
*/
136+
private function createDrivers(CypherMap $driverSetups, FormatterInterface $formatter, DriverConfiguration $configuration): array
137+
{
138+
if (count($driverSetups) === 0) {
139+
$drivers = ['default' => DriverFactory::create(self::DEFAULT_DRIVER_CONFIG, null, null, null, $formatter)];
140+
} else {
141+
$drivers = [];
142+
foreach ($driverSetups as $alias => $setup) {
143+
$uri = $setup->getUri();
144+
$timeout = $setup->getSocketTimeout();
145+
$auth = $setup->getAuth();
146+
147+
$drivers[$alias] = DriverFactory::create($uri, $configuration, $auth, $timeout, $formatter);
148+
}
149149
}
150150

151-
return 'default';
151+
/** @var non-empty-array<string, DriverInterface<ResultFormat>> */
152+
return $drivers;
152153
}
153154
}

src/ClientBuilder.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
use Laudis\Neo4j\Types\CypherMap;
3333

3434
/**
35+
* Immutable factory for creating a client.
36+
*
3537
* @template T
3638
*
3739
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
@@ -40,13 +42,22 @@ final class ClientBuilder
4042
{
4143
public const SUPPORTED_SCHEMES = ['', 'bolt', 'bolt+s', 'bolt+ssc', 'neo4j', 'neo4j+s', 'neo4j+ssc', 'http', 'https'];
4244

43-
/** @var CypherMap<DriverSetup> */
45+
/**
46+
* @psalm-readonly
47+
*
48+
* @var CypherMap<DriverSetup>
49+
*/
4450
private CypherMap $driverConfigurations;
51+
/** @psalm-readonly */
4552
private DriverConfiguration $configuration;
53+
/** @psalm-readonly */
4654
private ?string $defaultDriver;
55+
/** @psalm-readonly */
4756
private FormatterInterface $formatter;
4857

4958
/**
59+
* @psalm-mutation-free
60+
*
5061
* @param CypherMap<DriverSetup> $driverConfigurations
5162
* @param FormatterInterface<T> $formatter
5263
*/
@@ -59,6 +70,8 @@ public function __construct(DriverConfiguration $configuration, FormatterInterfa
5970
}
6071

6172
/**
73+
* @pure
74+
*
6275
* @return ClientBuilder<CypherList<CypherMap<OGMTypes>>>
6376
*/
6477
public static function create(): ClientBuilder
@@ -67,6 +80,8 @@ public static function create(): ClientBuilder
6780
}
6881

6982
/**
83+
* @psalm-mutation-free
84+
*
7085
* @return self<T>
7186
*/
7287
public function withDriver(string $alias, string $url, ?AuthenticateInterface $authentication = null, ?float $socketTimeout = null): self
@@ -78,6 +93,8 @@ public function withDriver(string $alias, string $url, ?AuthenticateInterface $a
7893
}
7994

8095
/**
96+
* @psalm-mutation-free
97+
*
8198
* @return self<T>
8299
*/
83100
private function withParsedUrl(string $alias, Uri $uri, AuthenticateInterface $authentication, float $socketTimeout): self
@@ -108,6 +125,7 @@ public function addBoltConnection(string $alias, string $url, BoltConfiguration
108125
{
109126
$config ??= BoltConfiguration::create();
110127
$parsedUrl = Uri::create($url);
128+
/** @psalm-suppress ImpureMethodCall */
111129
$options = $config->getSslContextOptions();
112130
$postScheme = '';
113131
if ($options && $options !== []) {
@@ -118,7 +136,6 @@ public function addBoltConnection(string $alias, string $url, BoltConfiguration
118136
}
119137
}
120138

121-
$query = [];
122139
parse_str($parsedUrl->getQuery(), $query);
123140
/** @var array<string, string> */
124141
$query['database'] ??= $config->getDatabase();
@@ -172,6 +189,7 @@ public function addHttpConnection(string $alias, string $url, HttpConfig $config
172189
*
173190
* @deprecated
174191
* @see ClientBuilder::withDefaultDriver()
192+
* @psalm-mutation-free
175193
*/
176194
public function setDefaultConnection(string $alias): self
177195
{
@@ -182,6 +200,7 @@ public function setDefaultConnection(string $alias): self
182200
* Sets the default connection to the given alias.
183201
*
184202
* @return self<T>
203+
* @psalm-mutation-free
185204
*/
186205
public function withDefaultDriver(string $alias): self
187206
{
@@ -194,6 +213,7 @@ public function withDefaultDriver(string $alias): self
194213
* @param FormatterInterface<U> $formatter
195214
*
196215
* @return self<U>
216+
* @psalm-mutation-free
197217
*/
198218
public function withFormatter(FormatterInterface $formatter): self
199219
{
@@ -202,12 +222,17 @@ public function withFormatter(FormatterInterface $formatter): self
202222

203223
/**
204224
* @return ClientInterface<T>
225+
*
226+
* @psalm-mutation-free
205227
*/
206228
public function build(): ClientInterface
207229
{
208230
return new Client($this->driverConfigurations, $this->configuration, $this->formatter, $this->defaultDriver);
209231
}
210232

233+
/**
234+
* @psalm-mutation-free
235+
*/
211236
public function withHttpPsrBindings(HttpPsrBindings $bindings): self
212237
{
213238
$config = $this->configuration->withHttpPsrBindings($bindings);

src/Common/Resolvable.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Resolvable
2626
/** @var callable():Resolved */
2727
private $toResolve;
2828

29-
/** @var array<string, self> */
29+
/** @var array<string, mixed> */
3030
private static array $cache = [];
3131

3232
/**
@@ -46,15 +46,17 @@ public function __construct($toResolve)
4646
*
4747
* @param callable():U $toResolve
4848
*
49-
* @return self<U>
49+
* @return Resolvable<U>
5050
*/
5151
public static function once(string $key, $toResolve): Resolvable
5252
{
53-
$tbr = static function () use ($key, $toResolve): self {
53+
/** @psalm-suppress MissingClosureReturnType */
54+
$tbr = static function () use ($key, $toResolve) {
5455
if (!isset(self::$cache[$key])) {
5556
self::$cache[$key] = call_user_func($toResolve);
5657
}
5758

59+
/** @var U */
5860
return self::$cache[$key];
5961
};
6062

0 commit comments

Comments
 (0)