Skip to content

Instantly share code, notes, and snippets.

<?php
class SpecializedValidator extends AbstractValidator
{
private $somethingToCheck;
public function __construct($somethingToCheck)
{
parent::__construct([$this, 'validate']); // sic!
$this->somethingToCheck = $somethingToCheck;
}

Keybase proof

I hereby claim:

  • I am mikey179 on github.
  • I am mikey179 (https://keybase.io/mikey179) on keybase.
  • I have a public key ASAEbnyVKjsTeNqHonyCxi4UscVjxEf-wMcLX2IlAGOxgAo

To claim this, I am signing this object:

@mikey179
mikey179 / selfbase.sh
Created March 28, 2017 13:11
How to retrieve the full path where a script resides
#!/bin/bash
set -o nounset
set -o errexit
selfbase() {
local selfbase=""
if [ -f $0 -a ! -h $0 ]; then
selfbase="$(dirname $0)"
elif [ $(uname) = "Darwin" ]; then
selfbase="$(dirname $(readlink $0))"
<?php
class MyStreamWrapper {
private $read = false;
public function stream_open($path , $mode , $options , &$opened_path) {
return true;
}
public function stream_stat() {
return [];
<?php
function foo(callable $bar)
{
$bar("nu?\n");
}
foo(function($arg) { echo "Hello world!\n" . $arg;});
function baz($arg)
{
@mikey179
mikey179 / build_tools_and_application_builds.md
Last active December 30, 2015 03:39
Some thoughts on build tools and application builds

If your application requires a build tool, there's something wrong with your application or the runtime environment of the application. For some that might be quite a controversial statement. What do I mean with it? In order to be able to run an application, the list of steps to fulfill should include just three steps. The first step is to get a checkout of the application from your source code management system (or download it to a local folder, this should require a single command line instruction only as well), and the last step is to start the application.

Now there is a single step left only. And this step should not be running a build tool which does hundreds of things in order to get a running application. Your application should be runnable right after checkout, the only thing that is allowed to miss are any dependencies the application is build on. So the second step should be fetching the dependencies. Nothing more. You don't need a build tool for that. A simple dependency manager is just enough to

@mikey179
mikey179 / private_methods.md
Created December 2, 2013 13:28
Some thoughts on private methods

In recent weeks I have come to the conclusion that private methods can be an indicator that a class needs to be refactored. The more lines a private method contains the stronger the indicator. Please be aware that I'm not arguing against private methods or that I consider them harmful, quite the contrary. A private method which encapsulates a condition to be used in another method of the class can be really helpful when it helps to keep the same level of abstraction in this other method.

However, such little methods seldom have more than 1 to 3 lines of code. When your private method starts to have five lines of code or more it becomes an indidator that it does things which shouldn't be done in this class in the first place. It should be done in one of the collaborators the class already has (i.e., one of its dependencies), either in a public method or as part of other operations in one of these collaborators. Alternatively, if it fits into none of those existing collaborators, it could give you a hint that

@mikey179
mikey179 / gist:5520225
Last active December 17, 2015 00:19
Which one is better? Both functions do the same, but in slightly different ways. Interestingly, the second one takes around 4 times longer than the first one.
function convertToStringRepresentation1($value)
{
$string = '';
$lines = explode("\n", (string) $value);
foreach ($lines as $lineCounter => $line) {
if (empty($line)) {
continue;
}
if (0 != $lineCounter) {
@mikey179
mikey179 / with.php
Last active December 11, 2015 00:38
with experiment
<?php
function with($var, \Closure $execute, \Closure $exit = null)
{
try {
$result = $execute($var);
$exit($var);
} catch (\Exception $e) {
if (null == $exit) {
return null;
}
@mikey179
mikey179 / convert.php
Created April 7, 2012 15:25
Convert trac wiki export directory to mediawiki format
<?php
# uses trac2mediawiki perl script from http://www.mediawiki.org/wiki/Extension:TracWiki2MediaWiki#Code
$dirit = new DirectoryIterator(getcwd());
foreach ($dirit as $file) {
if (!$file->isFile() || substr($file->getFilename(), 0 , 1) == '.' || $file->getExtension() == '.md') {
continue;
}