Skip to content

Commit

Permalink
Added strict params and return types in generated code (#1888)
Browse files Browse the repository at this point in the history
* Added toString return type

* Added return type for baseObjectMethodHook tpl

* Added return type for baseObjectMethods tpl

* Fixed return type

* Added typehint for prune method

* Reverted typehint

* Added return type for save method

* Added return type hint for getPrimeryKey method

* Reverted return type hint for getPrimeryKey method

* Added void return types for some generated methods

* Added typehint for generated method setPrimaryKey parm

* Added typehint for doSave generated method

* Fixed issues

* Fixed issues with setPrimearyKey generator code

* Set typehint for setter

* Fixed typehint for geenerated setters

* Reverted typehint for generated setters
  • Loading branch information
geega authored Jun 21, 2022
1 parent e6c147f commit 0f39bb2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
22 changes: 12 additions & 10 deletions src/Propel/Generator/Builder/Om/ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3927,10 +3927,10 @@ protected function addSetPrimaryKeySinglePK(string &$script): void
/**
* Generic method to set the primary key ($clo column).
*
* @param $ctype \$key Primary key.
* @param $ctype|null \$key Primary key.
* @return void
*/
public function setPrimaryKey(\$key): void
public function setPrimaryKey(?$ctype \$key = null): void
{
\$this->set" . $col->getPhpName() . "(\$key);
}
Expand All @@ -3953,7 +3953,7 @@ protected function addSetPrimaryKeyMultiPK(string &$script): void
* @param array \$keys The elements of the composite key (order must match the order in XML file).
* @return void
*/
public function setPrimaryKey(\$keys): void
public function setPrimaryKey(array \$keys): void
{";
$i = 0;
foreach ($this->getTable()->getPrimaryKey() as $pk) {
Expand Down Expand Up @@ -4463,7 +4463,7 @@ protected function addInitRelations(string &$script, array $referrers): void
* @param string \$relationName The name of the relation to initialize
* @return void
*/
public function initRelation(\$relationName)
public function initRelation(\$relationName): void
{";
foreach ($referrers as $refFK) {
if (!$refFK->isLocalPrimaryKey()) {
Expand Down Expand Up @@ -4836,7 +4836,7 @@ protected function addRefFKDoAdd(string &$script, ForeignKey $refFK): void
/**
* @param {$className} \${$lowerRelatedObjectClassName} The $className object to add.
*/
protected function doAdd{$relatedObjectClassName}($className \${$lowerRelatedObjectClassName})
protected function doAdd{$relatedObjectClassName}($className \${$lowerRelatedObjectClassName}): void
{
\$this->{$collName}[]= \${$lowerRelatedObjectClassName};
\${$lowerRelatedObjectClassName}->set" . $this->getFKPhpNameAffix($refFK, false) . "(\$this);
Expand Down Expand Up @@ -5377,8 +5377,10 @@ protected function addRefFKPartial(string &$script, ForeignKey $refFK): void
$script .= "
/**
* Reset is the $collName collection loaded partially.
*
* @return void
*/
public function resetPartial{$relCol}(\$v = true)
public function resetPartial{$relCol}(\$v = true): void
{
\$this->{$collName}Partial = \$v;
}
Expand Down Expand Up @@ -6296,7 +6298,7 @@ protected function addDoSave(string &$script): void
* @throws \Propel\Runtime\Exception\PropelException
* @see save()
*/
protected function doSave(ConnectionInterface \$con" . ($reloadOnUpdate || $reloadOnInsert ? ', $skipReload = false' : '') . ")
protected function doSave(ConnectionInterface \$con" . ($reloadOnUpdate || $reloadOnInsert ? ', $skipReload = false' : '') . "): int
{
\$affectedRows = 0; // initialize var to track total num of affected rows
if (!\$this->alreadyInSave) {
Expand Down Expand Up @@ -6438,7 +6440,7 @@ protected function addDoInsert(): string
* @throws \Propel\Runtime\Exception\PropelException
* @see doSave()
*/
protected function doInsert(ConnectionInterface \$con)
protected function doInsert(ConnectionInterface \$con): void
{";
if ($this->getPlatform() instanceof MssqlPlatform) {
if ($table->hasAutoIncrementPrimaryKey()) {
Expand Down Expand Up @@ -6793,7 +6795,7 @@ protected function addSaveOpen(string &$script): void
$reloadOnUpdate = $table->isReloadOnUpdate();
$reloadOnInsert = $table->isReloadOnInsert();
$script .= "
public function save(?ConnectionInterface \$con = null" . ($reloadOnUpdate || $reloadOnInsert ? ', $skipReload = false' : '') . ")
public function save(?ConnectionInterface \$con = null" . ($reloadOnUpdate || $reloadOnInsert ? ', $skipReload = false' : '') . "): int
{";
}

Expand Down Expand Up @@ -7285,7 +7287,7 @@ protected function addPrimaryString(string &$script): void
*
* @return string The value of the '{$column->getName()}' column
*/
public function __toString()
public function __toString(): string
{
return (string)\$this->get{$column->getPhpName()}();
}
Expand Down
8 changes: 4 additions & 4 deletions templates/Builder/Om/baseObjectMethodHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param ConnectionInterface|null $con
* @return bool
*/
public function preSave(?ConnectionInterface $con = null)
public function preSave(?ConnectionInterface $con = null): bool
{
<?php if ($hasBaseClass) : ?>
if (is_callable('parent::preSave')) {
Expand Down Expand Up @@ -38,7 +38,7 @@ public function postSave(?ConnectionInterface $con = null): void
* @param ConnectionInterface|null $con
* @return bool
*/
public function preInsert(?ConnectionInterface $con = null)
public function preInsert(?ConnectionInterface $con = null): bool
{
<?php if ($hasBaseClass) : ?>
if (is_callable('parent::preInsert')) {
Expand Down Expand Up @@ -71,7 +71,7 @@ public function postInsert(?ConnectionInterface $con = null): void
* @param ConnectionInterface|null $con
* @return bool
*/
public function preUpdate(?ConnectionInterface $con = null)
public function preUpdate(?ConnectionInterface $con = null): bool
{
<?php if ($hasBaseClass) : ?>
if (is_callable('parent::preUpdate')) {
Expand Down Expand Up @@ -104,7 +104,7 @@ public function postUpdate(?ConnectionInterface $con = null): void
* @param ConnectionInterface|null $con
* @return bool
*/
public function preDelete(?ConnectionInterface $con = null)
public function preDelete(?ConnectionInterface $con = null): bool
{
<?php if ($hasBaseClass) : ?>
if (is_callable('parent::preDelete')) {
Expand Down
10 changes: 6 additions & 4 deletions templates/Builder/Om/baseObjectMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function isNew(): bool
*
* @param bool $b the state of the object.
*/
public function setNew(bool $b)
public function setNew(bool $b): void
{
$this->new = $b;
}
Expand All @@ -66,9 +66,9 @@ public function isDeleted(): bool
* @param bool $b The deleted state of this object.
* @return void
*/
public function setDeleted($b): void
public function setDeleted(bool $b): void
{
$this->deleted = (boolean) $b;
$this->deleted = $b;
}

/**
Expand Down Expand Up @@ -200,8 +200,10 @@ public function exportTo($parser, bool $includeLazyLoadColumns = true, string $k
/**
* Clean up internal collections prior to serializing
* Avoids recursive loops that turn into segmentation faults when serializing
*
* @return array<string>
*/
public function __sleep()
public function __sleep(): array
{
$this->clearAllReferences();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestableComment extends AggregateComment
{
// overrides the parent save() to bypass behavior hooks

public function save(?ConnectionInterface $con = null)
public function save(?ConnectionInterface $con = null): int
{
$con->beginTransaction();
try {
Expand Down

0 comments on commit 0f39bb2

Please sign in to comment.