Skip to content

Commit 5f5f4b5

Browse files
Merge branch '5.1'
* 5.1: (28 commits) [DI] fix Use "composer/package-versions-deprecated" when possible Fix Small update in our internal terminology Fix support for PHP8 union types [VarDumper] fix typo [Lock][Messenger] Fix precedence of DSN options for 5.1 Fix support for PHP8 union types [FrameworkBundle] preserve dots in query-string when redirecting [3.4] Fix support for PHP8 union types [PhpUnitBridge] Streamline ansi/no-ansi of composer according to phpunit --colors option [3.4] Small update in our internal terminology [Cache] fix compat with DBAL v3 Remove unnecessary null check [HttpFoundation] Allow `null` in InputBag@set [HttpClient] Convert CurlHttpClient::handlePush() to instance method Fix package rename when releasing bumped Symfony version to 5.1.3 updated VERSION for 5.1.2 updated CHANGELOG for 5.1.2 ...
2 parents bb3e33b + 04b0e51 commit 5f5f4b5

File tree

8 files changed

+76
-37
lines changed

8 files changed

+76
-37
lines changed

Compiler/AutowireRequiredPropertiesPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function processValue($value, bool $isRoot = false)
4242

4343
$properties = $value->getProperties();
4444
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
45-
if (!$reflectionProperty->hasType()) {
45+
if (!($type = $reflectionProperty->getType()) instanceof \ReflectionNamedType) {
4646
continue;
4747
}
4848
if (false === $doc = $reflectionProperty->getDocComment()) {
@@ -55,7 +55,7 @@ protected function processValue($value, bool $isRoot = false)
5555
continue;
5656
}
5757

58-
$type = $reflectionProperty->getType()->getName();
58+
$type = $type->getName();
5959
$value->setProperty($name, new TypedReference($type, $type, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name));
6060
}
6161

Compiler/CheckTypeDeclarationsPass.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,26 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
153153
/**
154154
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
155155
*/
156-
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix): void
156+
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix, string $type = null): void
157157
{
158-
$type = $parameter->getType()->getName();
158+
if (null === $type) {
159+
$type = $parameter->getType();
160+
161+
if ($type instanceof \ReflectionUnionType) {
162+
foreach ($type->getTypes() as $type) {
163+
try {
164+
$this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $type);
165+
166+
return;
167+
} catch (InvalidParameterTypeException $e) {
168+
}
169+
}
170+
171+
throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter);
172+
}
173+
174+
$type = $type->getName();
175+
}
159176

160177
if ($value instanceof Reference) {
161178
if (!$this->container->has($value = (string) $value)) {
@@ -266,7 +283,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
266283
return;
267284
}
268285

269-
$checkFunction = sprintf('is_%s', $parameter->getType()->getName());
286+
$checkFunction = sprintf('is_%s', $type);
270287

271288
if (!$parameter->getType()->isBuiltin() || !$checkFunction($value)) {
272289
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : get_debug_type($value), $parameter);

Dumper/PhpDumper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function dump(array $options = [])
227227
$regex = preg_quote(\DIRECTORY_SEPARATOR.$dir[$i], '#').$regex;
228228
} while (0 < --$i);
229229

230-
$this->targetDirRegex = '#'.preg_quote($dir[0], '#').$regex.'#';
230+
$this->targetDirRegex = '#(^|file://|[:;, \|\r\n])'.preg_quote($dir[0], '#').$regex.'#';
231231
}
232232
}
233233

@@ -2030,11 +2030,12 @@ private function isSingleUsePrivateNode(ServiceReferenceGraphNode $node): bool
20302030
private function export($value)
20312031
{
20322032
if (null !== $this->targetDirRegex && \is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
2033-
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
20342033
$suffix = $matches[0][1] + \strlen($matches[0][0]);
2034+
$matches[0][1] += \strlen($matches[1][0]);
2035+
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
20352036
$suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : '';
20362037
$dirname = $this->asFiles ? '$this->containerDir' : '__DIR__';
2037-
$offset = 1 + $this->targetDirMaxMatches - \count($matches);
2038+
$offset = 2 + $this->targetDirMaxMatches - \count($matches);
20382039

20392040
if (0 < $offset) {
20402041
$dirname = sprintf('\dirname(__DIR__, %d)', $offset + (int) $this->asFiles);

Dumper/Preloader.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function preload(array $classes): void
6767
}
6868
}
6969

70-
private static function doPreload(string $class, array &$preloaded)
70+
private static function doPreload(string $class, array &$preloaded): void
7171
{
7272
if (isset($preloaded[$class]) || \in_array($class, ['self', 'static', 'parent'], true)) {
7373
return;
@@ -87,9 +87,7 @@ private static function doPreload(string $class, array &$preloaded)
8787

8888
if (\PHP_VERSION_ID >= 70400) {
8989
foreach ($r->getProperties(\ReflectionProperty::IS_PUBLIC) as $p) {
90-
if (($t = $p->getType()) && !$t->isBuiltin()) {
91-
self::doPreload($t->getName(), $preloaded);
92-
}
90+
self::preloadType($p->getType(), $preloaded);
9391
}
9492
}
9593

@@ -103,17 +101,26 @@ private static function doPreload(string $class, array &$preloaded)
103101
}
104102
}
105103

106-
if (($t = $p->getType()) && !$t->isBuiltin()) {
107-
self::doPreload($t->getName(), $preloaded);
108-
}
104+
self::preloadType($p->getType(), $preloaded);
109105
}
110106

