Skip to content
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

Added return types for methods in models #547

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ public function generate()
'relationsClassHints' => $this->generateRelationsClassHints($tableRelations, $this->generateQuery),
'enum' => $this->getEnum($tableSchema->columns),
];

$template = phpversion() < '7' ? 'model.php': 'model_from_7.php';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$template = phpversion() < '7' ? 'model.php': 'model_from_7.php';
$template = phpversion() < '7' ? 'model.php': 'model_with_types.php';


$files[] = new CodeFile(
Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $modelClassName . '.php',
$this->render('model.php', $params)
$this->render($template, $params)
);

// query:
Expand Down
165 changes: 165 additions & 0 deletions src/generators/model/default/model_from_7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
/**
* This is the template for generating the model class of a specified table.
*/

/** @var $enum array list of ENUM fields */
/** @var yii\web\View $this */
/** @var yii\gii\generators\model\Generator $generator */
/** @var string $tableName full table name */
/** @var string $className class name */
/** @var string $queryClassName query class name */
/** @var yii\db\TableSchema $tableSchema */
/** @var array $properties list of properties (property => [type, name. comment]) */
/** @var string[] $labels list of attribute labels (name => label) */
/** @var string[] $rules list of validation rules */
/** @var array $relations list of relations (name => relation declaration) */

echo "<?php\n";
?>

namespace <?= $generator->ns ?>;

use \yii\db\Connection;
/**
* This is the model class for table "<?= $generator->generateTableName($tableName) ?>".
*
<?php foreach ($properties as $property => $data): ?>
* @property <?= "{$data['type']} \${$property}" . ($data['comment'] ? ' ' . strtr($data['comment'], ["\n" => ' ']) : '') . "\n" ?>
<?php endforeach; ?>
<?php if (!empty($relations)): ?>
*
<?php foreach ($relations as $name => $relation): ?>
* @property <?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?>
<?php endforeach; ?>
<?php endif; ?>
*/
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
{

<?php if (!empty($enum)): ?>
/**
* ENUM field values
*/
<?php
foreach($enum as $columnName => $columnData) {
foreach ($columnData['values'] as $enumValue){
echo ' const ' . $enumValue['constName'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL;
}
}
endif
?>

/**
* {@inheritdoc}
*/
public static function tableName() : string
{
return '<?= $generator->generateTableName($tableName) ?>';
}
<?php if ($generator->db !== 'db'): ?>

/**
* @return Connection the database connection used by this AR class.
*/
public static function getDb() : Connection
{
return Yii::$app->get('<?= $generator->db ?>');
}
<?php endif; ?>

/**
* {@inheritdoc}
*/
public function rules() : array
{
return [<?= empty($rules) ? '' : ("\n " . implode(",\n ", $rules) . ",\n ") ?>];
}

/**
* {@inheritdoc}
*/
public function attributeLabels() : array
{
return [
<?php foreach ($labels as $name => $label): ?>
<?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?php endforeach; ?>
];
}
<?php foreach ($relations as $name => $relation): ?>

/**
* Gets query for [[<?= $name ?>]].
*
* @return <?= $relationsClassHints[$name] . "\n" ?>
*/
public function get<?= $name ?>() : <?= $relationsClassHints[$name] ?>
{
<?= $relation[0] . "\n" ?>
}
<?php endforeach; ?>
<?php if ($queryClassName): ?>
<?php
$queryClassFullName = ($generator->ns === $generator->queryNs) ? $queryClassName : '\\' . $generator->queryNs . '\\' . $queryClassName;
echo "\n";
?>
/**
* {@inheritdoc}
* @return <?= $queryClassFullName ?> the active query used by this AR class.
*/
public static function find() : <?= $queryClassFullName ?>
{
return new <?= $queryClassFullName ?>(get_called_class());
}
<?php endif; ?>

<?php if ($enum): ?>
<?php foreach ($enum as $columnName => $columnData): ?>

/**
* column <?= $columnName ?> ENUM value labels
* @return string[]
*/
public static function <?= $columnData['funcOptsName'] ?>() : array
{
return [
<?php foreach ($columnData['values'] as $k => $value): ?>
<?php
if ($generator->enableI18N) {
echo ' self::' . $value['constName'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n";
} else {
echo ' self::' . $value['constName'] . ' => \'' . $value['value'] . "',\n";
}
?>
<?php endforeach; ?>
];
}
<?php endforeach; ?>
<?php foreach ($enum as $columnName => $columnData): ?>

/**
* @return string
*/
public function <?= $columnData['displayFunctionPrefix'] ?>() : string
{
return self::<?= $columnData['funcOptsName'] ?>()[$this-><?=$columnName?>];
}
<?php foreach ($columnData['values'] as $enumValue): ?>

/**
* @return bool
*/
public function <?= $columnData['isFunctionPrefix'] . $enumValue['functionSuffix'] ?>() : bool
{
return $this-><?= $columnName ?> === self::<?= $enumValue['constName'] ?>;
}

public function <?= $columnData['setFunctionPrefix'] . $enumValue['functionSuffix'] ?>()
{
$this-><?= $columnName ?> = self::<?= $enumValue['constName'] ?>;
}
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
}
4 changes: 3 additions & 1 deletion tests/generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ public function testEnum()
'relationsClassHints' => [],
'enum' => $generator->getEnum($tableSchema->columns),
];
$codeFile = $generator->render('model.php', $params);

$template = phpversion() < '7' ? 'model.php': 'model_from_7.php';
$codeFile = $generator->render($template, $params);

/**
* Fix class code for eval - remove ?php, namespace and use Yii
Expand Down
Loading