Skip to content

Commit ba42f80

Browse files
greg0ireNyholm
authored andcommitted
Drop old sf and add type hints (#114)
* Drop old versions of Symfony * Replace exceptions with type hints * Add obvious type hint * Add type hints everywhere we can
1 parent 969080c commit ba42f80

File tree

40 files changed

+186
-305
lines changed

40 files changed

+186
-305
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ matrix:
2020
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"
2121

2222
# Force some major versions of Symfony
23-
- php: 7.3
24-
env: DEPENDENCIES="dunglas/symfony-lock:^2"
2523
- php: 7.3
2624
env: DEPENDENCIES="dunglas/symfony-lock:^3"
2725
- php: 7.3

Loader/ExtensionConfigurationBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setExtension(ExtensionInterface $extension)
2323
return $this;
2424
}
2525

26-
public function getExtension()
26+
public function getExtension(): ExtensionInterface
2727
{
2828
if (!($this->extension instanceof ExtensionInterface)) {
2929
throw new \LogicException('You need to call setExtension() first');
@@ -46,7 +46,7 @@ public function setSources(array $sources)
4646
return $this;
4747
}
4848

49-
public function getSources()
49+
public function getSources(): array
5050
{
5151
if (count($this->sources) === 0) {
5252
throw new \LogicException('You need to call setSources() or addSource() first');
@@ -55,7 +55,7 @@ public function getSources()
5555
return $this->sources;
5656
}
5757

58-
public function getConfiguration()
58+
public function getConfiguration(): array
5959
{
6060
$container = new ContainerBuilder();
6161

@@ -66,15 +66,15 @@ public function getConfiguration()
6666
return $this->getExtensionConfiguration($container);
6767
}
6868

69-
private function loadSources(ContainerBuilder $container, array $sources)
69+
private function loadSources(ContainerBuilder $container, array $sources): void
7070
{
7171
foreach ($sources as $source) {
7272
$loader = $this->loaderFactory->createLoaderForSource($container, $source);
7373
$loader->load($source);
7474
}
7575
}
7676

77-
private function getExtensionConfiguration(ContainerBuilder $container)
77+
private function getExtensionConfiguration(ContainerBuilder $container): array
7878
{
7979
$extensionAlias = $this->getExtension()->getAlias();
8080

Loader/LoaderFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Matthias\SymfonyDependencyInjectionTest\Loader\Exception\UnknownConfigurationSourceException;
66
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\Config\Loader\LoaderInterface;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
910
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
@@ -13,7 +14,7 @@
1314

1415
class LoaderFactory implements LoaderFactoryInterface
1516
{
16-
public function createLoaderForSource(ContainerBuilder $container, $source)
17+
public function createLoaderForSource(ContainerBuilder $container, $source): LoaderInterface
1718
{
1819
if ($source instanceof \Closure) {
1920
return new ClosureLoader($container);
@@ -41,22 +42,22 @@ public function createLoaderForSource(ContainerBuilder $container, $source)
4142
));
4243
}
4344

44-
public function createYamlFileLoader($container)
45+
public function createYamlFileLoader($container): YamlFileLoader
4546
{
4647
return new YamlFileLoader($container, new FileLocator());
4748
}
4849

49-
public function createXmlFileLoader(ContainerBuilder $container)
50+
public function createXmlFileLoader(ContainerBuilder $container): XmlFileLoader
5051
{
5152
return new XmlFileLoader($container, new FileLocator());
5253
}
5354

54-
public function createPhpFileLoader(ContainerBuilder $container)
55+
public function createPhpFileLoader(ContainerBuilder $container): PhpFileLoader
5556
{
5657
return new PhpFileLoader($container, new FileLocator());
5758
}
5859

59-
public function createIniFileLoader(ContainerBuilder $container)
60+
public function createIniFileLoader(ContainerBuilder $container): IniFileLoader
6061
{
6162
return new IniFileLoader($container, new FileLocator());
6263
}

Loader/LoaderFactoryInterface.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,5 @@
77

88
interface LoaderFactoryInterface
99
{
10-
/**
11-
* @param ContainerBuilder $container
12-
* @param $source
13-
*
14-
* @return LoaderInterface
15-
*/
16-
public function createLoaderForSource(ContainerBuilder $container, $source);
10+
public function createLoaderForSource(ContainerBuilder $container, $source): LoaderInterface;
1711
}

PhpUnit/AbstractCompilerPassTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ abstract class AbstractCompilerPassTestCase extends AbstractContainerBuilderTest
1212
*
1313
* $container->addCompilerPass(new MyCompilerPass());
1414
*/
15-
abstract protected function registerCompilerPass(ContainerBuilder $container);
15+
abstract protected function registerCompilerPass(ContainerBuilder $container): void;
1616

1717
/**
1818
* This test will run the compile method.
1919
*
2020
* @test
2121
*/
22-
public function compilation_should_not_fail_with_empty_container()
22+
public function compilation_should_not_fail_with_empty_container(): void
2323
{
2424
try {
2525
$this->compile();

PhpUnit/AbstractContainerBuilderTestCase.php

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ protected function tearDown(): void
2929

3030
/**
3131
* Shortcut for quickly defining services. The returned Definition object can be further modified if necessary.
32-
*
33-
* @param $serviceId
34-
* @param $class
35-
*
36-
* @return Definition
3732
*/
38-
protected function registerService($serviceId, $class)
33+
protected function registerService(string $serviceId, string $class): Definition
3934
{
4035
$definition = new Definition($class);
4136

@@ -46,41 +41,34 @@ protected function registerService($serviceId, $class)
4641

4742
/**
4843
* Set a service definition you manually created.
49-
*
50-
* @param $serviceId
51-
* @param Definition $definition
5244
*/
53-
protected function setDefinition($serviceId, Definition $definition)
45+
protected function setDefinition(string $serviceId, Definition $definition): void
5446
{
5547
$this->container->setDefinition($serviceId, $definition);
5648
}
5749

5850
/**
5951
* Set a parameter.
6052
*
61-
* @param $parameterId
62-
* @param $parameterValue
53+
* @param mixed $parameterValue
6354
*/
64-
protected function setParameter($parameterId, $parameterValue)
55+
protected function setParameter(string $parameterId, $parameterValue): void
6556
{
6657
$this->container->setParameter($parameterId, $parameterValue);
6758
}
6859

6960
/**
7061
* Call this method to compile the ContainerBuilder, to test if any problems would occur at runtime.
7162
*/
72-
protected function compile()
63+
protected function compile(): void
7364
{
7465
$this->container->compile();
7566
}
7667

7768
/**
7869
* Assert that the ContainerBuilder for this test has a service definition with the given id and class.
79-
*
80-
* @param $serviceId
81-
* @param $expectedClass
8270
*/
83-
protected function assertContainerBuilderHasService($serviceId, $expectedClass = null)
71+
protected function assertContainerBuilderHasService(string $serviceId, ?string $expectedClass = null): void
8472
{
8573
$checkExpectedClass = (func_num_args() > 1);
8674

@@ -92,10 +80,8 @@ protected function assertContainerBuilderHasService($serviceId, $expectedClass =
9280

9381
/**
9482
* Assert that the ContainerBuilder for this test does not have a service definition with the given id.
95-
*
96-
* @param $serviceId
9783
*/
98-
protected function assertContainerBuilderNotHasService($serviceId)
84+
protected function assertContainerBuilderNotHasService(string $serviceId): void
9985
{
10086
self::assertThat(
10187
$this->container,
@@ -105,10 +91,8 @@ protected function assertContainerBuilderNotHasService($serviceId)
10591

10692
/**
10793
* Assert that the ContainerBuilder for this test has a synthetic service with the given id.
108-
*
109-
* @param $serviceId
11094
*/
111-
protected function assertContainerBuilderHasSyntheticService($serviceId)
95+
protected function assertContainerBuilderHasSyntheticService(string $serviceId): void
11296
{
11397
self::assertThat(
11498
$this->container,
@@ -118,11 +102,8 @@ protected function assertContainerBuilderHasSyntheticService($serviceId)
118102

119103
/**
120104
* Assert that the ContainerBuilder for this test has an alias and that it is an alias for the given service id.
121-
*
122-
* @param $aliasId
123-
* @param $expectedServiceId
124105
*/
125-
protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = null)
106+
protected function assertContainerBuilderHasAlias(string $aliasId, ?string $expectedServiceId = null): void
126107
{
127108
self::assertThat(
128109
$this->container,
@@ -133,10 +114,9 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId =
133114
/**
134115
* Assert that the ContainerBuilder for this test has a parameter and that its value is the given value.
135116
*
136-
* @param $parameterName
137-
* @param $expectedParameterValue
117+
* @param mixed $expectedParameterValue
138118
*/
139-
protected function assertContainerBuilderHasParameter($parameterName, $expectedParameterValue = null)
119+
protected function assertContainerBuilderHasParameter(string $parameterName, $expectedParameterValue = null): void
140120
{
141121
$checkParameterValue = (func_num_args() > 1);
142122

@@ -150,15 +130,13 @@ protected function assertContainerBuilderHasParameter($parameterName, $expectedP
150130
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument
151131
* at the given index, and its value is the given value.
152132
*
153-
* @param $serviceId
154-
* @param $argumentIndex
155-
* @param $expectedValue
133+
* @param mixed $expectedValue
156134
*/
157135
protected function assertContainerBuilderHasServiceDefinitionWithArgument(
158-
$serviceId,
136+
string $serviceId,
159137
$argumentIndex,
160138
$expectedValue = null
161-
) {
139+
): void {
162140
$definition = $this->container->findDefinition($serviceId);
163141
$checkValue = (func_num_args() > 2);
164142

@@ -172,15 +150,14 @@ protected function assertContainerBuilderHasServiceDefinitionWithArgument(
172150
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument
173151
* at the given index, and its value is a ServiceLocator with a reference-map equal to the given value.
174152
*
175-
* @param string $serviceId
176153
* @param int|string $argumentIndex
177154
* @param array $expectedServiceMap an array of service-id references and their key in the map
178155
*/
179156
protected function assertContainerBuilderHasServiceDefinitionWithServiceLocatorArgument(
180-
$serviceId,
157+
string $serviceId,
181158
$argumentIndex,
182159
array $expectedValue
183-
) {
160+
): void {
184161
self::assertThat(
185162
$this->container,
186163
new DefinitionArgumentEqualsServiceLocatorConstraint($serviceId, $argumentIndex, $expectedValue)
@@ -191,17 +168,14 @@ protected function assertContainerBuilderHasServiceDefinitionWithServiceLocatorA
191168
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has a method
192169
* call to the given method with the given arguments.
193170
*
194-
* @param string $serviceId
195-
* @param string $method
196-
* @param array $arguments
197171
* @param int|null $index
198172
*/
199173
protected function assertContainerBuilderHasServiceDefinitionWithMethodCall(
200-
$serviceId,
201-
$method,
174+
string $serviceId,
175+
string $method,
202176
array $arguments = [],
203177
$index = null
204-
) {
178+
): void {
205179
$definition = $this->container->findDefinition($serviceId);
206180

207181
self::assertThat($definition, new DefinitionHasMethodCallConstraint($method, $arguments, $index));
@@ -210,16 +184,12 @@ protected function assertContainerBuilderHasServiceDefinitionWithMethodCall(
210184
/**
211185
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has a tag
212186
* with the given attributes.
213-
*
214-
* @param string $serviceId
215-
* @param string $tag
216-
* @param array $attributes
217187
*/
218188
protected function assertContainerBuilderHasServiceDefinitionWithTag(
219-
$serviceId,
220-
$tag,
189+
string $serviceId,
190+
string $tag,
221191
array $attributes = []
222-
) {
192+
): void {
223193
$definition = $this->container->findDefinition($serviceId);
224194

225195
self::assertThat($definition, new DefinitionHasTagConstraint($tag, $attributes));
@@ -228,11 +198,8 @@ protected function assertContainerBuilderHasServiceDefinitionWithTag(
228198
/**
229199
* Assert that the ContainerBuilder for this test has a service definition with the given id which is a decorated
230200
* service and it has the given parent service.
231-
*
232-
* @param $serviceId
233-
* @param $parentServiceId
234201
*/
235-
protected function assertContainerBuilderHasServiceDefinitionWithParent($serviceId, $parentServiceId)
202+
protected function assertContainerBuilderHasServiceDefinitionWithParent(string $serviceId, string $parentServiceId): void
236203
{
237204
$definition = $this->container->findDefinition($serviceId);
238205

@@ -242,10 +209,9 @@ protected function assertContainerBuilderHasServiceDefinitionWithParent($service
242209
/**
243210
* Assert that the ContainerBuilder for this test has a ServiceLocator service definition with the given id.
244211
*
245-
* @param string $serviceId
246-
* @param array $expectedServiceMap an array of service-id references and their key in the map
212+
* @param array $expectedServiceMap an array of service-id references and their key in the map
247213
*/
248-
protected function assertContainerBuilderHasServiceLocator(string $serviceId, array $expectedServiceMap = [])
214+
protected function assertContainerBuilderHasServiceLocator(string $serviceId, array $expectedServiceMap = []): void
249215
{
250216
$definition = $this->container->findDefinition($serviceId);
251217

PhpUnit/AbstractExtensionConfigurationTestCase.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ abstract class AbstractExtensionConfigurationTestCase extends TestCase
1313
{
1414
/**
1515
* Return an instance of the container extension that you are testing.
16-
*
17-
* @return ExtensionInterface
1816
*/
19-
abstract protected function getContainerExtension();
17+
abstract protected function getContainerExtension(): ExtensionInterface;
2018

2119
/**
2220
* Return an instance of the configuration class that you are testing.
23-
*
24-
* @return ConfigurationInterface
2521
*/
26-
abstract protected function getConfiguration();
22+
abstract protected function getConfiguration(): ConfigurationInterface;
2723

28-
protected function assertProcessedConfigurationEquals($expectedConfiguration, array $sources)
24+
protected function assertProcessedConfigurationEquals(array $expectedConfiguration, array $sources): void
2925
{
3026
$extensionConfigurationBuilder = new ExtensionConfigurationBuilder(new LoaderFactory());
3127
$extensionConfiguration = $extensionConfigurationBuilder

0 commit comments

Comments
 (0)