As designed Passing guest/0 to XF\Entity\User >> Argument 1 passed to XF\Entity\User::canStartConversationWith() must be an instance of XF\Entity\User, null given

hstammlerj

Active member
Affected version
2.1.10 Patch 2
One single (self-coded) if-clause in template thread_view:
Code:
<xf:if is="$xf.visitor.canStartConversationWith($thread.User)">
...causes big mess if the thread starter isn't registered anymore.

Passing a user_id=0 to canStartConversationWith() affects this error:
Code:
An exception occurred: [TypeError] Argument 1 passed to XF\Entity\User::canStartConversationWith() must be an instance of XF\Entity\User, null given in src/XF/Entity/User.php on line 828

    XF\Entity\User->canStartConversationWith()
    call_user_func_array() in src/XF/Template/Templater.php at line 999
    XF\Template\Templater->method() in internal_data/code_cache/templates/l13/s4/public/thread_view.php at line 712
    XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1315
    XF\Template\Templater->renderTemplate() in src/XF/Template/Template.php at line 24
    XF\Template\Template->render() in src/XF/Mvc/Renderer/Html.php at line 48
    XF\Mvc\Renderer\Html->renderView() in src/XF/Mvc/Dispatcher.php at line 458
    XF\Mvc\Dispatcher->renderView() in src/XF/Mvc/Dispatcher.php at line 440
    XF\Mvc\Dispatcher->renderReply() in src/XF/Mvc/Dispatcher.php at line 400
    XF\Mvc\Dispatcher->render() in src/XF/Mvc/Dispatcher.php at line 58
    XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2190
    XF\App->run() in src/XF.php at line 391
    XF::runApp() in index.php at line 20

Code:
array(4) {
  ["url"] => string(23) "/threads/asw-15-b.9352/"
  ["referrer"] => bool(false)
  ["_GET"] => array(0) {
  }
  ["_POST"] => array(0) {
  }
}

The original thread displayed w/o error: https://www.rc-network.de/threads/asw-15-b.9352/

My workaround: Capsuling if-clauses inside each other, first/outside checking if the thread starter still exists and afterwards/inside calling the user-entities. Since there are more issues reported in the past, it feels to me like it could be a general issue around that entity.
At least one bug report is proposed to be fixed for a special case:
---
Others are more unclear or discussed add-on related, also in 2.2 branch:

It looks like most people build pre-check workarounds to fix the issues. For me, it's not clear if that's the way it should be or not:
You can say: It's a "bad template" problem.
Or you can say: The object isn't able to cope an empty/NULL id value correctly, since it throws a hard error instead of e.g. an empty result.

My gut feeling: Capsuling independent calls because they screw up fully if they've been given an empty parameter just doesn't feel right for me... ;)
 
Last edited:
The solution is simple and it is to expand your condition to check for a value before use:

Code:
<xf:if is="$thread.User AND $xf.visitor.canStartConversationWith($thread.User)">

Many of our available PHP function calls require type specific values to be passed in and using strict types is generally the norm. PHP has historically been less type-strict but it is becoming more so. While being strict may mean that certain calls require more work and may not immediately seem useful, on the whole having strict mechanisms to ensure we're not accidentally passing in the wrong things is absolutely a benefit and ultimately reduces bugs.

We won't be making any changes here or in similar places as the behaviour is very much intentional.
 
Back
Top Bottom