Dion Grendelman
Dion Grendelman

Reputation: 63

PhpStorm autocomplete STD Object not working

I'm using PhpStorm to build my next project. ATM I'm looking for a clean way to save all my post requests, instead of checking them all one by one using isset etc.. so I can loop my stdObject.

I'm trying to make a STD Object, but PHP is not autocompleting it. I think I should be using PHPDoc but can't get any to work.

So how do I get PhpStorm to autocomplete the STD Object

I hope someone knows how!

PS: If you think of a better way to save the $_POST value's let me know!

Code:

$errors = 0;

$mandatory = [
    'first_name' => $_POST['first_name'],
    'last_name' => $_POST['last_name'],
    ];

$mandatory = json_decode(json_encode($mandatory));

$mandatory->

Phpstorm Screenshot

Upvotes: 1

Views: 1090

Answers (3)

Álvaro González
Álvaro González

Reputation: 146450

Objects of type stdClass do not have standard properties, only custom ones. So PhpStorm does not have any obvious way to figure out what to suggest. Even a human will have troubles since your code feeds from external input thus can contain anything.

Hacking with stubs can be a way but it'll affect the entire codebase.

A possible solution is to declare a custom object anywhere in your project (the code is there to be parsed by PhpStorm, it does not even need to be executed at all):

class MandatoryPost extends stdClass
{
    /**
     * @var string
     */
    public $first_name;

    /**
     * @var string
     */
    public $last_name;
}

… and provide some hints here and there:

/** @var MandatoryPost $mandatory */
$mandatory->

However, it feels like complete overkill. The only reason why you have stdClass in the first place is because you perform a dubious JSON serialization on what's otherwise fairly procedural code. If you want to validate using objects then create a proper class that takes care of the whole thing:

$user = User::createFromPost($_POST);
if ($user->isComplete()) {
}

... or just work with plain arrays all the time.

Upvotes: 2

Sherin Bloemendaal
Sherin Bloemendaal

Reputation: 69

You can create a class

<?php

class formFields
{
  private $first_name;
  private $last_name;

  public function __construct($first_name, $last_name)
  {
    $this->first_name = $first_name;
    $this->last_name = $last_name;
  }

  /**
   * @return mixed
   */
  public function getFirstName()
  {
    return $this->first_name;
  }

  /**
   * @return mixed
   */
  public function getLastName()
  {
    return $this->last_name;
  }

  /**
   * @param mixed $first_name
   */
  public function setFirstName($first_name)
  {
    $this->first_name = $first_name;
  }

  /**
   * @param mixed $last_name
   */
  public function setLastName($last_name)
  {
    $this->last_name = $last_name;
  }
}

Upvotes: 1

Robert
Robert

Reputation: 20286

i'm looking for a clean way

I'm trying to make a STD Object

Those sentences are mutually exclusive. If you want to make it clean way then create your own class for that data and populate it ex. using hydrator.

I don't know exactly where you want to save it but you have plenty of options. For example you can serialise an object.

If you want to it with "array way" then you can use this simple function

function checkMandatory(array $mandatoryFields, array $array) {
     return !array_diff_key(array_flip(mandatoryFields), $array);
}

usage

var_dump(checkMandatory(['first_name', 'last_name'], $_POST));

Upvotes: 1

Related Questions