├── .gitignore ├── code-of-conduct.md ├── composer.json ├── license.md ├── phpunit.xml ├── readme.md ├── source ├── ClassAccessorsTrait.php ├── expanders.php └── macros.yay └── tests ├── MacroTest.php └── specs ├── accessors.spec └── fallbacks.spec /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Our Pledge 2 | 3 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 4 | 5 | # Our Standards 6 | 7 | Examples of behavior that contributes to creating a positive environment include: 8 | 9 | * Using welcoming and inclusive language 10 | * Being respectful of differing viewpoints and experiences 11 | * Gracefully accepting constructive criticism 12 | * Focusing on what is best for the community 13 | * Showing empathy towards other community members 14 | 15 | Examples of unacceptable behavior by participants include: 16 | 17 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 18 | * Trolling, insulting/derogatory comments, and personal or political attacks 19 | * Public or private harassment 20 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 21 | * Other conduct which could reasonably be considered inappropriate in a professional setting 22 | 23 | # Our Responsibilities 24 | 25 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 26 | 27 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 28 | 29 | # Scope 30 | 31 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 32 | 33 | # Enforcement 34 | 35 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at cgpitt@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 36 | 37 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 38 | 39 | # Attribution 40 | 41 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 42 | 43 | [homepage]: http://contributor-covenant.org 44 | [version]: http://contributor-covenant.org/version/1/4 45 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "pre-macro", 3 | "name": "pre/class-accessors", 4 | "license": "MIT", 5 | "require": { 6 | "pre/plugin": "^0.11.3" 7 | }, 8 | "autoload": { 9 | "files": [ 10 | "source/expanders.php" 11 | ], 12 | "psr-4": { 13 | "Pre\\ClassAccessors\\": "source" 14 | } 15 | }, 16 | "require-dev": { 17 | "phpunit/phpunit": "^5.0|^6.0" 18 | }, 19 | "extra": { 20 | "macros": [ 21 | "source/macros.yay" 22 | ] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | Copyright Christopher Pitt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | tests 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Class Accessors 2 | 3 | Documentation can be found at [preprocess.io](https://preprocess.io#class-accessors). 4 | -------------------------------------------------------------------------------- /source/ClassAccessorsTrait.php: -------------------------------------------------------------------------------- 1 | handleGetClassAccessors($property); 16 | } 17 | 18 | /** 19 | * Finds and invokes getters. 20 | * 21 | * @param string $property 22 | * @param mixed $value 23 | */ 24 | protected function handleGetClassAccessors($property) 25 | { 26 | $property = $this->studly($property); 27 | 28 | if (method_exists($this, "get{$property}")) { 29 | return $this->{"get{$property}"}(); 30 | } 31 | } 32 | 33 | /** 34 | * Replaces "this_format" and "this-format" with "ThisFormat". 35 | * 36 | * @return string 37 | */ 38 | private function studly($string) 39 | { 40 | return str_replace(" ", "", ucwords(str_replace(["-", "_"], " ", $string))); 41 | } 42 | 43 | /** 44 | * @inheritdoc 45 | * 46 | * @param string $property 47 | * @param mixed $value 48 | */ 49 | public function __set($property, $value) 50 | { 51 | return $this->handleSetClassAccessors($property, $value); 52 | } 53 | 54 | /** 55 | * Finds and invokes setters. 56 | * 57 | * @param string $property 58 | * @param mixed $value 59 | */ 60 | protected function handleSetClassAccessors($property, $value) 61 | { 62 | $property = $this->studly($property); 63 | 64 | if (method_exists($this, "set{$property}")) { 65 | return $this->{"set{$property}"}($value); 66 | } 67 | } 68 | 69 | /** 70 | * @inheritdoc 71 | * 72 | * @param string $property 73 | */ 74 | public function __unset($property) 75 | { 76 | return $this->handleUnsetClassAccessors($property); 77 | } 78 | 79 | /** 80 | * Finds and invokes unsetters. 81 | * 82 | * @param string $property 83 | */ 84 | protected function handleUnsetClassAccessors($property) 85 | { 86 | $property = $this->studly($property); 87 | 88 | if (method_exists($this, "unset{$property}")) { 89 | return $this->{"unset{$property}"}(); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /source/expanders.php: -------------------------------------------------------------------------------- 1 | current())) { 11 | $stream = ": {$stream}"; 12 | } 13 | 14 | return TokenStream::fromSource( 15 | $engine->expand($stream, "", Engine::GC_ENGINE_DISABLED) 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /source/macros.yay: -------------------------------------------------------------------------------- 1 | > { 84 | $$(collapse($$(trim( 85 | $(visibility) $(name); 86 | 87 | $(accessors ... { 88 | $(simpleGetter ? {$$(trim( 89 | public function $$(concat(get $$(studly($$(unvar($(name)))))))()$$(\Pre\ClassAccessors\classAccessorsReturn($(type))) { 90 | return $this->$$(unvar($(name))); 91 | } 92 | ))}) 93 | 94 | $(getter ? {$$(trim( 95 | public function $$(concat(get $$(studly($$(unvar($(name)))))))()$$(\Pre\ClassAccessors\classAccessorsReturn($(type))) { 96 | $(getter ... {$$(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 | $(setter ... {$$(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 | $(immutableSetter ... {$$(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 | $(unsetter ... {$$(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 | $(immutableUnsetter ... {$$(trim($(immutableUnsetterBody)))}) 162 | }, $clone); 163 | 164 | $bound(); 165 | 166 | return $clone; 167 | } 168 | ))}) 169 | }) 170 | )))) 171 | } 172 | 173 | $(macro :unsafe) { 174 | function __get($(layer() as parameters)) { 175 | accessors; 176 | $(layer() as body) 177 | } 178 | } >> { 179 | function __get($(parameters)) { 180 | if ($result = $this->handleGetClassAccessors($(parameters))) { 181 | return $result; 182 | } 183 | 184 | $$(trim($(body))) 185 | } 186 | } 187 | 188 | $(macro :unsafe) { 189 | function __set($(layer() as parameters)) { 190 | accessors; 191 | $(layer() as body) 192 | } 193 | } >> { 194 | function __set($(parameters)) { 195 | if ($result = $this->handleSetClassAccessors($(parameters))) { 196 | return $result; 197 | } 198 | 199 | $$(trim($(body))) 200 | } 201 | } 202 | 203 | $(macro :unsafe) { 204 | function __unset($(layer() as parameters)) { 205 | accessors; 206 | $(layer() as body) 207 | } 208 | } >> { 209 | function __unset($(parameters)) { 210 | if ($result = $this->handleUnsetClassAccessors($(parameters))) { 211 | return $result; 212 | } 213 | 214 | $$(trim($(body))) 215 | } 216 | } 217 | 218 | $(macro) { 219 | $(chain( 220 | class, 221 | ns() as name, 222 | optional ( 223 | chain( 224 | extends, 225 | ns() as extendsName 226 | ) as extendsItem 227 | ), 228 | optional ( 229 | chain( 230 | implements, 231 | lst( 232 | ns() as implementsName, 233 | token(",") 234 | ) as implementsItems 235 | ) 236 | ) as implements, 237 | token("{"), 238 | layer() as body, 239 | token("}") 240 | )) 241 | } >> { 242 | class $(name) $(extendsItem ? {extends $(extendsItem ... {$(extendsName)})}) $(implements ? {implements $(implements ... {$(implementsItems ... ( , ){$(implementsName)})})}) { 243 | use \Pre\ClassAccessors\ClassAccessorsTrait; 244 | 245 | $(body) 246 | } 247 | } 248 | -------------------------------------------------------------------------------- /tests/MacroTest.php: -------------------------------------------------------------------------------- 1 | format($from))); 16 | $this->assertEquals($this->format($expected), $actual); 17 | } 18 | 19 | private function format($code) 20 | { 21 | return "name; 6 | } 7 | 8 | set { 9 | $this->name = ucwords($value); 10 | } 11 | 12 | unset { 13 | $this->name = "unset"; 14 | } 15 | }; 16 | 17 | private $person_age { 18 | set { 19 | $this->person_age = round($value); 20 | } 21 | }; 22 | 23 | private bool $shouldDoThing { 24 | get { 25 | return $this->shouldDoThing; 26 | } 27 | 28 | set { 29 | $this->shouldDoThing = $value; 30 | } 31 | } 32 | 33 | private string $typed { 34 | get { 35 | return $this->typed; 36 | } 37 | 38 | set { 39 | $this->typed = $value; 40 | } 41 | } 42 | 43 | protected $protected { 44 | get { 45 | return $this->protected; 46 | } 47 | } 48 | 49 | private $simple { 50 | get; set; unset; 51 | } 52 | 53 | private $immutable { 54 | immutable set { 55 | $this->immutable = $value; 56 | } 57 | 58 | immutable unset { 59 | unset($this->immutable); 60 | } 61 | } 62 | 63 | private $immutableSimple { 64 | immutable set; 65 | immutable unset; 66 | } 67 | } 68 | 69 | class Child extends Parent { 70 | 71 | } 72 | 73 | class Child implements Parent { 74 | 75 | } 76 | 77 | class VeryChild extends Parent implements Parent, Parent, Parent { 78 | 79 | } 80 | 81 | ~~~ 82 | 83 | class Fixture 84 | { 85 | use \Pre\ClassAccessors\ClassAccessorsTrait; 86 | 87 | private $name; 88 | 89 | public function getName() 90 | { 91 | return $this->name; 92 | } 93 | 94 | public function setName($value) 95 | { 96 | $this->name = ucwords($value); 97 | } 98 | 99 | public function unsetName() 100 | { 101 | $this->name = "unset"; 102 | } 103 | 104 | private $person_age; 105 | 106 | public function setPersonAge($value) 107 | { 108 | $this->person_age = round($value); 109 | } 110 | 111 | private $shouldDoThing; 112 | 113 | public function getShouldDoThing(): bool 114 | { 115 | return $this->shouldDoThing; 116 | } 117 | 118 | public function setShouldDoThing(bool $value) 119 | { 120 | $this->shouldDoThing = $value; 121 | } 122 | 123 | private $typed; 124 | 125 | public function getTyped(): string 126 | { 127 | return $this->typed; 128 | } 129 | 130 | public function setTyped(string $value) 131 | { 132 | $this->typed = $value; 133 | } 134 | 135 | protected $protected; 136 | 137 | public function getProtected() 138 | { 139 | return $this->protected; 140 | } 141 | 142 | private $simple; 143 | 144 | public function getSimple() 145 | { 146 | return $this->simple; 147 | } 148 | 149 | public function setSimple($value) 150 | { 151 | $this->simple = $value; 152 | return $this; 153 | } 154 | 155 | public function unsetSimple() 156 | { 157 | unset($this->simple); 158 | return $this; 159 | } 160 | 161 | private $immutable; 162 | 163 | public function withImmutable($value) 164 | { 165 | $clone = clone $this; 166 | 167 | $bound = \Closure::bind(function () use ($value) { 168 | $this->immutable = $value; 169 | }, $clone); 170 | 171 | $bound(); 172 | 173 | return $clone; 174 | } 175 | 176 | public function withoutImmutable() 177 | { 178 | $clone = clone $this; 179 | 180 | $bound = \Closure::bind(function () { 181 | unset($this->immutable); 182 | }, $clone); 183 | 184 | $bound(); 185 | 186 | return $clone; 187 | } 188 | 189 | private $immutableSimple; 190 | 191 | public function withImmutableSimple($value) 192 | { 193 | $clone = clone $this; 194 | $clone->immutableSimple = $value; 195 | return $clone; 196 | } 197 | 198 | public function withoutImmutableSimple() 199 | { 200 | $clone = clone $this; 201 | unset($clone->immutableSimple); 202 | return $clone; 203 | } 204 | } 205 | 206 | class Child extends Parent 207 | { 208 | use \Pre\ClassAccessors\ClassAccessorsTrait; 209 | } 210 | 211 | class Child implements Parent 212 | { 213 | use \Pre\ClassAccessors\ClassAccessorsTrait; 214 | } 215 | 216 | class VeryChild extends Parent implements Parent, Parent, Parent 217 | { 218 | use \Pre\ClassAccessors\ClassAccessorsTrait; 219 | } 220 | -------------------------------------------------------------------------------- /tests/specs/fallbacks.spec: -------------------------------------------------------------------------------- 1 | class Fixture 2 | { 3 | private $name { 4 | get { 5 | return $this->name; 6 | } 7 | } 8 | 9 | public function __get($property) 10 | { 11 | accessors; 12 | 13 | print "getting {$property}"; 14 | } 15 | 16 | private $age { 17 | set { 18 | $this->age = $value; 19 | } 20 | } 21 | 22 | public function __set($property, $value) 23 | { 24 | accessors; 25 | 26 | print "setting {$property}"; 27 | } 28 | 29 | private $feature { 30 | unset { 31 | $this->feature = null; 32 | } 33 | } 34 | 35 | public function __unset($property) 36 | { 37 | accessors; 38 | 39 | print "unsetting {$property}"; 40 | } 41 | } 42 | 43 | ~~~ 44 | 45 | class Fixture 46 | { 47 | use \Pre\ClassAccessors\ClassAccessorsTrait; 48 | 49 | private $name; 50 | 51 | public function getName() 52 | { 53 | return $this->name; 54 | } 55 | 56 | public function __get($property) 57 | { 58 | if (($result = $this->handleGetClassAccessors($property))) { 59 | return $result; 60 | } 61 | 62 | print "getting {$property}"; 63 | } 64 | 65 | private $age; 66 | 67 | public function setAge($value) 68 | { 69 | $this->age = $value; 70 | } 71 | 72 | public function __set($property, $value) 73 | { 74 | if (($result = $this->handleSetClassAccessors($property, $value))) { 75 | return $result; 76 | } 77 | 78 | print "setting {$property}"; 79 | } 80 | 81 | private $feature; 82 | 83 | public function unsetFeature() 84 | { 85 | $this->feature = null; 86 | } 87 | 88 | public function __unset($property) 89 | { 90 | if (($result = $this->handleUnsetClassAccessors($property))) { 91 | return $result; 92 | } 93 | 94 | print "unsetting {$property}"; 95 | } 96 | } 97 | --------------------------------------------------------------------------------