XF 2.0 asVisitor $user invocation parameter types are not compatible

LPH

Well-known member
The following appears to work but PhpStorm is showing an error:

Expected \XF\Entity\User, got \XF\Mvc\Entity\Entity
Invocation parameter types are not compatible with declared.

PHP:
/**
 * @param $post
 *
 * @return string
 * @throws \Exception
 */
public function insert( $post ) {

   $title = $this->getTitle( $post );
   $message = $this->getMessage( $post );

   // Get the forum using forum_id
   $forum_id = $this->getForumId( $post );

   $forum = \XF::em()->find( 'XF:Forum', $forum_id );

   $user = \XF::app()->finder('XF:User')->whereId($post->post_author)->fetchOne();

   \XF::asVisitor($user, function() use ($forum, $title, $message, $post)
   {
      /** @var \XF\Service\Thread\Creator $creator */
      $creator = \XF::service('XF:Thread\Creator', $forum);
      $creator->setContent( $title, $message );
      $creator->setPrefix( $this->getForumId( $post ) );
      $creator->setTags( $this->getTags( $post ) );
      $creator->setIsAutomated();

      if (!$creator->validate())
      {
         return null;
      }

      $creator->save();

      $creator->sendNotifications();

      $this->postMeta( $creator, $post );
   });

   return '';
}

Can someone explain the error? Is this due to the finder line with the whereId?

Next, I'd like to modify the edit($post) method but I cannot figure out how to modify the \XF::asVisitor because edit doesn't use Thread\Creator but rather Post\Editor. Any suggestions would be great.
 
PHP:
/** @var \XF\Entity\User $user **/
$user = \XF::app()->finder('XF:User')->whereId($post->post_author)->fetchOne();


Fillip
 
  • Like
Reactions: LPH
Yes. That's creating the $user. The $post->post_author is the ID from the post author in WordPress.

I can re-write it to

PHP:
$user = \XF::app()->find('XF:User', $post->post_author);

And still get the same error.

Update: OH! You mean it's just a matter of type hinting! Awesome. Now, I'll ask about alerts in another thread.
 
Top Bottom