| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Go | 250306T182632Z | bigyihsu |
| nan | Perl | 151112T221239Z | faubi |
| nan | Pyth | 151112T154122Z | kirbyfan |
| nan | JavaScript ES6 at the moment | 151112T155002Z | Conor O& |
| 071 | CJam | 151112T153859Z | Dennis |
Go, 452 bytes - 90% = 45.2 bytes
import(."fmt";."regexp";."strings")
func f(s string)string{C:=MustCompile
M,E,G,S,P,F,U:=C(`(p.+? )(static )?([a-zA-Z][a-zA-Z0-9]*)(;)`).FindAllStringSubmatch(s,-1),C(`}$`).FindStringIndex(s)[0],`%[1]s get%[3]s(){return this->%[2]s;}`,`%[1]s set%[3]s(%[2]s){this->%[2]s=%[2]s;}`,"public function",Sprintf,ToUpper
for _,r:=range M{m:=r[3]
g,t:=F(G,P,m,U(m[:1])+m[1:]),F(S,P,m,U(m[:1])+m[1:])
if !Contains(s,g){s=s[:E]+g+s[E:]}
s=s[:E]+t+s[E:]}
return s}
(semi-ungolfed) Explanation
import(."fmt";."regexp";."strings") // boilerplate
func f(s string)string{
// regexp for finding the member names, and find all groups
// also handles `static` (bonus C)
M:=MustCompile(`(p.+? )(static )?([a-zA-Z][a-zA-Z0-9]*)(;)`).FindAllStringSubmatch(s, -1)
// regexp for finding the last curly bracket, and get the index
E:=MustCompile(`}$`).FindStringIndex(s)[0]
G:=`%[1]s get%[3]s(){return this->%[2]s;}` // format for getter
S:=`%[1]s set%[3]s(%[2]s){this->%[2]s=%[2]s;}` // format for setter
P:="public function"
for _,r:=range M{m:=r[3] // for each member...
// generate the getter and setter
g,t:=Sprintf(G,P,m,ToUpper(m[:1])+m[1:]),Sprintf(S,P,m,ToUpper(m[:1])+m[1:])
if !Contains(s,g){s=s[:E]+g+s[E:]} // skip if getter exists (bonus A)
s=s[:E]+t+s[E:]} // always add the setter (bonus B)
return s}
Outputs a highly condensed class, with no whitespace between getters and setters.
Perl, 161 - 90% = 16.1 bytes
$/=0;$_=<>;for$g(/\bp\S*( +static)? +(\S*);/g){++$i%2?$c="public$g function":/get\u$g/||s/}$/$c get\u$g(){return this->$g;}\n$c set\u$g(x){this->$g=x;}\n}/}print
Pyth, 198 bytes - 90% = 19.8 bytes 187 - 90% = 18.7 bytes 183 bytes - 90% = 18.3 bytes
pJ<rs.z6_1sm?}+=b"get"K+rh=Zed1tZJks[Y=N|@d1kGbK"(){return "=H+"this->"Z";}"YNG"set"K"(x){"H"=x;}"):Js["(?:(?:"=Y"public""|private|protected)(?!"=G" function "")( static)?) (\w+)")4\}
Must...beat...Perl...
187-byte/18.7-byte version
J<rs.z6_1s_+\},sm?}+=b"get"K+rh=Zed1tZJks[Y=N|@d1kGbK"(){return "=H+"this->"Z";}"YNG"set"K"(x){"H"=x;}"):Js["(?:(?:"=Y"public""|private|protected)(?!"=G" function "")( static)?) (\w+)")4J
198-byte/19.8-byte version
J<rs.z6_1s_,sm?}K+rhed1tedJks["public"=N|@d1k=G" function ""get"K"(){return this->"ed";}public"NG"set"K"("ed"){this->"ed"="ed";}"):J"(?:(?:public|private|protected)(?! function )( static)?) (\w+)"4J
TODO: More golfing!
JavaScript ES6 (at the moment), 305 289 223 - 60% = 89.2 bytes
Was 256 - 30% = 179.2 bytes
Qualifies for static and setter bonuses; now with extra ES6!
s=>s.replace(/\}$/,s.match(/(public|private)( static)* \w+/g).map(e=>{o=(r=e.split` `).pop();return(n=r.join` `)+` get${U=o[0].toUpperCase()+o.slice(1)}(){return this->${o};}${n} set${U}(${o}){this->${o}=${o};}`}).join``+"}")
ES5 function, 115.6 bytes
function g(s){return s.replace(/\}$/,s.match(/(p(?:ublic|rivate))( static)* (\w+?);/gm).map(function(e){o=(r=e.split(" ")).pop().replace(/;/,"");return(n=r.join(" "))+" get"+(U=o[0].toUpperCase()+o.slice(1))+"(){return this->"+o+";}"+n+" set"+U+"("+o+"){this->"+o+"="+o+";}"}).join("")+"}")}
CJam, 71 bytes
q';/W<_{S%W=:O(eu"public function get"\@"{return this->"O";}"}%\';f+\'}
Try it online in the CJam interpreter.