Skip to content

Commit 7faebc4

Browse files
Fix bug: factory services are allowed to have an interface as class
1 parent d110f7d commit 7faebc4

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

ServiceDefinitionValidator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private function validateClass(Definition $definition)
6161
if ($class) {
6262
$class = $this->containerBuilder->getParameterBag()->resolveValue($class);
6363

64+
// TODO only services created using a factory can have an interface
6465
if (!class_exists($class) && !interface_exists($class)) {
6566
throw new ClassNotFoundException($class);
6667
}
@@ -113,7 +114,7 @@ private function validateFactoryService(Definition $definition)
113114

114115
private function validateFactoryClassAndMethod($factoryClass, $factoryMethod)
115116
{
116-
if (!class_exists($factoryClass)) {
117+
if (!class_exists($factoryClass) && !interface_exists($factoryClass)) {
117118
throw new ClassNotFoundException($factoryClass);
118119
}
119120

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures;
4+
5+
class Validator
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures;
4+
5+
interface ValidatorBuilderInterface
6+
{
7+
public function build();
8+
}

Tests/Functional/Fixtures/reported_problems.yml

+10
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ services:
77
password: "password"
88
calls:
99
- [setAttribute, [3, 2]]
10+
11+
# Issue 12: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/12
12+
# A service created by a factory might itself be created by another factory, in which case it should be possible to
13+
# supply an interface, not a class
14+
validator_builder:
15+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\ValidatorBuilderInterface
16+
validator:
17+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Validator
18+
factory_service: validator_builder
19+
factory_method: build

0 commit comments

Comments
 (0)