1. Go to this page and download the library: Download temirkhann/generic library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
temirkhann / generic example snippets
function addComment(...): Comment|Error {}
$result = addComment();
if ($result instanceof Error) {
// showError
} else {
// showComment
}
declare(strict_types=1);
use TemirkhanN\Generic\Result;
use TemirkhanN\Generic\ResultInterface;
// Just a data structure. Don't pay much attention here
class WishPromise
{
public string $wishName;
public \DateTime $willBeFulfilledAt;
public function __construct(string $wishName, \DateTime $willBeFulfilledAt)
{
$this->wishName = $wishName;
$this->willBeFulfilledAt = $willBeFulfilledAt;
}
}
class WishMaker
{
/**
* This one is actually not truly honest: error means that there is no data.
* Still, it is fully valid - attempt to get it will end up with exception.
* Result should always be checked for `isSuccessful` state.
*
* @return ResultInterface<WishPromise>
*/
public function pleaseAddNativeGenerics(): ResultInterface
{
return Result::error(
Error::create('We have much more important things to do. Named parameters, breaking Liskov and stuff.')
);
}
}
class Highlighter
{
public function grantFirstWish(): void
{
$service = new WishMaker();
$result = $service->pleaseAddNativeGenerics();
if ($result->isSuccessful()) {
// This is it. The data is generic yet PHPStorm and PHPStan know what particular type it is
$fulfillmentDate = $result->getData()->willBeFulfilledAt;
printf('OMG! Santa will add this at %s!', $fulfillmentDate->format('d-m-Y'));
} else {
print('Well, Santa can not do everything instead of you. Put some effort by yourself.');
}
}
}
// Just a data structure. Don't pay much attention here
class WishPromise
{
public string $wishName;
public \DateTime $willBeFulfilledAt;
public function __construct(string $wishName, \DateTime $willBeFulfilledAt)
{
$this->wishName = $wishName;
$this->willBeFulfilledAt = $willBeFulfilledAt;
}
}
class WishMaker
{
/**
* @return CollectionInterface<WishPromise>
*/
public function showMeSomeGoodWishes(): CollectionInterface
{
return new Collection([
new WishPromise('Make PHP mature and awesome', new \DateTime('03 December 2015')),
new WishPromise('Please, we need typed properties', new \DateTime('28 November 2019')),
new WishPromise('Enums are essential. We need them', new \DateTime('25 November 2021')),
]);
}
}
class Highlighter
{
public function showSomeHappyStories(): void
{
$service = new WishMaker();
$goodWishesThatWereFulfilled = $service->showMeSomeGoodWishes();
// Both PHPStorm and PHPStan know that items are WishPromise instances
// No need for inline manual highlighting on each call
foreach ($goodWishesThatWereFulfilled as $fulfilledWish) {
printf(
'%s was fulfilled on %s' . PHP_EOL,
$fulfilledWish->wishName,
$fulfilledWish->willBeFulfilledAt->format('d-m-Y')
);
}
}
}
class WishMaker
{
/**
* @return ResultInterface<WishPromise>
*/
public function pleaseAddNativeGenerics(): ResultInterface
{
// Generic can not determine the data type, so it will not match ResultInterface<WishPromise>
return Result::error(Error::create('Nope'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.