From 624e3dd0b234c4bab3e537173421045c7c82590c Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 10 Oct 2020 17:54:50 +0200 Subject: [PATCH] Fix deprecation warnings in form builder in PHP 8 Close #3489 --- .../CustomFormDataProcessor.class.php | 46 ++++++++++++------- .../validation/FormFieldValidator.class.php | 14 ++++-- 2 files changed, 40 insertions(+), 20 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..98a1374083 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,17 +61,30 @@ 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))) { + if (PHP_MAJOR_VERSION >= 8) { + $parameterType = $parameters[0]->getType(); + } + else { + $parameterType = $parameters[0]->getClass(); + } + + if ($parameterType === null || ($parameterType->getName() !== IFormDocument::class && !is_subclass_of($parameterType->getName(), 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->getName() . "'") . " parameter is expected." ); } - if (!$parameters[1]->isArray()) { - throw new \InvalidArgumentException("The form data processor function's second parameter must be an array."); + if (PHP_MAJOR_VERSION >= 8) { + $parameterType = $parameters[1]->getType(); + } + else { + $parameterType = $parameters[1]->getClass(); + } + + if ($parameterType === null || $parameterType->getName() !== 'array') { + throw new \InvalidArgumentException("The form data processor function's second parameter must be an array, instead " . + ($parameterType === null ? 'any' : "'" . $parameterType->getName() . "'") . " parameter is expected."); } $this->formDataProcessor = $formDataProcessor; @@ -87,22 +100,23 @@ 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))) { + $parameterType = $parameters[0]->getType(); + if ($parameterType === null || ($parameterType->getName() !== IFormDocument::class && !is_subclass_of($parameterType->getName(), 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." + "The form data processor function's first parameter must be an instance of '" . IFormDocument::class . "', instead " . + ($parameterType === null ? 'any' : "'" . $parameterType->getName() . "'") . " parameter is expected." ); } - if (!$parameters[1]->isArray()) { - throw new \InvalidArgumentException("The object data processor function's second parameter must be an array."); + $parameterType = $parameters[1]->getType(); + if ($parameterType === null || $parameterType->getName() !== 'array') { + throw new \InvalidArgumentException("The form data processor function's second parameter must be an array, instead " . + ($parameterType === null ? 'any' : "'" . $parameterType->getName() . "'") . " parameter is expected."); } - $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 ($parameterType === null || $parameterType->getName() !== 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->getName() . "'") . " 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..9540f3bb92 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,18 @@ 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))) { + + if (PHP_MAJOR_VERSION >= 8) { + $parameterType = $parameters[0]->getType(); + } + else { + $parameterType = $parameters[0]->getClass(); + } + + if ($parameterType === null || ($parameterType->getName() !== IFormField::class && !is_subclass_of($parameterType->getName(), 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->getName() . "'") . " parameter is expected." ); } -- 2.20.1