Skip to content

Commit

Permalink
Merge pull request #3 from preprocess/develop
Browse files Browse the repository at this point in the history
Added simple + immutable accessors
  • Loading branch information
assertchris authored May 20, 2017
2 parents 43a44af + a974209 commit 003d36d
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 45 deletions.
214 changes: 169 additions & 45 deletions src/macros.yay
Original file line number Diff line number Diff line change
@@ -1,62 +1,170 @@
<?php

macro {
class ·ns()·class {
···body
}
} >> {
class ·class {
use \Pre\ClassAccessors\ClassAccessorsTrait;

···body
}
}

macro ·unsafe {
private ·optional(·either(·ns(), T_ARRAY·a))·type T_VARIABLE·name {
·chain (
·either(
private,
protected
)·visibility,
·optional(
·either(
·ns(),
·token(T_ARRAY)
)
)·type,
·token(T_VARIABLE)·name,
·token("{"),
·repeat(
·either(
·chain(
get,
·between(
·token("{"), ·layer(), ·token("}")
)·getter_body
·token(";")
)·simpleGetter,
·chain(
get,
·token("{"),
·layer()·getterBody,
·token("}"),
·optional(·token(";"))
)·getter,
·chain(
set,
·between(
·token("{"), ·layer(), ·token("}")
)·setter_body
·token(";")
)·simpleSetter,
·chain(
set,
·token("{"),
·layer()·setterBody,
·token("}"),
·optional(·token(";"))
)·setter,
·chain(
immutable,
set,
·token(";")
)·immutableSimpleSetter,
·chain(
immutable,
set,
·token("{"),
·layer()·immutableSetterBody,
·token("}"),
·optional(·token(";"))
)·immutableSetter,
·chain(
unset,
·token(";")
)·simpleUnsetter,
·chain(
unset,
·token("{"),
·layer()·unsetterBody,
·token("}"),
·optional(·token(";"))
)·unsetter,
·chain(
immutable,
unset,
·between(
·token("{"), ·layer(), ·token("}")
)·unsetter_body
)·unsetter
·token(";")
)·immutableSimpleUnsetter,
·chain(
immutable,
unset,
·token("{"),
·layer()·immutableUnsetterBody,
·token("}"),
·optional(·token(";"))
)·immutableUnsetter
)
)·accessors
}
·optional(·token(";"))
)·accessors,
·token("}"),
·optional(·token(";"))
)
} >> {
private T_VARIABLE·name;
·visibility ·name;

·accessors ··· {
·getter ?··· {
public function ··concat(get ··studly(··unvar(T_VARIABLE·name)))()··class_accessors_return(·type) {
·getter_body
}
}
·setter ?··· {
public function ··concat(set ··studly(··unvar(T_VARIABLE·name)))(·type $value) {
·setter_body
}
}
·unsetter ?··· {
public function ··concat(unset ··studly(··unvar(T_VARIABLE·name)))() {
·unsetter_body
}
}
·simpleGetter ?· {··trim(
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {··trim(
return $this->··unvar(·name);
)}
)}

·getter ?· {··trim(
public function ··concat(get ··studly(··unvar(·name)))()··class_accessors_return(·type) {··trim(
·getterBody
)}
)}

·simpleSetter ?· {··trim(
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
$this->··unvar(·name) = $value;
return $this;
)}
)}

·setter ?· {··trim(
public function ··concat(set ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
·setterBody
)}
)}

·immutableSimpleSetter ?· {··trim(
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
$clone = clone($this);
$clone->··unvar(·name) = $value;
return $clone;
)}
)}

·immutableSetter ?· {··trim(
public function ··concat(with ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
$clone = clone $this;

$bound = \Closure::bind(function() use ($value) {
·immutableSetterBody
}, $clone);

$bound();

return $clone;
)}
)}

·simpleUnsetter ?· {··trim(
public function ··concat(unset ··studly(··unvar(·name)))() {··trim(
unset($this->··unvar(·name));
return $this;
)}
)}

·unsetter ?· {··trim(
public function ··concat(unset ··studly(··unvar(·name)))() {··trim(
·unsetterBody
)}
)}

·immutableSimpleUnsetter ?· {··trim(
public function ··concat(without ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
$clone = clone($this);
unset($clone->··unvar(·name));
return $clone;
)}
)}

·immutableUnsetter ?· {··trim(
public function ··concat(without ··studly(··unvar(·name)))(··trim(·type $value)) {··trim(
$clone = clone $this;

$bound = \Closure::bind(function() use ($value) {
·immutableUnsetterBody
}, $clone);

$bound();

return $clone;
)}
)}
}
}

Expand All @@ -71,7 +179,7 @@ macro {
return $result;
}

···body
··trim(···body)
}
}

Expand All @@ -86,7 +194,7 @@ macro {
return $result;
}

···body
··trim(···body)
}
}

Expand All @@ -101,6 +209,22 @@ macro {
return $result;
}

···body
··trim(···body)
}
}

macro {
·chain(
class,
·ns()·name,
·token("{"),
·layer()·body,
·token("}")
)
} >> {
class ·name {
use \Pre\ClassAccessors\ClassAccessorsTrait;

·body
}
}
117 changes: 117 additions & 0 deletions tests/specs/accessors.spec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ class Fixture
$this->shouldDoThing = $value;
}
}

private string $typed {
get {
return $this->typed;
}

set {
$this->typed = $value;
}
}

protected $protected {
get {
return $this->protected;
}
}

private $simple {
get; set; unset;
}

private $immutable {
immutable set {
$this->immutable = $value;
}

immutable unset {
unset($this->immutable);
}
}

private $immutableSimple {
immutable set;
immutable unset;
}
}

--EXPECT--
Expand Down Expand Up @@ -82,4 +117,86 @@ class Fixture
{
$this->shouldDoThing = $value;
}

private $typed;

public function getTyped(): string
{
return $this->typed;
}

public function setTyped(string $value)
{
$this->typed = $value;
}

protected $protected;

public function getProtected()
{
return $this->protected;
}

private $simple;

public function getSimple()
{
return $this->simple;
}

public function setSimple($value)
{
$this->simple = $value;
return $this;
}

public function unsetSimple()
{
unset($this->simple);
return $this;
}

private $immutable;

public function withImmutable($value)
{
$clone = clone $this;

$bound = \Closure::bind(function () use ($value) {
$this->immutable = $value;
}, $clone);

$bound();

return $clone;
}

public function withoutImmutable($value)
{
$clone = clone $this;

$bound = \Closure::bind(function () use ($value) {
unset($this->immutable);
}, $clone);

$bound();

return $clone;
}

private $immutableSimple;

public function withImmutableSimple($value)
{
$clone = clone($this);
$clone->immutableSimple = $value;
return $clone;
}

public function withoutImmutableSimple($value)
{
$clone = clone($this);
unset($clone->immutableSimple);
return $clone;
}
}

0 comments on commit 003d36d

Please sign in to comment.