Skip to content

Instantly share code, notes, and snippets.

View stefanofago73's full-sized avatar
💭
Software Designer, I like to build software, a little tormented soul

Stefano Fago stefanofago73

💭
Software Designer, I like to build software, a little tormented soul
View GitHub Profile
@stefanofago73
stefanofago73 / RandomEnumGenerator.php
Created May 16, 2022 20:15
Example of Usage of UnitEnum, Template and Callable to obtain a "generic function"...
<?php declare(strict_types = 1);
/**
* @template T of \UnitEnum
* @param class-string<T> $enumType
* @return callable():T
**/
function randomEnumGenerator(string $enumType):callable
{
/** @var array<T> $cases **/ $cases = [$enumType,"cases"]();
<?php declare(strict_types = 1);
// ====================================================================
//
// Copy and Paste on https://3v4l.org/
// PHPStan & Psalm verified...
// Some code isn't the best but I want to transmit the ideas!
//
// ====================================================================
//====================================
//
// MAIN TYPE
//
//====================================
public interface ComposingAutocloseable extends AutoCloseable
{
<?php declare(strict_types = 1);
class Logger
{
function info(string $msg):void{}
}
$logger = new Logger();
/**
<?php declare(strict_types = 1);
//-------------------------------------------
//
// FIRST NAMESPACES
//
//-------------------------------------------
namespace FST{
import static it.fago.loggerstein.Loggerstein.$;
import static it.fago.loggerstein.Loggerstein.lazy;
import static it.fago.loggerstein.Loggerstein.logWith;
import static it.fago.loggerstein.Loggerstein.STD_ERR;
import static it.fago.loggerstein.Loggerstein.STD_OUT;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.*;
import java.util.List;
import static java.util.stream.Collectors.toList;
class Command {
}
interface CommandHandler {
public Result handle(Command cmd);
}
<?php declare(strict_types = 1);
/**
* NOTE:
*
* This example is for that legacy code where it's possible to
* refactor a sequence of action using a custom DSL.
* When not possible to refactor to FP or before using Try-Monad we can
* simulate something like a functor...
<?php declare(strict_types = 1);
/**
* NOTE:
*
* This example is for that legacy code where more error conditions
* are in sequence... Using exceptions, we are creating a control flow
* policy where every exception is an exit from actual flow.
* When not possible to refactor to FP or before using Try-Monad we can
* simulate monoid-al behaviour with an accumulator...
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
public interface Initializer<R> {