Skip to content

Instantly share code, notes, and snippets.

@ryangjchandler
Created September 10, 2023 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryangjchandler/6d2e864112732b97508b3fc918da0fbd to your computer and use it in GitHub Desktop.
Save ryangjchandler/6d2e864112732b97508b3fc918da0fbd to your computer and use it in GitHub Desktop.
Syntax Highlighting Kitchen Sink
<?php
namespace Sample;
class SampleClass implements SampleInterface
{
public string $public;
protected SampleClass $protected;
private SampleClass $private;
public static string $static;
public function __construct()
{
//
}
public function sampleSample()
{
}
final public static function sample(string $sample): void
{
}
}
interface SampleInterface
{
public function __construct()
{
//
}
}
enum SampleEnum: int
{
case Sample = 0;
}
function sample()
{
}
if (true) {
} elseif (false) {
} else {
}
foreach ([] as $foo => $bar) {
}
for ($i = 0; $i < 10; $i += 1) {
}
while (true) {
}
do {
} while (true);
$numbers = [1, 2, 3, 4, 5];
$string = 'Hello!';
$true = true;
$false = false;
$null = null;
const SAMPLE = 'sample';
sample();
match (true) {
sample() => null,
default => SAMPLE,
};
switch (sample()) {
case SAMPLE:
break;
case sample():
break;
default:
break;
}
$string = '<span></span>';
$object = new SampleClass();
$object->public;
$object->sampleSample();
SampleClass::$static;
SampleClass::sample(sample: SAMPLE);
?>
<h1>
Hello, world!
</h1>
@deleugpn
Copy link

deleugpn commented Sep 10, 2023

function items(): \Generator
{
    yield 5;
    yield 6;

    yield from moreItems();
}

namespace Testing {
    use Package\{Foo, Bar, Baz};

    use function round;

    final readonly class Summer extends Season implements Sun, Heat
    {
        use HasTemperature;

        public function __construct(private Foo $foo, protected readonly Bar $bar) {}

        public function types(Foo|Bar $union, Foo&Bar $intersection, ?Foo, (Foo|Bar)&Baz $disjunction): ?Result
        {
            require 'blah.php';
            require_once 'blah.php';
            include 'blah.php';
            include_once 'blah.php';

            echo "output" . "concatenation" . "$interpolation", 'single-quote';

            $heredoc = <<<SQL
            SELECT FROM ...
            SQL;
        }
    }
}

@Wulfheart
Copy link

Wulfheart commented Sep 10, 2023

Missing the global keyword and $var::class.

@MrPunyapal
Copy link

MrPunyapal commented Sep 11, 2023

trait HasSomething{
   use CanDoSoemthing, HasNothing;
}

@jeroenvanrensen
Copy link

Hello, I saw your tweet and here's a file from a project I'm currently working on. It's written in Typescript.

import Type from './type'
const asciitree = require('ascii-tree')

export default class Node {
    type: Type
    value: string | number
    children: Node[]
    parent: Node | null

    constructor(type: Type, value: string | number) {
        this.type = type
        this.value = type === Type.Number ? Number(value) : value
        this.children = []
        this.parent = null
    }

    setParent(parent: Node | null) {
        this.parent = parent
    }

    addChild(child: Node) {
        this.children.push(child)

        child.setParent(this)

        return child
    }

    replaceChild(search: Node, replace: Node) {
        let index = this.children.indexOf(search)
        this.children[index] = replace

        search.setParent(null)
        replace.setParent(this)
    }

    toString(indent: number = 1) {
        let string =
            '#'.repeat(indent) +
            this.value.toString() +
            (this.parent ? ` [${this.parent.value.toString()}]` : ' [null]') +
            '\r\n'

        this.children.forEach(child => (string += child.toString(indent + 1)))

        return indent === 1 ? asciitree.generate(string) : string
    }

    root(): Node {
        return this.parent === null ? this : this.parent.root()
    }

    insertBetween(parent: Node, child: Node) {
        parent.replaceChild(child, this)

        this.addChild(child)
    }

    setChildren(children: Node[]) {
        this.children = children
    }

    clone(): Node {
        let node = new Node(this.type, this.value)

        this.children.forEach(child => node.addChild(child.clone()))

        return node
    }

    equals(node: Node): boolean {
        if (!node) return false

        if (this.type !== node.type || this.value !== node.value) return false

        return (
            !this.children.map((child, index) => child.equals(node.children[index])).includes(false) &&
            !node.children.map((child, index) => child.equals(this.children[index])).includes(false)
        )
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment