PhpStorm autocomplete STD Object not working

intellij code completion not working
intellij custom autocomplete
intellij code completion plugin
pycharm code completion not working
intellij groovy code completion
intellij react code completion not working
intellij super completion
codota not working in intellij

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->

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.

PhpStorm autocomplete STD Object not working, 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  I have the same problem with auto-completion not working (Windows PHPStorm 2016.2.2). It doesn't seem to know how to complete any general logic statement (if, for, while, switch, etc.). Also, I have the same IDE on Mac, and auto-complete works as expected.

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));

stdclass with autocompletion – IDEs Support (IntelliJ Platform , Based on your code example: 1) Generally speaking -- why do use intermediate variable and not working with class variable directly? This way  I found the problem. in PHP files, if you use style tag in current page, CSS class autocomplete fails. you should define your stylesheet into a CSS file then load it to your page. of course it is a bug, but you can fix it.

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;
  }
}

How to declare a type of an object within an , How do I declare an object that is property of another object without reasigning it​? $this->owhatever-> will not work for code completion PHPStorm won't show any autocompletion, nothing about properties overwritten or inherited, and so forth Deleting the Model part from _ide_helper.php solves the problem for me so far. I didn't experience this behaviour in laravel-ide-helper in combination with Laravel 5.1 or 5.2 using an identical setup like the project in question here. PHPStorm does complain about "multiple definitions" in the latter cases as well, but still autocompletion and all the other inspections continue to work.

Code completion - Help, Configure: Settings/Preferences | Editor | General | Code Completion However, IntelliJ IDEA does not recognize the structure of such files, and suggests options After the new keyword in an object declaration that are defined in your project, in the libraries you are using, and in standard APIs. Troubleshooting. Based on your code example: 1) Generally speaking -- why do use intermediate variable and not working with class variable directly? This way you just making IDE's life harder (you are describing structure of some local variable.. but then (in another method) working with completely different class variable (from IDE point of view)).

PHP autocomplete not working – IDEs Support (IntelliJ Platform , Using PHPStorm 10.0.3 and my PHP code completion has stopped working. For exmaple, if I start typing out a `foreach()` loop, nothing Basic code completion helps you complete the names of classes, methods, and keywords within the visibility scope. When you invoke code completion, PhpStorm analyses the context and suggests the choices that are reachable from the current caret position (suggestions also include Live templates).

Code Inspections in PHP - Help, This can lead to issues with code completion, members resolution, and class inheritance The inspection skips the objects of the stdClass or derived types. There was a version of phpStorm where i was able to use namespace autocomplete in phpdoc (similar to usual autocomplete in code), i.e. i typed ' /** @return ActiveReco' and autocomplete popup appears allowing me to either add whole class name with namespace or add 'use ' at the top of the file.

Comments
  • are you asking how to validate the incoming data? It's not really clear what you want to achieve.
  • @ADyson Updated the post, put the "main question" in bold.
  • Right so it's a question about the IDE. But why don't you just declare an object / class to begin with instead of an array? I would guess it doesn't autocomplete because it's not based on some other hard-coded compilable code. instead you're creating an object by executing some code instead of writing it - a compiler won't be able to know the outcome of the execution (even if in this case it's predictable).
  • Because i wanna loop truth the array, so i dont have to if(isset) every thing time, So i can just do foreach($mandatory as $key => $value){ if(isset($value)){return true}}
  • Not unless you declare it as a concrete class to begin with. BTW you can perfectly easily loop over the properties of a class, so there isn't really any need to start off with an array. php.net/manual/en/language.oop5.iterations.php
  • Not sure why's he supposed to extend from the \stdClass. This should be a POPO.
  • @emix To make the object as close as possible as the actual type. But I don't have PhpStorm here to verify, perhaps it doesn't display any useful information from that.
  • BTW, you made me google for POPO and my PC is configured for Spanish... xD
  • LOL you made my day.
  • Thanks! I love the idea. But i also wanna be able to use the variables later on. So thats why i wanna declare a array, so can i even use this: $mandatory->first_name = stripslashes($mandatory->first_name); $mandatory->last_name = stripslashes($mandatory->last_name); $mandatory->first_name = htmlspecialchars($mandatory->first_name); $mandatory->last_name = htmlspecialchars($mandatory->last_name); This works! But it is not auto completing..
  • then don't use stdClass but create an class and populate it from vars comming from $_POST
  • Yhea great idea man! But why use setters? You can just do $formFields::last_name right?
  • Its much cleaner to use getters and setters
  • Not trying to amend a perfectly fine answer, but setters/getters are often recommended as a way to provide further encapsulation. Since external code does not have direct access to public properties it's trivial to change the internal implementation, add validations, etc. In practice, 99% of the times they end up being nothing but empty boilerplate ;-)