Skip to content

Commit 6b42484

Browse files
authored
Merge pull request geocoder-php#392 from Chris53897/feature/new
feat: add strong PHP Types, cleanup
2 parents bb2322b + 6bb7b6a commit 6b42484

File tree

15 files changed

+80
-282
lines changed

15 files changed

+80
-282
lines changed

src/DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,15 @@ private function findReferences(array $options): array
209209
foreach ($options as $key => $value) {
210210
if (is_array($value)) {
211211
$options[$key] = $this->findReferences($value);
212-
} elseif ('_service' === substr((string) $key, -8) || 0 === strpos((string) $value, '@') || 'service' === $key) {
212+
} elseif (str_ends_with((string) $key, '_service') || str_starts_with((string) $value, '@') || 'service' === $key) {
213213
$options[$key] = new Reference(ltrim($value, '@'));
214214
}
215215
}
216216

217217
return $options;
218218
}
219219

220-
/**
221-
* @param mixed $factoryClass
222-
*/
223-
private function implementsProviderFactory($factoryClass): bool
220+
private function implementsProviderFactory(mixed $factoryClass): bool
224221
{
225222
if (false === $interfaces = class_implements($factoryClass)) {
226223
return false;

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ public function getConfigTreeBuilder(): TreeBuilder
8383
return $treeBuilder;
8484
}
8585

86-
/**
87-
* @return ArrayNodeDefinition
88-
*/
89-
private function getProvidersNode()
86+
private function getProvidersNode(): ArrayNodeDefinition
9087
{
9188
$treeBuilder = new TreeBuilder('providers');
9289
$rootNode = $treeBuilder->getRootNode();
@@ -121,16 +118,13 @@ private function getProvidersNode()
121118

122119
/**
123120
* Create plugin node of a client.
124-
*
125-
* @return ArrayNodeDefinition The plugin node
126121
*/
127-
private function createClientPluginNode()
122+
private function createClientPluginNode(): ArrayNodeDefinition
128123
{
129124
$treeBuilder = new TreeBuilder('plugins');
130125
$rootNode = $treeBuilder->getRootNode();
131126
assert($rootNode instanceof ArrayNodeDefinition);
132127

133-
/** @var ArrayNodeDefinition $pluginList */
134128
$pluginList = $rootNode
135129
->info('A list of plugin service ids. The order is important.')
136130
->arrayPrototype()

src/ProviderFactory/GeoIP2Factory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use GeoIp2\Database\Reader;
1919
use GeoIp2\ProviderInterface;
2020
use GeoIp2\WebService\Client;
21+
use MaxMind\Db\Reader\InvalidDatabaseException;
2122
use Symfony\Component\OptionsResolver\OptionsResolver;
2223

2324
final class GeoIP2Factory extends AbstractFactory
@@ -28,6 +29,8 @@ final class GeoIP2Factory extends AbstractFactory
2829

2930
/**
3031
* @param array{provider: string, provider_service: ?ProviderInterface, model: string, user_id: string|int|null, license_key: string|null, locales: list<string>, webservice_options: array<string, mixed>, database_filename: ?string} $config
32+
*
33+
* @throws InvalidDatabaseException
3134
*/
3235
protected function getProvider(array $config): Provider
3336
{

src/ProviderFactory/PluginProviderFactory.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Geocoder\Plugin\Plugin;
1616
use Geocoder\Plugin\PluginProvider;
17+
use Geocoder\Provider\Provider;
1718

1819
/**
1920
* This factory creates a PluginProvider.
@@ -23,19 +24,17 @@
2324
final class PluginProviderFactory
2425
{
2526
/**
26-
* @param Plugin[] $plugins
27-
* @param ProviderFactoryInterface|callable $factory
28-
* @param array<mixed> $config config to the client factory
29-
* @param array{max_restarts?: int<0, max>} $pluginProviderOptions config forwarded to the PluginProvider
27+
* @param Plugin[] $plugins
28+
* @param callable(array<mixed>):Provider|ProviderFactoryInterface $factory
29+
* @param array<mixed> $config config to the client factory
30+
* @param array{max_restarts?: int<0, max>} $pluginProviderOptions config forwarded to the PluginProvider
3031
*/
31-
public static function createPluginProvider(array $plugins, $factory, array $config, array $pluginProviderOptions = []): PluginProvider
32+
public static function createPluginProvider(array $plugins, callable|ProviderFactoryInterface $factory, array $config, array $pluginProviderOptions = []): PluginProvider
3233
{
3334
if ($factory instanceof ProviderFactoryInterface) {
3435
$client = $factory->createProvider($config);
35-
} elseif (is_callable($factory)) {
36-
$client = $factory($config);
3736
} else {
38-
throw new \RuntimeException(sprintf('Second argument to PluginProviderFactory::createPluginProvider must be a "%s" or a callable.', ProviderFactoryInterface::class));
37+
$client = $factory($config);
3938
}
4039

4140
return new PluginProvider($client, $plugins, $pluginProviderOptions);

src/Validator/Constraint/Address.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,9 @@ class Address extends Constraint
2929
self::INVALID_ADDRESS_ERROR => 'INVALID_ADDRESS_ERROR',
3030
];
3131

32-
/**
33-
* @var string
34-
*/
35-
public $service = AddressValidator::class;
36-
37-
/**
38-
* @var string
39-
*/
40-
public $message = 'Address {{ address }} is not valid.';
41-
42-
/**
43-
* @param string[]|null $options
44-
*/
45-
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, $payload = null)
32+
public function __construct(public string $service = AddressValidator::class, public string $message = 'Address {{ address }} is not valid.', ?array $groups = null, $payload = null)
4633
{
47-
parent::__construct($options, $groups, $payload);
48-
49-
$this->message = $message ?? $this->message;
34+
parent::__construct(null, $groups, $payload);
5035
}
5136

5237
public function validatedBy(): string

tests/DependencyInjection/Compiler/FactoryValidatorPassTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ protected function tearDown(): void
3333
{
3434
$reflection = new \ReflectionObject($this->compilerPass);
3535
$prop = $reflection->getProperty('factoryServiceIds');
36-
if (PHP_VERSION_ID < 80100) {
37-
$prop->setAccessible(true);
38-
}
3936
$prop->setValue(null, []);
4037
}
4138

tests/Functional/BundleInitializationTest.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public function testInitBundle(): void
5252
{
5353
self::bootKernel(['config' => static function (TestKernel $kernel) {
5454
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
55-
56-
if ($kernel::VERSION_ID >= 60000) {
57-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
58-
}
55+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
5956
}]);
6057

6158
$container = self::getContainer();
@@ -70,11 +67,7 @@ public function testBundleWithOneProviderConfiguration(): void
7067
{
7168
self::bootKernel(['config' => static function (TestKernel $kernel) {
7269
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
73-
74-
if ($kernel::VERSION_ID >= 60000) {
75-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
76-
}
77-
70+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
7871
$kernel->addTestConfig(__DIR__.'/config/simple.yml');
7972
}]);
8073

@@ -90,11 +83,7 @@ public function testBundleWithCachedProvider(): void
9083
{
9184
self::bootKernel(['config' => static function (TestKernel $kernel) {
9285
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
93-
94-
if ($kernel::VERSION_ID >= 60000) {
95-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
96-
}
97-
86+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
9887
$kernel->addTestConfig(__DIR__.'/config/cache.yml');
9988
}]);
10089

@@ -112,11 +101,7 @@ public function testCacheLifetimeCanBeNull(): void
112101
{
113102
self::bootKernel(['config' => static function (TestKernel $kernel) {
114103
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
115-
116-
if ($kernel::VERSION_ID >= 60000) {
117-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
118-
}
119-
104+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
120105
$kernel->addTestConfig(__DIR__.'/config/cache_without_lifetime.yml');
121106
}]);
122107

@@ -142,11 +127,7 @@ public function testBundleWithPluginsYml(): void
142127
{
143128
self::bootKernel(['config' => static function (TestKernel $kernel) {
144129
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
145-
146-
if ($kernel::VERSION_ID >= 60000) {
147-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
148-
}
149-
130+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
150131
$kernel->addTestConfig(__DIR__.'/config/service_plugin.yml');
151132
}]);
152133

@@ -164,10 +145,7 @@ public function testBundleHasRegisteredDumpers(): void
164145
{
165146
self::bootKernel(['config' => static function (TestKernel $kernel) {
166147
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
167-
168-
if ($kernel::VERSION_ID >= 60000) {
169-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
170-
}
148+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
171149
}]);
172150

173151
$container = self::getContainer();

tests/Functional/GeocodeEntityListenerTest.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ public function testPersistForProperty(): void
106106
{
107107
self::bootKernel(['config' => static function (TestKernel $kernel) {
108108
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
109-
110-
if ($kernel::VERSION_ID >= 60000) {
111-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
112-
}
113-
109+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
114110
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
115111
}]);
116112

@@ -145,11 +141,7 @@ public function testPersistForGetter(): void
145141
{
146142
self::bootKernel(['config' => static function (TestKernel $kernel) {
147143
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
148-
149-
if ($kernel::VERSION_ID >= 60000) {
150-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
151-
}
152-
144+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
153145
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
154146
}]);
155147

@@ -184,11 +176,7 @@ public function testPersistForStringableGetter(): void
184176
{
185177
self::bootKernel(['config' => static function (TestKernel $kernel) {
186178
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
187-
188-
if ($kernel::VERSION_ID >= 60000) {
189-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
190-
}
191-
179+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
192180
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
193181
}]);
194182

@@ -223,11 +211,7 @@ public function testPersistForInvalidGetter(): void
223211
{
224212
self::bootKernel(['config' => static function (TestKernel $kernel) {
225213
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
226-
227-
if ($kernel::VERSION_ID >= 60000) {
228-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
229-
}
230-
214+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
231215
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
232216
}]);
233217

@@ -255,11 +239,7 @@ public function testPersistForEmptyProperty(): void
255239
{
256240
self::bootKernel(['config' => static function (TestKernel $kernel) {
257241
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
258-
259-
if ($kernel::VERSION_ID >= 60000) {
260-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
261-
}
262-
242+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
263243
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
264244
}]);
265245

@@ -287,11 +267,7 @@ public function testDoesNotGeocodeIfAddressNotChanged(): void
287267
{
288268
self::bootKernel(['config' => static function (TestKernel $kernel) {
289269
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
290-
291-
if ($kernel::VERSION_ID >= 60000) {
292-
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
293-
}
294-
270+
$kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml');
295271
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
296272
}]);
297273

tests/Functional/Helper/CacheHelper.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,49 @@
1414

1515
use Psr\SimpleCache\CacheInterface;
1616

17-
if (PHP_VERSION_ID >= 80000) {
18-
/**
19-
* @internal
20-
*
21-
* @author Tobias Nyholm <[email protected]>
22-
*/
23-
class CacheHelper implements CacheInterface
24-
{
25-
use CacheHelperV8;
26-
}
27-
} else {
28-
/**
29-
* @internal
30-
*
31-
* @author Tobias Nyholm <[email protected]>
32-
*/
33-
class CacheHelper implements CacheInterface
34-
{
35-
use CacheHelperV7;
17+
/**
18+
* @internal
19+
*
20+
* @author Tobias Nyholm <[email protected]>
21+
*/
22+
class CacheHelper implements CacheInterface
23+
{
24+
public function get(string $key, mixed $default = null): mixed
25+
{
26+
}
27+
28+
public function set(string $key, mixed $value, int|\DateInterval|null $ttl = null): bool
29+
{
30+
return true;
31+
}
32+
33+
public function delete(string $key): bool
34+
{
35+
return true;
36+
}
37+
38+
public function clear(): bool
39+
{
40+
return true;
41+
}
42+
43+
public function getMultiple(iterable $keys, mixed $default = null): iterable
44+
{
45+
return [];
46+
}
47+
48+
public function setMultiple(iterable $values, int|\DateInterval|null $ttl = null): bool
49+
{
50+
return true;
51+
}
52+
53+
public function deleteMultiple(iterable $keys): bool
54+
{
55+
return true;
56+
}
57+
58+
public function has(string $key): bool
59+
{
60+
return false;
3661
}
3762
}

0 commit comments

Comments
 (0)