From 26ead9f58b1554956e8fb46133e20a5f3fdf5d25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 20 Oct 2020 12:02:22 +0200 Subject: [PATCH] Fix PHP 8 support in form builder > Message: Method ReflectionParameter::getClass() is deprecated Fixes #3489 --- .../CustomFormDataProcessor.class.php | 63 +++++++++++++++---- .../validation/FormFieldValidator.class.php | 15 +++-- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/data/processor/CustomFormDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/data/processor/CustomFormDataProcessor.class.php index 1920721f76..f4a15954b4 100644 --- a/wcfsetup/install/files/lib/system/form/builder/data/processor/CustomFormDataProcessor.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/data/processor/CustomFormDataProcessor.class.php @@ -61,16 +61,31 @@ class CustomFormDataProcessor extends AbstractFormDataProcessor { ); } - /** @var \ReflectionClass $parameterClass */ - $parameterClass = $parameters[0]->getClass(); - if ($parameterClass === null || ($parameterClass->getName() !== IFormDocument::class && !is_subclass_of($parameterClass->getName(), IFormDocument::class))) { + /** @var \ReflectionType $parameterType */ + $parameterType = $parameters[0]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === IFormDocument::class || is_subclass_of($parameterType->getName(), IFormDocument::class))) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === IFormDocument::class || is_subclass_of($parameterType->__toString(), IFormDocument::class))) + )) { throw new \InvalidArgumentException( "The form data processor function's first parameter must be an instance of '" . IFormDocument::class . "', instead " . - ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." + @($parameterType === null ? 'any' : "'" . $parameterType . "'") . " parameter is expected." ); } - if (!$parameters[1]->isArray()) { + $parameterType = $parameters[1]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === 'array')) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === 'array')) + )) { throw new \InvalidArgumentException("The form data processor function's second parameter must be an array."); } @@ -87,22 +102,44 @@ class CustomFormDataProcessor extends AbstractFormDataProcessor { ); } - /** @var \ReflectionClass $parameterClass */ - $parameterClass = $parameters[0]->getClass(); - if ($parameterClass === null || ($parameterClass->getName() !== IFormDocument::class && !is_subclass_of($parameterClass->getName(), IFormDocument::class))) { + /** @var \ReflectionType $parameterType */ + $parameterType = $parameters[0]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === IFormDocument::class || is_subclass_of($parameterType->getName(), IFormDocument::class))) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === IFormDocument::class || is_subclass_of($parameterType->__toString(), IFormDocument::class))) + )) { throw new \InvalidArgumentException( "The object data processor function's first parameter must be an instance of '" . IFormDocument::class . "', instead " . - ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." + @($parameterType === null ? 'any' : "'" . $parameterType . "'") . " parameter is expected." ); } - if (!$parameters[1]->isArray()) { + $parameterType = $parameters[1]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === 'array')) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === 'array')) + )) { throw new \InvalidArgumentException("The object data processor function's second parameter must be an array."); } - $parameterClass = $parameters[2]->getClass(); - if ($parameterClass === null || $parameterClass->getName() !== IStorableObject::class) { - throw new \InvalidArgumentException("The object data processor function's third parameter must be an instance of '" . IStorableObject::class . "', instead " . ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected."); + $parameterType = $parameters[2]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === IStorableObject::class)) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === IStorableObject::class)) + )) { + throw new \InvalidArgumentException("The object data processor function's third parameter must be an instance of '" . IStorableObject::class . "', instead " . @($parameterType === null ? 'any' : "'" . $parameterType . "'") . " parameter is expected."); } $this->objectDataProcessor = $objectDataProcessor; diff --git a/wcfsetup/install/files/lib/system/form/builder/field/validation/FormFieldValidator.class.php b/wcfsetup/install/files/lib/system/form/builder/field/validation/FormFieldValidator.class.php index 79c69f1d3c..f5ef82db17 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/validation/FormFieldValidator.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/validation/FormFieldValidator.class.php @@ -37,12 +37,19 @@ class FormFieldValidator implements IFormFieldValidator { if (count($parameters) !== 1) { throw new \InvalidArgumentException("The validation function must expect one parameter, instead " . count($parameters) . " parameters are expected."); } - /** @var \ReflectionClass $parameterClass */ - $parameterClass = $parameters[0]->getClass(); - if ($parameterClass === null || ($parameterClass->getName() !== IFormField::class && !is_subclass_of($parameterClass->getName(), IFormField::class))) { + /** @var \ReflectionType $parameterType */ + $parameterType = $parameters[0]->getType(); + if (!( + // PHP 7.1+ + ($parameterType instanceof \ReflectionNamedType && + ($parameterType->getName() === IFormField::class || is_subclass_of($parameterType->getName(), IFormField::class))) || + // PHP 7.0 + (get_class($parameterType) === \ReflectionType::class && + ($parameterType->__toString() === IFormField::class || is_subclass_of($parameterType->__toString(), IFormField::class))) + )) { throw new \InvalidArgumentException( "The validation function's parameter must be an instance of '" . IFormField::class . "', instead " . - ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." + @($parameterType === null ? 'any' : "'" . $parameterType . "'") . " parameter is expected." ); } -- 2.20.1