Last active
November 9, 2018 17:35
Generate PHPDoc @method for magic getters/setters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// See https://youtrack.jetbrains.com/issue/WI-19869 | |
// require all php files (load class declarations) | |
$entitiesDirectory = '/path/to/entities'; | |
$files = | |
new CallbackFilterIterator( | |
new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($entitiesDirectory), RecursiveIteratorIterator::SELF_FIRST), | |
function (SplFileInfo $current, $key, $iterator) { | |
return ($current->getExtension() === 'php'); | |
}); | |
/** @var SplFileInfo $info */ | |
foreach ($files as $info) { | |
require_once $info->getPathname(); | |
} | |
foreach (get_declared_classes() as $className) { | |
try { | |
$reflectionClass = new ReflectionClass($className); | |
} catch (ReflectionException $e) { | |
error_log("ReflectionException $className"); | |
continue; | |
} | |
$methodNames = array_map( | |
function (ReflectionMethod $method) { | |
return $method->getName(); | |
}, | |
$reflectionClass->getMethods()); | |
$methodAnnotations = []; | |
$reflectionProperties = $reflectionClass->getProperties(); | |
foreach ($reflectionProperties as $reflectionProperty) { | |
if ($reflectionProperty->isStatic()) { | |
continue; | |
} | |
$propertyName = $reflectionProperty->getName(); | |
$matches = []; | |
preg_match( | |
'/@var (\w+\\\\)/', | |
$reflectionProperty->getDocComment(), | |
$matches); | |
if (!isset($matches[1])) { | |
error_log("$className::$propertyName has no type annotation"); | |
continue; | |
} | |
$propertyType = $matches[1]; | |
$getterName = 'get' . ucfirst($propertyName); | |
$setterName = 'set' . ucfirst($propertyName); | |
if (!in_array($getterName, $methodNames, true)) { | |
$methodAnnotations[] = " * @method $propertyType $getterName()"; | |
} | |
if (!in_array($setterName, $methodNames, true)) { | |
$methodAnnotations[] = " * @method void $setterName($propertyType \$$propertyName)"; | |
} | |
} | |
$hasBeenReplaced = false; | |
$oldDocComment = $reflectionClass->getDocComment(); | |
$newLineRegex = '(\r\n|\r|\n)'; | |
$newDocComment = | |
preg_replace( | |
"/(\\s\\*\\s*$newLineRegex( \* @method.+?$newLineRegex)+)?\\s?\\*\\//", | |
" *\n" . join("\n", $methodAnnotations) . "\n */", | |
$oldDocComment, | |
1, | |
$hasBeenReplaced); | |
if (!$hasBeenReplaced) { | |
error_log("Could not update $className"); | |
} elseif ($oldDocComment === $newDocComment) { | |
error_log("Nothing to update in $className"); | |
} else { | |
file_put_contents( | |
$reflectionClass->getFileName(), | |
str_replace( | |
$oldDocComment, | |
$newDocComment, | |
file_get_contents($reflectionClass->getFileName()))); | |
error_log("Updated $className"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, the doc comment of
class Foo
will be updated to