From 0f39bb21c7de58f9c72f2befca8121f3b183e953 Mon Sep 17 00:00:00 2001 From: Svyatoslav V Date: Tue, 21 Jun 2022 14:24:46 +0300 Subject: [PATCH] Added strict params and return types in generated code (#1888) * 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 --- .../Generator/Builder/Om/ObjectBuilder.php | 22 ++++++++++--------- templates/Builder/Om/baseObjectMethodHook.php | 8 +++---- templates/Builder/Om/baseObjectMethods.php | 10 +++++---- .../AggregateColumnsBehaviorTestClasses.php | 2 +- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Propel/Generator/Builder/Om/ObjectBuilder.php b/src/Propel/Generator/Builder/Om/ObjectBuilder.php index 4fa9407511..7a0e56d74f 100644 --- a/src/Propel/Generator/Builder/Om/ObjectBuilder.php +++ b/src/Propel/Generator/Builder/Om/ObjectBuilder.php @@ -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); } @@ -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) { @@ -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()) { @@ -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); @@ -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; } @@ -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) { @@ -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()) { @@ -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 {"; } @@ -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()}(); } diff --git a/templates/Builder/Om/baseObjectMethodHook.php b/templates/Builder/Om/baseObjectMethodHook.php index ad945a8e42..efd73a13e1 100644 --- a/templates/Builder/Om/baseObjectMethodHook.php +++ b/templates/Builder/Om/baseObjectMethodHook.php @@ -5,7 +5,7 @@ * @param ConnectionInterface|null $con * @return bool */ - public function preSave(?ConnectionInterface $con = null) + public function preSave(?ConnectionInterface $con = null): bool { if (is_callable('parent::preSave')) { @@ -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 { if (is_callable('parent::preInsert')) { @@ -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 { if (is_callable('parent::preUpdate')) { @@ -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 { if (is_callable('parent::preDelete')) { diff --git a/templates/Builder/Om/baseObjectMethods.php b/templates/Builder/Om/baseObjectMethods.php index 2c496c9c8e..f86caadeb9 100644 --- a/templates/Builder/Om/baseObjectMethods.php +++ b/templates/Builder/Om/baseObjectMethods.php @@ -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; } @@ -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; } /** @@ -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 */ - public function __sleep() + public function __sleep(): array { $this->clearAllReferences(); diff --git a/tests/Propel/Tests/Generator/Behavior/AggregateColumn/AggregateColumnsBehaviorTestClasses.php b/tests/Propel/Tests/Generator/Behavior/AggregateColumn/AggregateColumnsBehaviorTestClasses.php index 7c3db6a561..9b3ee16092 100644 --- a/tests/Propel/Tests/Generator/Behavior/AggregateColumn/AggregateColumnsBehaviorTestClasses.php +++ b/tests/Propel/Tests/Generator/Behavior/AggregateColumn/AggregateColumnsBehaviorTestClasses.php @@ -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 {