Skip to content

Instantly share code, notes, and snippets.

Avatar

Charles Sprayberry cspray

View GitHub Profile
@cspray
cspray / class-string-rfc.md
Last active May 31, 2022
RFC: Introduce a class-string type
View class-string-rfc.md

RFC: Introduce a class-string type

  • Version: 0.3
  • Last Substantial Updates: 2022-05-04
  • Target Version: PHP 8.next

Introduction

There are many scenarios in which userland code might need to pass parameters to a method or assign a value to a property that is intended to represent a class. Currently the only way to type-hint that a provided value represents a fully-qualified class-name (FQCN) is through userland code. This RFC proposes adding a new internal type that would verify provided values are FQCN.

View inject-secrets.php
<?php
use Cspray\AnnotatedContainer\Attribute\Service;
use Cspray\AnnotatedContainer\Attribute\Inject;
use Cspray\AnnotatedContainer\ParameterStore;
use Cspray\Typiphy\Type;
class MySecretParameterStore implements ParameterStore {
public function getName() : string {
@cspray
cspray / callable-interface.php
Last active Apr 1, 2022
A spike for having CallableInterfaces in PHP
View callable-interface.php
<?php
// In PHP internals, maybe this is an abstract class instead?
// Maybe an attribute?
#[Attribute]
class FunctionalInterface {}
#[FunctionalInterface]
interface MyAction {
View enum-compass-values.php
<?php
namespace YourNamespace\Enums;
use Cspray\Yape\Enum;
use Cspray\Yape\EnumTrait;
final class Compass implements Enum {
use EnumTrait;
View enum-example.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use YourNamespace\Enums\Compass;
$north = Compass::North();
$south = Compass::South();
$east = Compass::East();
$west = Compass::West();
View keybase.md

Keybase proof

I hereby claim:

  • I am cspray on github.
  • I am cspray (https://keybase.io/cspray) on keybase.
  • I have a public key ASDgKvQd1ACmkJh6ME2WlXVdnigsEpwUUTk4BZF5Xbbjrwo

To claim this, I am signing this object:

@cspray
cspray / EchoCommand.php
Last active Sep 22, 2019
cspray/websocket-commands Quick Start
View EchoCommand.php
<?php
namespace MyApp\Command;
use Amp\Promise;
use Amp\Websocket\Client;
use Cspray\WebsocketCommands\ClientPayload;
use Cspray\WebsocketCommands\WebsocketCommand;
use function Amp\Promise\call;
View expecting-exception-async.txt
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
E.F 3 / 3 (100%)
Time: 132 ms, Memory: 4.00MB
There was 1 error:
1) Amp\PHPUnit\Test\AsyncTestCaseTest::testThatMethodRunsInLoopContext
Error: Loop exceptionally stopped without resolving the promise
@cspray
cspray / aysnc-test-example.php
Created Feb 9, 2019
AsyncTestCase example with amphp
View aysnc-test-example.php
<?php
// We assume that your PHPUnit bootstrap will include the appropriate class loader and setup your environment
use Amp\Delayed;
use Amp\Promise;
use Amp\PHPUnit\AsyncTestCase;
use function Amp\call;
View 01-async-test-case.php
<?php
abstract class AsyncTestCase extends \PHPUnit\Framework\TestCase {
public function runTest() {
Loop::run(function() {
$watcherId = Loop::delay(1500, function() {
Loop::stop();
$this->fail('The asynchronous test was expected to finish before the 1500 millisecond time limit');
});