Skip to content

Commit 85ba40e

Browse files
authored
Merge pull request #7 from preprocess/refactor-for-new-plugin
Refactor for new plugin
2 parents d102750 + 6b8634b commit 85ba40e

File tree

10 files changed

+146
-156
lines changed

10 files changed

+146
-156
lines changed

composer.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@
33
"name": "pre/class-accessors",
44
"license": "MIT",
55
"require": {
6-
"pre/plugin": "^0.7.3"
6+
"pre/plugin": "^0.10.0"
77
},
88
"autoload": {
9-
"files": [
10-
"src/expanders.php"
11-
],
9+
"files": ["source/expanders.php"],
1210
"psr-4": {
13-
"Pre\\ClassAccessors\\": "src"
11+
"Pre\\ClassAccessors\\": "source"
1412
}
1513
},
1614
"require-dev": {
1715
"phpunit/phpunit": "^5.0|^6.0"
1816
},
19-
"autoload-dev": {
20-
"psr-4": {
21-
"Pre\\ClassAccessors\\": "tests"
22-
}
23-
},
2417
"extra": {
25-
"macros": [
26-
"src/macros.yay"
27-
]
18+
"macros": ["source/macros.yay"]
2819
}
2920
}

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit
33
backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="tests/bootstrap.php"
5+
bootstrap="vendor/autoload.php"
66
colors="true"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
File renamed without changes.

src/macros.yay renamed to source/macros.yay

Lines changed: 90 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -81,94 +81,96 @@ macro ·unsafe {
8181
·optional(·token(";"))
8282
)
8383
} >> {
84-
·visibility ·name;
85-
86-
·accessors ··· {
87-
·simpleGetter ?· {··trim(
88-
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {
89-
return $this->··unvar(·name);
90-
}
91-
)}
92-
93-
·getter ?· {··trim(
94-
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {
95-
··trim(·getterBody)
96-
}
97-
)}
98-
99-
·simpleSetter ?· {··trim(
100-
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {
101-
$this->··unvar(·name) = $value;
102-
return $this;
103-
}
104-
)}
105-
106-
·setter ?· {··trim(
107-
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {
108-
··trim(·setterBody)
109-
}
110-
)}
111-
112-
·immutableSimpleSetter ?· {··trim(
113-
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {
114-
$clone = clone($this);
115-
$clone->··unvar(·name) = $value;
116-
return $clone;
117-
}
118-
)}
119-
120-
·immutableSetter ?· {··trim(
121-
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {
122-
$clone = clone $this;
123-
124-
$bound = \Closure::bind(function() use ($value) {
125-
··trim(·immutableSetterBody)
126-
}, $clone);
127-
128-
$bound();
129-
130-
return $clone;
131-
}
132-
)}
133-
134-
·simpleUnsetter ?· {··trim(
135-
public function ··concat(unset ··studly(··unvar(·name)))() {
136-
unset($this->··unvar(·name));
137-
return $this;
138-
}
139-
)}
140-
141-
·unsetter ?· {··trim(
142-
public function ··concat(unset ··studly(··unvar(·name)))() {
143-
··trim(·unsetterBody)
144-
}
145-
)}
146-
147-
·immutableSimpleUnsetter ?· {··trim(
148-
public function ··concat(without ··studly(··unvar(·name)))() {
149-
$clone = clone($this);
150-
unset($clone->··unvar(·name));
151-
return $clone;
152-
}
153-
)}
154-
155-
·immutableUnsetter ?· {··trim(
156-
public function ··concat(without ··studly(··unvar(·name)))() {
157-
$clone = clone $this;
158-
159-
$bound = \Closure::bind(function () {
160-
··trim(·immutableUnsetterBody)
161-
}, $clone);
162-
163-
$bound();
164-
165-
return $clone;
166-
}
167-
)}
168-
}
84+
··collapse(··trim(
85+
·visibility ·name;
86+
87+
·accessors ··· {
88+
·simpleGetter ?· {··trim(
89+
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {
90+
return $this->··unvar(·name);
91+
}
92+
)}
93+
94+
·getter ?· {··trim(
95+
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {
96+
··trim(·getterBody)
97+
}
98+
)}
99+
100+
·simpleSetter ?· {··trim(
101+
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {
102+
$this->··unvar(·name) = $value;
103+
return $this;
104+
}
105+
)}
106+
107+
·setter ?· {··trim(
108+
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {
109+
··trim(·setterBody)
110+
}
111+
)}
112+
113+
·immutableSimpleSetter ?· {··trim(
114+
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {
115+
$clone = clone($this);
116+
$clone->··unvar(·name) = $value;
117+
return $clone;
118+
}
119+
)}
120+
121+
·immutableSetter ?· {··trim(
122+
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {
123+
$clone = clone $this;
124+
125+
$bound = \Closure::bind(function() use ($value) {
126+
··trim(·immutableSetterBody)
127+
}, $clone);
128+
129+
$bound();
130+
131+
return $clone;
132+
}
133+
)}
134+
135+
·simpleUnsetter ?· {··trim(
136+
public function ··concat(unset ··studly(··unvar(·name)))() {
137+
unset($this->··unvar(·name));
138+
return $this;
139+
}
140+
)}
141+
142+
·unsetter ?· {··trim(
143+
public function ··concat(unset ··studly(··unvar(·name)))() {
144+
··trim(·unsetterBody)
145+
}
146+
)}
147+
148+
·immutableSimpleUnsetter ?· {··trim(
149+
public function ··concat(without ··studly(··unvar(·name)))() {
150+
$clone = clone($this);
151+
unset($clone->··unvar(·name));
152+
return $clone;
153+
}
154+
)}
155+
156+
·immutableUnsetter ?· {··trim(
157+
public function ··concat(without ··studly(··unvar(·name)))() {
158+
$clone = clone $this;
159+
160+
$bound = \Closure::bind(function () {
161+
··trim(·immutableUnsetterBody)
162+
}, $clone);
163+
164+
$bound();
165+
166+
return $clone;
167+
}
168+
)}
169+
}
170+
))
169171
}
170172

