10000 bug #37857 [PropertyInfo] Fix ReflectionExtractor + minor tweaks (ogi… · symfony/symfony@679cc4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 679cc4d

Browse files
committed
bug #37857 [PropertyInfo] Fix ReflectionExtractor + minor tweaks (ogizanagi)
This PR was merged into the 5.1 branch. Discussion ---------- [PropertyInfo] Fix ReflectionExtractor + minor tweaks | Q | A | ------------- | --- | Branch? | 5.1 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | N/A Spotted while rebasing #32133 Commits ------- 7ccb5a1 [PropertyInfo] Fix ReflectionExtractor + minor tweaks
2 parents c426abe + 7ccb5a1 commit 679cc4d

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function getReadInfo(string $class, string $property, array $context = []
272272
/**
273273
* {@inheritdoc}
274274
*/
275-
public function getWriteInfo(string $class, string $property, array $context = []): PropertyWriteInfo
275+
public function getWriteInfo(string $class, string $property, array $context = []): ?PropertyWriteInfo
276276
{
277277
try {
278278
$reflClass = new \ReflectionClass($class);
@@ -308,10 +308,10 @@ public function getWriteInfo(string $class, string $property, array $context = [
308308
$mutator->setRemoverInfo(new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $removerAccessName, $this->getWriteVisiblityForMethod($removerMethod), $removerMethod->isStatic()));
309309

310310
return $mutator;
311-
} else {
312-
$errors = array_merge($errors, $adderAndRemoverErrors);
313311
}
314312

313+
$errors = array_merge($errors, $adderAndRemoverErrors);
314+
315315
foreach ($this->mutatorPrefixes as $mutatorPrefix) {
316316
$methodName = $mutatorPrefix.$camelized;
317317

@@ -330,12 +330,14 @@ public function getWriteInfo(string $class, string $property, array $context = [
330330

331331
$getsetter = lcfirst($camelized);
332332

333-
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
334-
if ($allowGetterSetter && $accessible) {
335-
$method = $reflClass->getMethod($getsetter);
333+
if ($allowGetterSetter) {
334+
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
335+
if ($accessible) {
336+
$method = $reflClass->getMethod($getsetter);
337+
338+
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $getsetter, $this->getWriteVisiblityForMethod($method), $method->isStatic());
339+
}
336340

337-
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $getsetter, $this->getWriteVisiblityForMethod($method), $method->isStatic());
338-
} else {
339341
$errors = array_merge($errors, $methodAccessibleErrors);
340342
}
341343

@@ -348,25 +350,27 @@ public function getWriteInfo(string $class, string $property, array $context = [
348350
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__set', 2);
349351
if ($accessible) {
350352
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
351-
} else {
352-
$errors = array_merge($errors, $methodAccessibleErrors);
353353
}
354354

355-
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
356-
if ($allowMagicCall && $accessible) {
357-
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
358-
} else {
355+
$errors = array_merge($errors, $methodAccessibleErrors);
356+
357+
if ($allowMagicCall) {
358+
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
359+
if ($accessible) {
360+
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
361+
}
362+
359363
$errors = array_merge($errors, $methodAccessibleErrors);
360364
}
361365

362366
if (!$allowAdderRemover && null !== $adderAccessName && null !== $removerAccessName) {
363-
$errors = array_merge($errors, [sprintf(
367+
$errors[] = sprintf(
364368
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
365369
'the new value must be an array or an instance of \Traversable',
366370
$property,
367371
$reflClass->getName(),
368372
implode('()", "', [$adderAccessName, $removerAccessName])
369-
)]);
373+
);
370374
}
371375

372376
$noneProperty = new PropertyWriteInfo();
@@ -626,7 +630,7 @@ private function getPropertyName(string $methodName, array $reflectionProperties
626630
private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars): array
627631
{
628632
if (!\is_array($this->arrayMutatorPrefixes) && 2 !== \count($this->arrayMutatorPrefixes)) {
629-
return null;
633+
return [null, null, []];
630634
}
631635

632636
[$addPrefix, $removePrefix] = $this->arrayMutatorPrefixes;
@@ -642,7 +646,9 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
642646

643647
if ($addMethodFound && $removeMethodFound) {
644648
return [$addMethod, $removeMethod, []];
645-
} elseif ($addMethodFound && !$removeMethodFound) {
649+
}
650+
651+
if ($addMethodFound && !$removeMethodFound) {
646652
$errors[] = sprintf('The add method "%s" in class "%s" was found, but the corresponding remove method "%s" was not found', $addMethod, $reflClass->getName(), $removeMethod);
647653
} elseif (!$addMethodFound && $removeMethodFound) {
648654
$errors[] = sprintf('The remove method "%s" in class "%s" was found, but the corresponding add method "%s" was not found', $removeMethod, $reflClass->getName(), $addMethod);

0 commit comments

Comments
 (0)
0