call to method exists throws an error.

Created on 23 August 2023, over 1 year ago
Updated 24 August 2023, over 1 year ago

Problem/Motivation

This error appears on occasion. Likely needs a defensive check.

TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given in method_exists() (line 136 of /var/www/cms/docroot/modules/contrib/post_api/src/Plugin/QueueWorker/PostApiQueueBase.php) #0 /var/www/cms/docroot/modules/contrib/post_api/src/Plugin/QueueWorker/PostApiQueueBase.php(136): method_exists(500, 'getStatusCode') #1 /var/www/cms/docroot/modules/contrib/post_api/src/Form/PostApiQueueForm.php(180): Drupal\post_api\Plugin\QueueWorker\PostApiQueueBase->processQueue() #2 [internal function]: Drupal\post_api\Form\PostApiQueueForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #3 /var/www/cms/docroot/core/lib/Drupal/Core/Form/FormSubmitter.php(114): call_user_func_array(Array, Array) #4 /var/www/cms/docroot/core/lib/Drupal/Core/Form/FormSubmitter.php(52): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #5 /var/www/cms/docroot/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array,

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇺🇸United States swirt Florida

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

Comments & Activities

  • Issue created by @swirt
  • 🇮🇳India Shreya_98

    The error occurs in the code at line 136 of the PostApiQueueBase.php file, where we are using method_exists() with an incorrect argument type i.e. $response_code = (method_exists($response, 'getStatusCode')) ? $response->getStatusCode() : 500;
    In this line, we trying to check if the $response object has a method called getStatusCode(). However, the $response variable seems to hold an HTTP response instance, which should already have the getStatusCode() method. Therefore, there's no need to use method_exists() here.
    As per my knowledge i think To solve this issue, we can directly call the getStatusCode() method on the $response object.
    no need to using method_exists().

  • 🇮🇳India dineshkumarbollu

    Hi
    In this line $response_code = (method_exists($response, 'getStatusCode')) ? $response->getStatusCode() : 500;
    if No response then they are adding $response_code as '500' i.e Internal Server Error, the issue is Response they are getting at line 135 is not a object $response = $this->processItem($item->data);.

  • 🇮🇳India mohd sahzad

    i have created patch for this call to method exists throws an error.

  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States swirt Florida

    Wow. I have never seen such a fast response from so many people. Thanks for all the help.
    Patch #2 is targeting the wrong problem. We can't forgo the call to method_exists because sometimes the response is not an object. So, as @dineshkumarbollu called out removing it does not prevent the problem we would have calling ->getStatusCode() on something that is not a response object.

    I think the defensive check of ` if (!is_int($response)) {` in patch #4 is close, but affirming the indirect thing, instead of affirming the direct thing. What we really want to check is, is the $response we are about to act on an actual \Psr\Http\Message\ResponseInterface So it would be more direct to have the check be

    if ($response instanceof ResponseInterface)  {
    
  • 🇺🇸United States swirt Florida

    The simplest fix is to replace

    $response_code = (method_exists($response, 'getStatusCode')) ? $response->getStatusCode() : 500;
    $reason_phrase = (method_exists($response, 'getReasonPhrase')) ? $response->getReasonPhrase() : 'none provided';
    

    with

    $response_code = ($response instanceof ResponseInterface) ? $response->getStatusCode() : 500;
    $reason_phrase = ($response instanceof ResponseInterface) ? $response->getReasonPhrase() : 'none provided';
    
    • swirt committed c290b35e on 2.x
      Issue #3382748 Fix call to method exists throws an error
      
  • Status changed to Fixed over 1 year ago
  • 🇺🇸United States swirt Florida
  • 🇺🇸United States swirt Florida

    This will go out with 1.0.5

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

Production build 0.71.5 2024