171-
macro {
173+
macro ·unsafe {
172174
function __get(···parameters) {
173175
accessors;
174176
···body
@@ -183,7 +185,7 @@ macro {
183185
}
184186
}
185187

186-
macro {
188+
macro ·unsafe {
187189
function __set(···parameters) {
188190
accessors;
189191
···body
@@ -198,7 +200,7 @@ macro {
198200
}
199201
}
200202

201-
macro {
203+
macro ·unsafe {
202204
function __unset(···parameters) {
203205
accessors;
204206
···body

tests/MacroTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class MacroTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
* @dataProvider specs
10+
*/
11+
public function can_transform_code($from, $expected)
12+
{
13+
Pre\Plugin\addMacro(__DIR__ . "/../source/macros.yay");
14+
15+
$actual = Pre\plugin\format(Pre\Plugin\parse($this->format($from)));
16+
$this->assertEquals($this->format($expected), $actual, $actual);
17+
}
18+
19+
private function format($code)
20+
{
21+
return "<?php\n\n" . trim($code) . "\n";
22+
}
23+
24+
public static function specs()
25+
{
26+
$specs = [];
27+
28+
$files = [
29+
__DIR__ . "/specs/accessors.spec",
30+
__DIR__ . "/specs/fallbacks.spec",
31+
];
32+
33+
foreach ($files as $file) {
34+
$contents = file_get_contents($file);
35+
36+
foreach (explode("---", $contents) as $spec) {
37+
array_push($specs, explode("~~~", $spec));
38+
}
39+
}
40+
41+
return $specs;
42+
}
43+
}

tests/SpecTest.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/bootstrap.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/specs/accessors.spec

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
--DESCRIPTION--
2-
3-
Test class accessors macros
4-
5-
--GIVEN--
6-
7-
namespace Pre\ClassAccessors\Fixture;
8-
91
class Fixture
102
{
113
private $name {
@@ -86,9 +78,7 @@ class VeryChild extends Parent implements Parent, Parent, Parent {
8678

8779
}
8880

89-
--EXPECT--
90-
91-
namespace Pre\ClassAccessors\Fixture;
81+
~~~
9282

9383
class Fixture
9484
{

tests/specs/fallbacks.spec

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
--DESCRIPTION--
2-
3-
Test class accessors fallback macros
4-
5-
--GIVEN--
6-
7-
namespace Pre\ClassAccessors\Fixture;
8-
91
class Fixture
102
{
113
private $name {
@@ -48,9 +40,7 @@ class Fixture
4840
}
4941
}
5042

51-
--EXPECT--
52-
53-
namespace Pre\ClassAccessors\Fixture;
43+
~~~
5444

5545
class Fixture
5646
{
@@ -65,8 +55,8 @@ class Fixture
6555

6656
public function __get($property)
6757
{
68-
if ($result·2 = $this->handleGetClassAccessors($property)) {
69-
return $result·2;
58+
if ($result = $this->handleGetClassAccessors($property)) {
59+
return $result;
7060
}
7161

7262
print "getting {$property}";
@@ -81,8 +71,8 @@ class Fixture
8171

8272
public function __set($property, $value)
8373
{
84-
if ($result·4 = $this->handleSetClassAccessors($property, $value)) {
85-
return $result·4;
74+
if ($result = $this->handleSetClassAccessors($property, $value)) {
75+
return $result;
8676
}
8777

8878
print "setting {$property}";
@@ -97,8 +87,8 @@ class Fixture
9787

9888
public function __unset($property)
9989
{
100-
if ($result·6 = $this->handleUnsetClassAccessors($property)) {
101-
return $result·6;
90+
if ($result = $this->handleUnsetClassAccessors($property)) {
91+
return $result;
10292
}
10393

10494
print "unsetting {$property}";

0 commit comments

Comments
 (0)