Last active
August 8, 2017 07:40
-
-
Save erop/e2876a075035a8eef598afcd58f7bbe4 to your computer and use it in GitHub Desktop.
How to let PhpStorm to know more about my static methods?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Address extends AbstractClass | |
{ | |
// we setup entry point | |
// which will be used in Service | |
public static $entryPoint = 'address'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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