Skip to content

Instantly share code, notes, and snippets.

View jesseschalken's full-sized avatar

Jesse Schalken jesseschalken

View GitHub Profile
<?php
// See http://www.unicode.org/versions/corrigendum1.html Table 3.1B. Legal UTF-8 Byte Sequences
$utf8 = "^([\\\x00-\\\x7F]|[\\\xC2-\\\xDF][\\\x80-\\\xBF]|\\\xE0[\\\xA0-\\\xBF][\\\x80-\\\xBF]|[\\\xE1-\\\xEF][\\\x80-\\\xBF][\\\x80-\\\xBF]|\\\xF0[\\\x90-\\\xBF][\\\x80-\\\xBF][\\\x80-\\\xBF]|[\\\xF1-\\\xF3][\\\x80-\\\xBF][\\\x80-\\\xBF][\\\x80-\\\xBF]|\\\xF4[\\\x80-\\\x8F][\\\x80-\\\xBF][\\\x80-\\\xBF])*$";
@jesseschalken
jesseschalken / gist:de2e35d81d317235d289
Last active May 5, 2016 06:18
Trait inlining process
  1. Start with all methods from all used traits.
  2. Resolve conflicting method names using "insteadof" rules.
    • Each trait offering a method may be excluded as the implementation of that method by being mentioned on the right hand side of the "insteadof" rules for the method.
    • A trait cannot be mentioned on the right hand side of the "insteadof" rules for a method more than once in total.
    • Of all the used traits which offer the method, either one or none must remain as the implemenation of the method. If none remain, the method does not exist after this step.
    • The trait name used on the left hand side of an "insteadof" rule has no meaning, but must be one of the used traits which offer the method and must not be mentioned on the right hand side of the same "insteadof" rule.
  3. Add aliases.
    • Qualified aliases use the method implementation from the specified trait.
    • Unqualified aliases use the method implementation from the first used trait that offers it.
  • The method visibility
@jesseschalken
jesseschalken / php-cross-compiler.php
Last active May 26, 2016 02:32
Compile PHP 7.0 to PHP 5.3
<?php
const PHP50 = 50000;
const PHP51 = 50100;
const PHP52 = 50200;
const PHP53 = 50300;
const PHP54 = 50400;
const PHP55 = 50500;
const PHP56 = 50600;
const PHP70 = 70000;
@jesseschalken
jesseschalken / js-classes.md
Last active April 3, 2017 02:02
JavaScript classes

JavaScript Objects, Classes, Functions and Prototypes

Objects

A JavaScript object is a map from keys (or "properties") to values, supporting the usual methods of a map/dictionary (set, get, delete, exists, iterate):

// set property
object.proprty = value;
object['property'] = value;
@jesseschalken
jesseschalken / ts-modules.md
Last active November 24, 2015 08:35
TypeScript modules

The export of a module is effectively a single JavaScript value, usually an object with individual functions, classes, constants etc as properties. The exported value can be set with export = ... or individual things can be exported as properties of an object by putting export before its definition. For example, the following modules are equivelant:

export function foo() {}
export class Blah {}
export = {
 foo: function () {}
@jesseschalken
jesseschalken / FileMode.php
Created November 4, 2015 06:09
Parser/Unparser of Unix file mode
<?php
/**
* @param int $int
* @param int $offset
* @return bool
*/
function get_bit($int, $offset) {
return (bool)((1 << $offset) & $int);
}

My preferred code style is 2-space K&R. This is intended to provide a justification for this style.

Why K&R?

K&R style has the following properties:

  1. Provides symmetric size (in terms of screen space consumed) between the opening and closing syntax of a clode block.
  2. Forces no empty or meaningless lines, thereby avoiding artificial distance between related things that should be together.
  3. Consumes the minimum vertical space while keeping the opening and closing syntax of a block on separate lines from the content.
@jesseschalken
jesseschalken / codeStyleSettings.xml
Created September 24, 2015 09:17
PhpStorm code style settings
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<PHPCodeStyleSettings>
<option name="ALIGN_KEY_VALUE_PAIRS" value="true" />
<option name="ALIGN_PHPDOC_PARAM_NAMES" value="true" />
<option name="ALIGN_PHPDOC_COMMENTS" value="true" />
<option name="ALIGN_ASSIGNMENTS" value="true" />
@jesseschalken
jesseschalken / memory_limit.php
Last active September 11, 2015 01:14
Class to increase/decrease PHP's memory_limit by a given amount
<?php
class MemoryLimit {
private $num = 0;
function inc($num) {
if (!$num)
return;
$this->num += $num;
@jesseschalken
jesseschalken / simple-autoload.php
Last active March 3, 2016 12:49
This autoloader will search for Foo\Bar\Baz (for example) in ./Foo/Bar/Baz.php, ./Foo/Bar.php and ./Foo.php, in that order. This enables developers to place multiple classes and even multiple namespaces in one file when appropriate. Just place this file in your src/ directory and use it as the entry point for your project.
<?php
spl_autoload_register(function ($cls) {
for ($cls = explode('\\', $cls); $cls; array_pop($cls)) {
$sep = DIRECTORY_SEPARATOR;
$php = __DIR__ . $sep . join($sep, $cls) . '.php';
if (file_exists($php)) {
/** @noinspection PhpIncludeInspection */
require_once $php;
break;