Skip to content

Feature: Add comments in columns PHPDoc. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/Services/Generators/ColumnsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
use Doctrine\DBAL\Schema\Column;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model;
use ReflectionClass;

class ColumnsGenerator implements PhpDocGeneratorContract
{

protected AbstractSchemaManager $schema;
protected Model $model;

public function __construct(
Connection $databaseConnection
) {
)
{
$this->schema = $databaseConnection->getDoctrineSchemaManager();
}

Expand All @@ -30,10 +33,11 @@ public function generate(Model $model, array $options = []): string
// columns
foreach ($columns as $column) {
$phpDocStr .= sprintf(
'%s * @property %s %s',
'%s * @property %s %s %s',
"\n",
$this->resolveColumnType($column),
'$' . $column->getName()
'$' . $column->getName(),
$column->getComment(),
);
}

Expand All @@ -50,27 +54,34 @@ protected function resolveColumnType(Column $column): string
{
$columnType = strtolower($column->getType()->getName());

$type = match ($columnType) {
'int', 'smallint', 'tinyint',
'mediumint', 'bigint', 'integer' => 'int',
'float', 'double', 'decimal', 'dec' => 'float',
$cast = $this->model->getCasts()[$column->getName()] ?? null;
if (class_exists($cast) && (new ReflectionClass($cast))->isEnum()) {
$type = "\\${cast}";
}

if (!isset($type)) {
$type = match ($columnType) {
'int', 'smallint', 'tinyint',
'mediumint', 'bigint', 'integer' => 'int',
'float', 'double', 'decimal', 'dec' => 'float',

'bool', 'boolean' => 'bool',
'bool', 'boolean' => 'bool',

// would be string if you don't add 'casts'
'date', 'datetime', 'timestamp', 'time', 'year' => $this->hasDateCasting($column->getName())
? '\Carbon\Carbon'
: 'string',
// would be string if you don't add 'casts'
'date', 'datetime', 'timestamp', 'time', 'year' => $this->hasDateCasting($column->getName())
? '\Carbon\Carbon'
: 'string',

'char', 'string', 'varchar',
'text', 'tinytext', 'mediumtext',
'longtext', 'enum', 'binary',
'varbinary', 'set', 'json', 'jsonb' => $this->getJsonCastType($column->getName()),
// ^ because sqlite doesn't have json, they will use `text` but Eloquent can parse text to particular cast
'char', 'string', 'varchar',
'text', 'tinytext', 'mediumtext',
'longtext', 'enum', 'binary',
'varbinary', 'set', 'json', 'jsonb' => $this->getJsonCastType($column->getName()),
// ^ because sqlite doesn't have json, they will use `text` but Eloquent can parse text to particular cast

// would be string if you don't add 'casts', default to array
default => 'mixed',
};
// would be string if you don't add 'casts', default to array
default => 'mixed',
};
}

if (!$column->getNotnull()) {
$type .= '|null';
Expand Down Expand Up @@ -104,4 +115,5 @@ protected function getJsonCastType(string $column): string

return 'string';
}
}

}