Skip to content

Instantly share code, notes, and snippets.

@erop
Last active Aug 8, 2017
Embed
What would you like to do?
How to let PhpStorm to know more about my static methods?
<?php
abstract class AbstractObject
{
// we use static var to enforce setting up entry point
// in child classes
public static $entryPoint = null;
public static function getEntryPoint()
{
// late static binding let us to get entry point value
// in concrete child class
if (is_null(static::$entryPoint)) {
$className = static::class;
throw new \UnexpectedValueException("You must setup entry point for $className type objects ");
}
return static::$entryPoint;
}
}
<?php
class Address extends AbstractClass
{
// we setup entry point
// which will be used in Service
public static $entryPoint = 'address';
}
<?php
$service = new Service();
// user sends just 1) query string and 2) type/class of objects he want to get response for
$object = $service->getObject('Tour de Eiffel', Address::class);
// next time a user probably would like to get a company data
$google = $service->getObject("Google", Company::class);
<?php
class Service
{
// now I would like a user/client to pass 1) query string and 2) class of objects
// in form Address::class
public function getObject(string $query, string $fqcn)
{
// PhpStorm can find (as it should!) getEntryPoint() method in $template class
// what should i do?
// Looks like adding annotation below solves the problem
/** @var AbstractObject $fqcn */
$response = $this->getResponse($query, $fqcn::getEntryPoint());
// some logic applied here to return to a user needed result;
return $object;
}
private function getResponse($query, $entryPoint)
{
// build full url for API entry point and
// actually asks API
return new stdClass();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment