Skip to content

Commit

Permalink
Merge pull request #1 from preprocess/develop
Browse files Browse the repository at this point in the history
The great refactoring!
  • Loading branch information
assertchris authored May 13, 2017
2 parents b8278fe + 0c006ad commit 087a8c1
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 80 deletions.
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"type": "pre-macro",
"name": "pre/short-closures",
"license": "MIT",
"require": {
"pre/plugin": "^0.6"
},
"autoload": {
"files": [
"src/bootstrap.php"
]
"pre/plugin": "^0.7"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
"phpunit/phpunit": "^5.0|^6.0"
},
"autoload-dev": {
"psr-4": {
"Pre\\ShortClosures\\": "tests"
}
},
"extra": {
"macros": [
"src/macros.yay"
]
}
}
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
convertWarningsToExceptions="false"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
syntaxCheck="false"
>
<testsuites>
<testsuite>
<directory suffix="Test.php">tests</directory>
Expand Down
6 changes: 0 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# Pre Short Closures

Documentation can be found at [preprocess.io](https://preprocess.io/docs#short-closures).

## Versioning

This library follows [Semver](http://semver.org). According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library.

All methods, with `public` visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep `protected` methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading.
3 changes: 0 additions & 3 deletions src/bootstrap.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/macros.pre

This file was deleted.

94 changes: 94 additions & 0 deletions src/macros.yay
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

macro ·recursion {
·chain(
·token("("),
·optional(
·ls(
·chain(
·optional(·ns())·argType,
·token(T_VARIABLE)·argName,
·optional(
·chain(
·token("="),
·either(
·ns(),
·token(T_CONSTANT_ENCAPSED_STRING),
·token(T_LNUMBER),
·token(T_DNUMBER),
·token(T_STRING)
)
)
)·argAssignment
)·arg,
·token(",")
)
)·args,
·token(")"),
·optional(
·chain(
·token(":"),
·ns()
)
)·returnType,
·token(T_DOUBLE_ARROW),
·token("{"),
·layer()·body,
·token("}"),
·_()·scope,
·_()·simple
)
} >> function($ast) {
$defined = [];

foreach ($ast->{"·args"} as $node) {
$name = (string) $node["·arg"]["·argName"];
$defined[$name] = true;
}

$bound = false;
$scope = new \Yay\Ast("·scope");

$pushed = [];

foreach ($ast->{"·body"} as $token) {
$name = $token->value();

if (!$token->is(T_VARIABLE)) {
continue;
}

if (isset($defined[$name]) || isset($pushed[$name])) {
continue;
}

if (substr($name, 1) === "this") {
continue;
}

$scope->push(new \Yay\Ast("·var", $token));
$pushed[$name] = true;
$bound = true;
}

if ($bound) {
$ast->append($scope);
} else {
$simple = new \Yay\Ast("·simple");
$simple->push(new \Yay\Ast());

$ast->append($simple);
}
} >> {··trim(
·scope ?·{
[·scope ···(, ) { ·var = ·var ?? null}, "fn" => function (·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignment } }) use (·scope ···(, ) { &·var }) ·returnType {
·body
}]["fn"]
}

·simple ?·{
function (·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignment } }) ·returnType {
·body
}
}
)}
2 changes: 1 addition & 1 deletion tests/SpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Pre\ShortClosures;

use Pre\Testing\Runner;
use Pre\Plugin\Testing\Runner;

class SpecTest extends Runner
{
Expand Down
11 changes: 5 additions & 6 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Yay;

// let's use a namespace trick, to make non-colliding variables predictable.

function md5($value)
{
function md5($value) {
return $value;
}

putenv("PRE_BASE_DIR=" . __DIR__ . "/..");

require __DIR__ . "/../vendor/autoload.php";

putenv("PRE_BASE_DIR=" . __DIR__ . "/../");

\Pre\Plugin\addMacroPath(__DIR__ . "/../src/macros.yay");
14 changes: 0 additions & 14 deletions tests/specs/async.spec

This file was deleted.

43 changes: 28 additions & 15 deletions tests/specs/recursion.spec
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@ $cb = () => {
};
};

$foo = "hello";
$bar = "world";

$cb = () => {
return () => {
return () => {
print $foo . $bar;
};
};
};

--EXPECT--

$cb = call_user_func(function ($context·0) {
return function () use ($context·0) {
extract($context·0);
return call_user_func(function ($context·1) {
return function () use ($context·1) {
extract($context·1);
return call_user_func(function ($context·2) {
return function () use ($context·2) {
extract($context·2);
return "hello world";
};
}, get_defined_vars());
};
}, get_defined_vars());
$cb = function () {
return function () {
return function () {
return "hello world";
};
};
}, get_defined_vars());
};

$foo = "hello";
$bar = "world";

$cb = [$foo = $foo ?? null, $bar = $bar ?? null, "fn" => function () use (&$foo, &$bar) {
return [$foo = $foo ?? null, $bar = $bar ?? null, "fn" => function () use (&$foo, &$bar) {
return [$foo = $foo ?? null, $bar = $bar ?? null, "fn" => function () use (&$foo, &$bar) {
print $foo . $bar;
}]["fn"];
}]["fn"];
}]["fn"];
17 changes: 8 additions & 9 deletions tests/specs/short-closures.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ Test short closure macros

class Fixture
{
public function foo($end) {
public function foo($end, $thing) {
return ($name) => {
return "hello {$name}{$end}";
$this->something();
return "hello {$name}{$end}{$thing}";
};
}
}
Expand All @@ -17,13 +18,11 @@ class Fixture

class Fixture
{
public function foo($end)
public function foo($end, $thing)
{
return call_user_func(function ($context·0) {
return function ($name) use ($context·0) {
extract($context·0);
return "hello {$name}{$end}";
};
}, get_defined_vars());
return [$end = $end ?? null, $thing = $thing ?? null, "fn" => function ($name) use (&$end, &$thing) {
$this->something();
return "hello {$name}{$end}{$thing}";
}]["fn"];
}
}

0 comments on commit 087a8c1

Please sign in to comment.