Skip to content

Instantly share code, notes, and snippets.

View ezzatron's full-sized avatar

Erin ezzatron

View GitHub Profile
@ezzatron
ezzatron / StringTest.php
Created April 30, 2011 04:16
Situation where programmatic @Covers could be used in PHPUnit
<?php
namespace Typhoon\Type;
class StringTest extends \Typhoon\Test\TypeTestCase
{
protected function typeFixture()
{
return new String;
}
@ezzatron
ezzatron / ExceptionTest.php
Created May 3, 2011 03:33
Example of using a test fixture method in a PHPUnit test case base class
<?php
namespace Typhoon\Exception;
use Typhoon\Primitive\String;
use Typhoon\Test\ExceptionTestCase;
class ExceptionTest extends ExceptionTestCase
{
/**
@ezzatron
ezzatron / After.php
Created May 9, 2011 09:51
Scisr clean-use
<?php
namespace Foo;
use Bar\Baz\Qux;
use Bar\Baz\Splat;
class Donk
{
public function foo(Qux $qux) {}
@ezzatron
ezzatron / clean-use.php
Created May 9, 2011 12:10
Script to clean unnecessary PHP use statements
#!/usr/bin/env php
<?php
$paths = array();
if (isset($_SERVER['argv']))
{
$paths = $_SERVER['argv'];
array_shift($paths);
if (!$paths)
@ezzatron
ezzatron / Boolean.php
Created August 15, 2011 05:42
Difficult situation: mocking immutable PHP objects with final constructors.
<?php
// Example of how the Primitive class is extended
class Boolean extends Primitive
{
public function assert($value)
{
if (!is_bool($value)) throw new InvalidArgumentException;
}
@ezzatron
ezzatron / LCSTest.php
Created September 5, 2011 02:02
Longest common subsequence for PHP using arrays
<?php
class LCSTest extends PHPUnit_Framework_TestCase
{
/**
* Returns the longest common subsequence of two arrays.
*
* @link http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*
* @param array $left The first array.
@ezzatron
ezzatron / num-cpus.php
Created October 28, 2011 03:53
Function to detect number of CPUs in PHP
<?php
/**
* Copyright © 2011 Erin Millard
*/
/**
* Returns the number of available CPU cores
*
* Should work for Linux, Windows, Mac & BSD
@ezzatron
ezzatron / gist:1547377
Created January 1, 2012 13:50
Situation where serialized attributes might be necessary in Typhoon
<?php
/**
* My awesome function that does amazing things
*/
function doSomeAwesomeStuff($foo)
{
// creating a custom type for the foo attribute
// passing some special matcher object which does the validation work
$fooType = new CustomType(array(
@ezzatron
ezzatron / sudoku.html
Created January 27, 2012 09:35
Nice Sudoku layout
<!DOCTYPE html>
<html>
<head>
<title>Sudoku layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
/* change font size to change scale */
.sudoku-grid {
font-size: 24px;
@ezzatron
ezzatron / gist:2004537
Created March 9, 2012 01:36
Snippet showing basic form validation, entities, persistence etc.
<?php
// VALIDATORS
/**
* Stores validation error messages.
*/
class ErrorData
{
/**