technergy
technergy

Reputation: 254

PHP: Type hinting for one method to declare a few object variables as return

I'm working with the PHPStorm IDE. I have a method which fetches a few columns from my mysql db and returns an object which contains these values. Is there a possibility to write a PHPDoc comment, which is type hinting the specific values? So that if I type

self::getCommissionFromCacheOrDb($provider_id, $type, $platform)->

The IDE will show me a few variables, which I've declared in the PHPDoc comment above the method.

f.e. the method.

public static getValuesFromDb($provider_id){
            $data = self::find()->where(['provider_id' => $provider_id,
                    'revision' => '0000-00-00 00:00:00',
                    'platform' => $platform,
                    'type' => $type])
                ->select(['commission',
                        'direct_commission',
                        'super_commission',
                        'super_commission_maturity',
                        'valid_from',
                        'valid_to'])
                ->one();
            return $data;
}

I could imageine some PHPDoc block like

/**
 * @return $direct_commission
 * @return @super_commission
 * @return @super_commission
 * @return @valid_from
 * @return @valid_to
 */

but unfortunately this doesn't work. Is it realy a must to write a getter and setter for each variable or can this be done by any PHPDoc way?

Any help would be appreciated.

Thanks

Upvotes: 1

Views: 429

Answers (2)

Álvaro González
Álvaro González

Reputation: 146660

Basic rules are:

  • Only one @return statement
  • No variable name
  • Data type as-is (no use @ prefix)

Said that, there's not much you can do if your object is created ad-hoc. You'll possibly need to create an empty class just for code completion. e.g.:

class Comission{
    public $commission;
    public $direct_commission;
    public $super_commission;
    public $super_commission_maturity;
    public $valid_from;
    public $valid_to;
}

... and:

/**
 * @return Comission
 */

PhpStorm itself uses this trick quite a lot to document built-in classes and functions:

/**
 * Representation of date and time.
 * @link http://php.net/manual/en/class.datetime.php
 */
class DateTime implements DateTimeInterface {
    const ATOM = 'Y-m-d\TH:i:sP';
    const COOKIE = 'l, d-M-y H:i:s T';
...

Upvotes: 2

Marek
Marek

Reputation: 7423

I'm not sure about PHPStorm, but Netbeans accepts pipe sign:

/**
 * 
 * @return A|B
 */

Upvotes: 0

Related Questions