Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell PhpStorm about implementation details? (magic methods)

Tags:

phpstorm

I have an object "User" that has attributes whose accessability is declared as protected but which can be set directly via a magic __set-method.

enter image description here

Now PhpStorm signals this apparent inconsistency with a big red column on the right side.

Is it possible to explain to PhpStorm what is going on so this is not shown as an error any more?


EDIT :

I use PhpStorm 2.1.4

okay here is some code that exemplifies the issue (together with the so far suggested workaround from Alexey which sadly doesn't do it for me):

c.php:

<?php
/**
 * @property mixed $a
 */
class c1
{
    protected $a;

    public function __construct() { $this->a = __CLASS__; }

    public function __get($n) { return $this->{$n}; }
}

/**
 * @property $a mixed
 */
class c2
{
    protected $a;

    public function __construct() { $this->a = __CLASS__; }

    public function __get($n) { return $this->{$n}; }
}

test.php

<?php
require "c.php";

$c1 = new c1();
var_dump($c1->a);

$c2 = new c2();
var_dump($c2->a);

and the output:

string 'c1' (length=2)
string 'c2' (length=2)

and how it looks like in PhpStorm:

enter image description here

my goal:

either having PhpStorm "understand" the design or just getting rid of those annoying red marks everywhere while not impairing the error detection apart from this issue.

like image 658
Raffael Avatar asked Oct 04 '11 09:10

Raffael


1 Answers

This is now working in PHPStorm 3 :)

Unfortunately this is a open request in our tracker, see http://youtrack.jetbrains.net/issue/WI-4468

The only way to avoid this warnings now, is to add @property to $user's class declaration. i.e.

/**
 * @property $name string
 */
class User {
    protected $name; 
}
$user = new User();
$user->name = "me";
like image 135
Alexey Gopachenko Avatar answered Jan 03 '23 11:01

Alexey Gopachenko