8000 [PropertyInfo] Fix ReflectionExtractor + minor tweaks by ogizanagi · Pull Request #37857 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Fix ReflectionExtractor + minor tweaks #37857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6E5C
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function getReadInfo(string $class, string $property, array $context = []
/**
* {@inheritdoc}
*/
public function getWriteInfo(string $class, string $property, array $context = []): PropertyWriteInfo
public function getWriteInfo(string $class, string $property, array $context = []): ?PropertyWriteInfo
{
try {
$reflClass = new \ReflectionClass($class);
Expand Down Expand Up @@ -308,10 +308,10 @@ public function getWriteInfo(string $class, string $property, array $context = [
$mutator->setRemoverInfo(new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $removerAccessName, $this->getWriteVisiblityForMethod($removerMethod), $removerMethod->isStatic()));

return $mutator;
} else {
$errors = array_merge($errors, $adderAndRemoverErrors);
}

$errors = array_merge($errors, $adderAndRemoverErrors);

foreach ($this->mutatorPrefixes as $mutatorPrefix) {
$methodName = $mutatorPrefix.$camelized;

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

$getsetter = lcfirst($camelized);

[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
if ($allowGetterSetter && $accessible) {
$method = $reflClass->getMethod($getsetter);
if ($allowGetterSetter) {
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
if ($accessible) {
$method = $reflClass->getMethod($getsetter);

return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $getsetter, $this->getWriteVisiblityForMethod($method), $method->isStatic());
}

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

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

[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to compute method is accessible nor adding errors if the magic __call isn't allowed, right?

if ($allowMagicCall && $accessible) {
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
} else {
$errors = array_merge($errors, $methodAccessibleErrors);

if ($allowMagicCall) {
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
if ($accessible) {
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
}

$errors = array_merge($errors, $methodAccessibleErrors);
}

if (!$allowAdderRemover && null !== $adderAccessName && null !== $removerAccessName) {
$errors = array_merge($errors, [sprintf(
$errors[] = sprintf(
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
'the new value must be an array or an instance of \Traversable',
$property,
$reflClass->getName(),
implode('()", "', [$adderAccessName, $removerAccessName])
)]);
);
}

$noneProperty = new PropertyWriteInfo();
Expand Down Expand Up @@ -626,7 +630,7 @@ private function getPropertyName(string $methodName, array $reflectionProperties
private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars): array
{
if (!\is_array($this->arrayMutatorPrefixes) && 2 !== \count($this->arrayMutatorPrefixes)) {
return null;
return [null, null, []];
}

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

if ($addMethodFound && $removeMethodFound) {
return [$addMethod, $removeMethod, []];
} elseif ($addMethodFound && !$removeMethodFound) {
}

if ($addMethodFound && !$removeMethodFound) {
$errors[] = sprintf('The add method "%s" in class "%s" was found, but the corresponding remove method "%s" was not found', $addMethod, $reflClass->getName(), $removeMethod);
} elseif (!$addMethodFound && $removeMethodFound) {
$errors[] = sprintf('The remove method "%s" in class "%s" was found, but the corresponding add method "%s" was not found', $removeMethod, $reflClass->getName(), $addMethod);
Expand Down
0