Skip to content
Snippets Groups Projects
Commit ffc91490 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

add method definitions for sorm's magic methods

parent fa7dfaf0
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,16 @@ use Symfony\Component\Console\Output\OutputInterface;
final class DescribeModels extends AbstractCommand
{
const METHODS_TEMPLATES = [
'findOneBy%s' => 'static static findOneBy%s(string $value, string $order = \'\')',
'findBy%s' => 'static static[] findBy%s(string $value, string $order = \'\')',
'findManyBy%s' => 'static static[] findManyBy%s(array $values, string $order = \'\')',
'findEachBy%s' => 'static static[] findEachBy%s(callable $callable, string $value, string $order = \'\')',
'findEachManyBy%s' => 'static static[] findEachManyBy%s(callable $callable, array $values, string $order = \'\')',
'countBy%s' => 'static int countBy%s(string $value)',
'deleteBy%s' => 'static int deleteBy%s(string $value)',
];
protected static $defaultName = 'sorm:describe';
private $progress;
......@@ -108,6 +118,7 @@ final class DescribeModels extends AbstractCommand
$meta = $model->getTableMetaData();
$properties = [];
$methods = [];
foreach ($meta['fields'] as $field => $info) {
$name = mb_strtolower($field);
......@@ -116,11 +127,13 @@ final class DescribeModels extends AbstractCommand
'type' => $type,
'description' => 'database column',
];
$methods = array_merge($methods, $this->makeMethods($reflection, $field));
if ($alias = array_search($name, $meta['alias_fields'])) {
$properties[$alias] = [
'type' => $type,
'description' => "alias column for {$name}",
];
$methods = array_merge($methods, $this->makeMethods($reflection, $alias));
}
}
......@@ -144,7 +157,7 @@ final class DescribeModels extends AbstractCommand
];
}
if ($this->updateDocBlockOfClass($reflection, $properties)) {
if ($this->updateDocBlockOfClass($reflection, $properties, $methods)) {
$this->outputForFile(
$output,
'<info>Updated ' . $this->relativeFilePath($file->getPathname()) . '</info>'
......@@ -189,7 +202,7 @@ final class DescribeModels extends AbstractCommand
return 'string';
}
private function updateDocBlockOfClass(\ReflectionClass $reflection, array $properties): bool
private function updateDocBlockOfClass(\ReflectionClass $reflection, array $properties, array $methods): bool
{
$has_docblock = (bool) $reflection->getDocComment();
$docblock = $reflection->getDocComment() ?: $this->getDefaultDocblock();
......@@ -220,6 +233,13 @@ final class DescribeModels extends AbstractCommand
array_unshift($properties, ' *');
array_splice($docblock_lines, -1, 0, $properties);
$methods = array_map(function ($method) {
return " * @method {$method}";
}, $methods);
array_unshift($methods, ' *');
array_splice($docblock_lines, -1, 0, $methods);
$new_docblock = implode("\n", $docblock_lines);
if ($docblock === $new_docblock) {
......@@ -282,4 +302,16 @@ final class DescribeModels extends AbstractCommand
}
return null;
}
private function makeMethods(\ReflectionClass $reflection, string $field): array
{
$result = [];
foreach (self::METHODS_TEMPLATES as $name => $template) {
$method = sprintf($name, $field);
if (!$reflection->hasMethod($method)) {
$result[] = sprintf($template, $field);
}
}
return $result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment