Skip to content

Commit 003d36d

Browse files
authored
Merge pull request #3 from preprocess/develop
Added simple + immutable accessors
2 parents 43a44af + a974209 commit 003d36d

File tree

2 files changed

+286
-45
lines changed

2 files changed

+286
-45
lines changed

src/macros.yay

Lines changed: 169 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,170 @@
11
<?php
22

3-
macro {
4-
class ·ns()·class {
5-
···body
6-
}
7-
} >> {
8-
class ·class {
9-
use \Pre\ClassAccessors\ClassAccessorsTrait;
10-
11-
···body
12-
}
13-
}
14-
153
macro ·unsafe {
16-
private ·optional(·either(·ns(), T_ARRAY·a))·type T_VARIABLE·name {
4+
·chain (
5+
·either(
6+
private,
7+
protected
8+
)·visibility,
9+
·optional(
10+
·either(
11+
·ns(),
12+
·token(T_ARRAY)
13+
)
14+
)·type,
15+
·token(T_VARIABLE)·name,
16+
·token("{"),
1717
·repeat(
1818
·either(
1919
·chain(
2020
get,
21-
·between(
22-
·token("{"), ·layer(), ·token("}")
23-
)·getter_body
21+
·token(";")
22+
)·simpleGetter,
23+
·chain(
24+
get,
25+
·token("{"),
26+
·layer()·getterBody,
27+
·token("}"),
28+
·optional(·token(";"))
2429
)·getter,
2530
·chain(
2631
set,
27-
·between(
28-
·token("{"), ·layer(), ·token("}")
29-
)·setter_body
32+
·token(";")
33+
)·simpleSetter,
34+
·chain(
35+
set,
36+
·token("{"),
37+
·layer()·setterBody,
38+
·token("}"),
39+
·optional(·token(";"))
3040
)·setter,
3141
·chain(
42+
immutable,
43+
set,
44+
·token(";")
45+
)·immutableSimpleSetter,
46+
·chain(
47+
immutable,
48+
set,
49+
·token("{"),
50+
·layer()·immutableSetterBody,
51+
·token("}"),
52+
·optional(·token(";"))
53+
)·immutableSetter,
54+
·chain(
55+
unset,
56+
·token(";")
57+
)·simpleUnsetter,
58+
·chain(
59+
unset,
60+
·token("{"),
61+
·layer()·unsetterBody,
62+
·token("}"),
63+
·optional(·token(";"))
64+
)·unsetter,
65+
·chain(
66+
immutable,
3267
unset,
33-
·between(
34-
·token("{"), ·layer(), ·token("}")
35-
)·unsetter_body
36-
)·unsetter
68+
·token(";")
69+
)·immutableSimpleUnsetter,
70+
·chain(
71+
immutable,
72+
unset,
73+
·token("{"),
74+
·layer()·immutableUnsetterBody,
75+
·token("}"),
76+
·optional(·token(";"))
77+
)·immutableUnsetter
3778
)
38-
)·accessors
39-
}
40-
·optional(·token(";"))
79+
)·accessors,
80+
·token("}"),
81+
·optional(·token(";"))
82+
)
4183
} >> {
42-
private T_VARIABLE·name;
84+
·visibility ·name;
4385

4486
·accessors ··· {
45-
·getter ?··· {
46-
public function ··concat(get ··studly(··unvar(T_VARIABLE·name)))()··class_accessors_return(·type) {
47-
·getter_body
48-
}
49-
}
50-
·setter ?··· {
51-
public function ··concat(set ··studly(··unvar(T_VARIABLE·name)))(·type $value) {
52-
·setter_body
53-
}
54-
}
55-
·unsetter ?··· {
56-
public function ··concat(unset ··studly(··unvar(T_VARIABLE·name)))() {
57-
·unsetter_body
58-
}
59-
}
87+
·simpleGetter ?· {··trim(
88+
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {··trim(
89+
return $this->··unvar(·name);
90+
)}
91+
)}
92+
93+
·getter ?· {··trim(
94+
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {··trim(
95+
·getterBody
96+
)}
97+
)}
98+
99+
·simpleSetter ?· {··trim(
100+
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
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)) {··trim(
108+
·setterBody
109+
)}
110+
)}
111+
112+
·immutableSimpleSetter ?· {··trim(
113+
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
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)) {··trim(
122+
$clone = clone $this;
123+
124+
$bound = \Closure::bind(function() use ($value) {
125+
·immutableSetterBody
126+
}, $clone);
127+
128+
$bound();
129+
130+
return $clone;
131+
)}
132+
)}
133+
134+
·simpleUnsetter ?· {··trim(
135+
public function ··concat(unset ··studly(··unvar(·name)))() {··trim(
136+
unset($this->··unvar(·name));
137+
return $this;
138+
)}
139+
)}
140+
141+
·unsetter ?· {··trim(
142+
public function ··concat(unset ··studly(··unvar(·name)))() {··trim(
143+
·unsetterBody
144+
)}
145+
)}
146+
147+
·immutableSimpleUnsetter ?· {··trim(
148+
public function ··concat(without ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
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)))(··trim(·type $value)) {··trim(
157+
$clone = clone $this;
158+
159+
$bound = \Closure::bind(function() use ($value) {
160+
·immutableUnsetterBody
161+
}, $clone);
162+
163+
$bound();
164+
165+
return $clone;
166+
)}
167+
)}
60168
}
61169
}
62170

@@ -71,7 +179,7 @@ macro {
71179
return $result;
72180
}
73181

74-
···body
182+
··trim(···body)
75183
}
76184
}
77185

@@ -86,7 +194,7 @@ macro {
86194
return $result;
87195
}
88196

89-
···body
197+
··trim(···body)
90198
}
91199
}
92200

@@ -101,6 +209,22 @@ macro {
101209
return $result;
102210
}
103211

104-
···body
212+
··trim(···body)
213+
}
214+
}
215+
216+
macro {
217+
·chain(
218+
class,
219+
·ns()·name,
220+
·token("{"),
221+
·layer()·body,
222+
·token("}")
223+
)
224+
} >> {
225+
class ·name {
226+
use \Pre\ClassAccessors\ClassAccessorsTrait;
227+
228+
·body
105229
}
106230
}

tests/specs/accessors.spec

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ class Fixture
3737
$this->shouldDoThing = $value;
3838
}
3939
}
40+
41+
private string $typed {
42+
get {
43+
return $this->typed;
44+
}
45+
46+
set {
47+
$this->typed = $value;
48+
}
49+
}
50+
51+
protected $protected {
52+
get {
53+
return $this->protected;
54+
}
55+
}
56+
57+
private $simple {
58+
get; set; unset;
59+
}
60+
61+
private $immutable {
62+
immutable set {
63+
$this->immutable = $value;
64+
}
65+
66+
immutable unset {
67+
unset($this->immutable);
68+
}
69+
}
70+
71+
private $immutableSimple {
72+
immutable set;
73+
immutable unset;
74+
}
4075
}
4176

4277
--EXPECT--
@@ -82,4 +117,86 @@ class Fixture
82117
{
83118
$this->shouldDoThing = $value;
84119
}
120+
121+
private $typed;
122+
123+
public function getTyped(): string
124+
{
125+
return $this->typed;
126+
}
127+
128+
public function setTyped(string $value)
129+
{
130+
$this->typed = $value;
131+
}
132+
133+
protected $protected;
134+
135+
public function getProtected()
136+
{
137+
return $this->protected;
138+
}
139+
140+
private $simple;
141+
142+
public function getSimple()
143+
{
144+
return $this->simple;
145+
}
146+
147+
public function setSimple($value)
148+
{
149+
$this->simple = $value;
150+
return $this;
151+
}
152+
153+
public function unsetSimple()
154+
{
155+
unset($this->simple);
156+
return $this;
157+
}
158+
159+
private $immutable;
160+
161+
public function withImmutable($value)
162+
{
163+
$clone = clone $this;
164+
165+
$bound = \Closure::bind(function () use ($value) {
166+
$this->immutable = $value;
167+
}, $clone);
168+
169+
$bound();
170+
171+
return $clone;
172+
}
173+
174+
public function withoutImmutable($value)
175+
{
176+
$clone = clone $this;
177+
178+
$bound = \Closure::bind(function () use ($value) {
179+
unset($this->immutable);
180+
}, $clone);
181+
182+
$bound();
183+
184+
return $clone;
185+
}
186+
187+
private $immutableSimple;
188+
189+
public function withImmutableSimple($value)
190+
{
191+
$clone = clone($this);
192+
$clone->immutableSimple = $value;
193+
return $clone;
194+
}
195+
196+
public function withoutImmutableSimple($value)
197+
{
198+
$clone = clone($this);
199+
unset($clone->immutableSimple);
200+
return $clone;
201+
}
85202
}

0 commit comments

Comments
 (0)