Example #1
0
 public function testCompose()
 {
     $c1 = f\compose('md5', 'strtoupper', 'trim', 'strrev');
     $c2 = f\compose('count', 'array_filter');
     $this->assertSame(md5('OLLEH'), $c1(' hello '));
     $this->assertSame(2, $c2(array('1', 2, '3', 4, '5'), 'is_int'));
 }
Example #2
0
function function_doc_info_from_code($code)
{
    $buildDoc = function ($raw, $doc) {
        return array('raw' => $raw, 'usage' => $doc[0], 'desc' => $doc[1], 'example' => $doc[2]);
    };
    $docTemplate = array('', '', '');
    $getRawDoc = function ($contents) {
        if (preg_match('@^/\\*\\*\\n(.*)\\n \\*/\\nfunction.@ms', $contents, $matches)) {
            return preg_replace('/^ \\* ?/m', '', $matches[1]);
        }
        return false;
    };
    $rawDoc = $getRawDoc($code);
    if ($rawDoc === false) {
        return $buildDoc('', $docTemplate);
    }
    $processDoc = f\compose(f\partial('array_replace', $docTemplate), f\partial('explode', "\n\n", f\_(), 3));
    $doc = $processDoc($rawDoc);
    return $buildDoc($rawDoc, $doc);
}
Example #3
0
function benchmark()
{
    $testTime = function ($function) {
        $start = microtime(true);
        $function();
        $time = microtime(true) - $start;
        return $time;
    };
    $testCompare = function ($functions) use($testTime) {
        $times = f\map($testTime, $functions);
        $faster = min($times);
        $percentage = function ($time) use($faster) {
            return ['time' => $time, 'percentage' => $time / $faster];
        };
        $print = function ($time, $name) {
            return sprintf("%20s: %10f | %10f\n", $name, $time['time'], $time['percentage']);
        };
        f\map(f\compose('printf', $print), f\map($percentage, $times));
        echo "\n";
    };
    f\each($testCompare, benchmark_compares());
}
Example #4
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Finder\Finder;
use felpado as f;
$src = __DIR__ . '/../src';
$finder = Finder::create()->name('*.php')->name('*.xml')->in($src);
$fixUses = f\partial('preg_replace', '/^(use Akamon\\\\OAuth2\\\\Server)(?:.+);$/m', '');
$fixNamespace = f\partial('preg_replace', '/(namespace Akamon\\\\OAuth2\\\\Server)(.+);/', '$1;');
$fixFullyQualifiedClasses = f\partial('preg_replace', '/(\\\\?Akamon\\\\OAuth2\\\\Server)(?:\\\\(\\w+))+(?!;)/m', '$1\\\\$2');
$fixTripleEof = function ($content) use(&$fixTripleEof) {
    $search = "/\n\n\n/m";
    $replace = f\partial('preg_replace', $search, "\n\n");
    return preg_match($search, $content) ? $fixTripleEof($replace($content)) : $content;
};
$fix = f\compose($fixTripleEof, $fixFullyQualifiedClasses, $fixUses, $fixNamespace);
$byePsr0 = function ($file) use($fix) {
    $newContent = $fix(file_get_contents($file));
    file_put_contents($file, $newContent);
};
f\each($byePsr0, $finder);