111-
if (($t = $m->getReturnType()) && !$t->isBuiltin()) {
112-
self::doPreload($t->getName(), $preloaded);
113-
}
107+
self::preloadType($p->getReturnType(), $preloaded);
114108
}
115109
} catch (\ReflectionException $e) {
116110
// ignore missing classes
117111
}
118112
}
113+
114+
private static function preloadType(?\ReflectionType $t, array &$preloaded): void
115+
{
116+
if (!$t || $t->isBuiltin()) {
117+
return;
118+
}
119+
120+
foreach ($t instanceof \ReflectionUnionType ? $t->getTypes() : [$t] as $t) {
121+
if (!$t->isBuiltin()) {
122+
self::doPreload($t instanceof \ReflectionNamedType ? $t->getName() : $t, $preloaded);
123+
}
124+
}
125+
}
119126
}

Exception/InvalidParameterTypeException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class InvalidParameterTypeException extends InvalidArgumentException
2121
{
2222
public function __construct(string $serviceId, string $type, \ReflectionParameter $parameter)
2323
{
24-
parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s::%s" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->getType()->getName(), $type));
24+
$acceptedType = $parameter->getType();
25+
$acceptedType = $acceptedType instanceof \ReflectionNamedType ? $acceptedType->getName() : (string) $acceptedType;
26+
$this->code = $type;
27+
28+
parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s::%s" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $acceptedType, $type));
2529
}
2630
}

LazyProxy/ProxyHelper.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,36 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
3131
if (!$type) {
3232
return null;
3333
}
34-
if (!\is_string($type)) {
35-
$name = $type->getName();
34+
35+
$types = [];
36+
37+
foreach ($type instanceof \ReflectionUnionType ? $type->getTypes() : [$type] as $type) {
38+
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
3639

3740
if ($type->isBuiltin()) {
38-
return $noBuiltin ? null : $name;
41+
if (!$noBuiltin) {
42+
$types[] = $name;
43+
}
44+
continue;
3945
}
40-
}
41-
$lcName = strtolower($name);
42-
$prefix = $noBuiltin ? '' : '\\';
4346

44-
if ('self' !== $lcName && 'parent' !== $lcName) {
45-
return $prefix.$name;
46-
}
47-
if (!$r instanceof \ReflectionMethod) {
48-
return null;
49-
}
50-
if ('self' === $lcName) {
51-
return $prefix.$r->getDeclaringClass()->name;
47+
$lcName = strtolower($name);
48+
$prefix = $noBuiltin ? '' : '\\';
49+
50+
if ('self' !== $lcName && 'parent' !== $lcName) {
51+
$types[] = '' !== $prefix ? $prefix.$name : $name;
52+
continue;
53+
}
54+
if (!$r instanceof \ReflectionMethod) {
55+
continue;
56+
}
57+
if ('self' === $lcName) {
58+
$types[] = $prefix.$r->getDeclaringClass()->name;
59+
} else {
60+
$types[] = ($parent = $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name : null;
61+
}
5262
}
5363

54-
return ($parent = $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name : null;
64+
return $types ? implode('|', $types) : null;
5565
}
5666
}

Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testDumpRelativeDir()
113113

114114
$container = new ContainerBuilder();
115115
$container->setDefinition('test', $definition);
116-
$container->setParameter('foo', 'wiz'.\dirname(__DIR__));
116+
$container->setParameter('foo', 'file://'.\dirname(__DIR__));
117117
$container->setParameter('bar', __DIR__);
118118
$container->setParameter('baz', '%bar%/PhpDumperTest.php');
119119
$container->setParameter('buz', \dirname(__DIR__, 2));

Tests/Fixtures/php/services12.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getRemovedIds(): array
5353
*/
5454
protected function getTestService()
5555
{
56-
return $this->services['test'] = new \stdClass(('wiz'.\dirname(__DIR__, 1)), [('wiz'.\dirname(__DIR__, 1)) => (\dirname(__DIR__, 2).'/')]);
56+
return $this->services['test'] = new \stdClass(('file://'.\dirname(__DIR__, 1)), [('file://'.\dirname(__DIR__, 1)) => (\dirname(__DIR__, 2).'/')]);
5757
}
5858

5959
public function getParameter(string $name)
@@ -102,7 +102,7 @@ private function getDynamicParameter(string $name)
102102
protected function getDefaultParameters(): array
103103
{
104104
return [
105-
'foo' => ('wiz'.\dirname(__DIR__, 1)),
105+
'foo' => ('file://'.\dirname(__DIR__, 1)),
106106
'bar' => __DIR__,
107107
'baz' => (__DIR__.'/PhpDumperTest.php'),
108108
'buz' => \dirname(__DIR__, 2),

0 commit comments

Comments
 (0)