Optional parameter $elements declared before required parameter $field is implicitly treated as a required parameter

Created on 12 July 2024, 6 months ago
Updated 19 September 2024, 4 months ago

Problem/Motivation

I get these three warnings when running graphql_webform on PHP 8.3:

NOTICE: PHP message: PHP Deprecated:  Optional parameter $elements declared before required parameter $field is implicitly treated as a required parameter in /app/web/modules/contrib/graphql_webform/src/Plugin/GraphQL/DataProducer/WebformSubmit.php on line 122
NOTICE: PHP message: PHP Deprecated:  Optional parameter $sourceEntityType declared before required parameter $field is implicitly treated as a required parameter in /app/web/modules/contrib/graphql_webform/src/Plugin/GraphQL/DataProducer/WebformSubmit.php on line 122
NOTICE: PHP message: PHP Deprecated:  Optional parameter $sourceEntityId declared before required parameter $field is implicitly treated as a required parameter in /app/web/modules/contrib/graphql_webform/src/Plugin/GraphQL/DataProducer/WebformSubmit.php on line 122

For some reason that actually breaks the submit webform requests even though those are just warnings...

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇨🇭Switzerland Lukas von Blarer

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Lukas von Blarer
  • Status changed to Needs review 6 months ago
  • 🇨🇭Switzerland Lukas von Blarer

    I needed a quick fix and simpy made all mentioned parameters required. This fixes the requests for me. But this probably should be fixed differently.

  • Pipeline finished with Failed
    6 months ago
    Total: 215s
    #222790
  • Status changed to Needs work 6 months ago
  • 🇨🇭Switzerland Lukas von Blarer

    I'm sorry, The cause of my errors was something else. Still, this should be fixed I guess.

  • 🇧🇬Bulgaria pfrenssen Sofia

    This should probably be fixed in the GraphQL module. They are now only calling the ::resolve() method with a simple call_user_func_array() with the `$field` argument as the last one in the list. Ref.\Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase::resolveField()

    This can be refactored to use the `ReflectionMethod` class to inspect the argument order. I asked an LLM for a suggested approach and got this example:

    public function resolveField(FieldContext $field) {
        if (!method_exists($this, 'resolve')) {
            throw new \LogicException('Missing data producer resolve method.');
        }
    
        $reflectionMethod = new \ReflectionMethod($this, 'resolve');
        $params = $reflectionMethod->getParameters();
        $arguments = [];
        $contextValues = $this->configuration['dataproducer_populate_default_values'] ?? TRUE
            ? $this->getContextValuesWithDefaults()
            : $this->getContextValues();
    
        foreach ($params as $param) {
            $name = $param->getName();
            if ($name === 'field') {
                $arguments[] = $field;
            } elseif (array_key_exists($name, $contextValues)) {
                $arguments[] = $contextValues[$name];
            } elseif ($param->isDefaultValueAvailable()) {
                $arguments[] = $param->getDefaultValue();
            } else {
                throw new \InvalidArgumentException("Missing required argument: $name");
            }
        }
    
        return $reflectionMethod->invokeArgs($this, $arguments);
    }
    

    If this is fixed in the GraphQL module we can reorder the arguments into their proper positions and don't need to use an incorrect method signature.

  • Pipeline finished with Skipped
    4 months ago
    #274848
  • Status changed to Fixed 4 months ago
  • 🇧🇬Bulgaria pfrenssen Sofia

    Thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024