Skip to content

Instantly share code, notes, and snippets.

@kraftb
Created March 5, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kraftb/5d65254083d8f67bb1dc8aaf22915afa to your computer and use it in GitHub Desktop.
Save kraftb/5d65254083d8f67bb1dc8aaf22915afa to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// generateGettersAndSetters.php
//
// (c) 2017: kraftb@think-open.at
// Version: 1.0
$getterTemplate = '
/**
* Returns ###lc_desc###
*
* @return ###type### ###desc###
*/
public function get###uc_name###() {
return $this->###name###;
}
';
$setterTemplate = '
/**
* Sets ###lc_desc###
*
* @param ###type### $###name###: ###desc###
* @return void
*/
public function set###uc_name###(###arg_type###$###name###) {
$this->###name### = $###name###;
}
';
function replaceMarkers($template, $name, $type, $desc) {
$arg_type = ($type === 'array' || substr($type, 0, 1) === '\\') ? $type . ' ' : '';
$markers = array(
'###desc###' => $desc,
'###lc_desc###' => lcfirst($desc),
'###type###' => $type,
'###arg_type###' => $arg_type,
'###name###' => $name,
'###uc_name###' => ucfirst($name),
);
return str_replace(array_keys($markers), array_values($markers), $template);
}
function removeDocCommentFormatting($desc) {
$lines = explode("\n", trim($desc));
$result = array();
foreach ($lines as $line) {
$line = trim($line, "/* \t");
if ($line) {
$result[] = $line;
}
}
return $result[0];
}
require_once($argv[1]);
// $class = file_get_contents($argv[1]);
// echo $class;
// $properteis =
$reflected = new \ReflectionClass(\ThinkopenAt\Gnucash\Domain\Model\InvoiceEntry::class);
$properties = $reflected->getProperties();
foreach ($properties as $property) {
$comment = trim($property->getDocComment());
// $comment = trim(preg_replace('|^/\*\*?|', '', $comment));
// $comment = trim(preg_replace('|\*?\*/|', '', $comment));
// $comment = trim(preg_replace('|^\s*\*\s*|m', "", $comment));
if (!preg_match('/^(.*)@var\s+(.*)\s+/s', $comment, $matches)) {
continue;
}
$desc = trim($matches[1]);
$desc = removeDocCommentFormatting($desc);
$type = trim($matches[2]);
$name = $property->getName();
$getter = replaceMarkers($getterTemplate, $name, $type, $desc);
$setter = replaceMarkers($setterTemplate, $name, $type, $desc);
echo $getter;
echo $setter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment