Skip to content

Commit

Permalink
Added return types for methods in models
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkinbek committed Feb 12, 2024
1 parent c7f7533 commit 9041513
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
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';

$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

0 comments on commit 9041513

Please sign in to comment.