Skip to content

Commit 907d504

Browse files
author
Andrey Kostylev
committed
fix bug: "Validation fails with factory service "public" set to false"
1 parent cffffb0 commit 907d504

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

ConstructorResolver.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function resolve(Definition $definition)
3636
return $this->resolveFactoryServiceWithMethod((string) $factory[0], $factory[1]);
3737
}
3838

39+
if (is_array($factory) && $factory[0] instanceof Definition) {
40+
return $this->resolveFactoryServiceDefinitionWithMethod($factory[0], $factory[1]);
41+
}
42+
3943
if (is_array($factory)) {
4044
return $this->resolveFactoryClassWithMethod($factory[0], $factory[1]);
4145
}
@@ -133,4 +137,15 @@ private function resolveFactoryFunction($factory)
133137

134138
return new \ReflectionFunction($factory);
135139
}
140+
141+
private function resolveFactoryServiceDefinitionWithMethod(Definition $factoryDefinition, $factoryMethod)
142+
{
143+
$factoryClass = $this->resultingClassResolver->resolve($factoryDefinition);
144+
145+
if (!method_exists($factoryClass, $factoryMethod)) {
146+
throw new MethodNotFoundException($factoryClass, $factoryMethod);
147+
}
148+
149+
return new \ReflectionMethod($factoryClass, $factoryMethod);
150+
}
136151
}

ServiceDefinitionValidator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Matthias\SymfonyServiceDefinitionValidator\Exception\ServiceNotFoundException;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
1111
use Symfony\Component\DependencyInjection\Definition;
12-
use Symfony\Component\DependencyInjection\Reference;
1312

1413
class ServiceDefinitionValidator implements ServiceDefinitionValidatorInterface
1514
{
@@ -95,6 +94,8 @@ private function validateFactory(Definition $definition)
9594
list($factoryClassOrService, $method) = $factory;
9695
if (is_string($factoryClassOrService)) {
9796
$this->validateFactoryClassAndMethod($factoryClassOrService, $method);
97+
} elseif ($factoryClassOrService instanceof Definition) {
98+
$this->validateFactoryServiceDefinitionAndMethod($factoryClassOrService, $method);
9899
} else {
99100
$this->validateFactoryServiceAndMethod((string) $factoryClassOrService, $method);
100101
}
@@ -164,4 +165,15 @@ private function validateFactoryServiceAndMethod($factoryServiceId, $factoryMeth
164165

165166
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
166167
}
168+
169+
private function validateFactoryServiceDefinitionAndMethod(Definition $factoryServiceDefinition, $factoryMethod)
170+
{
171+
if (!$factoryMethod) {
172+
throw new MissingFactoryMethodException();
173+
}
174+
175+
$factoryClass = $factoryServiceDefinition->getClass();
176+
177+
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
178+
}
167179
}

Tests/Functional/Fixtures/reported_problems.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@ services:
77
password: "password"
88
calls:
99
- [setAttribute, [3, 2]]
10+
11+
# Issue 33: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/33
12+
service_created_by_nonpublic_factory:
13+
class: stdClass
14+
factory: ["@nonpublic_factory", "create"]
15+
arguments:
16+
- required_argument
17+
18+
nonpublic_factory:
19+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Factory
20+
public: false

0 commit comments

Comments
 (0)