Switches instead of normal ifs and elseifs #6018

Closed
opened 2026-01-22 15:24:51 +01:00 by admin · 2 comments
Owner

Originally created by @ZielonyBuszmen on GitHub (Jul 16, 2018).

Originally assigned to: @ostrolucky on GitHub.

Q A
Version 2.5

Support Question

I have small question about code and architecture.

In source code I found odd switches like that: https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L399

switch (true) {
	case isset($this->class->fieldNames[$columnName]):
		$fieldName  = $this->class->fieldNames[$columnName];
		$column     = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform);
		if (isset($this->class->fieldMappings[$fieldName]['requireSQLConversion'])) {
			$type        = Type::getType($this->columnTypes[$columnName]);
			$placeholder = $type->convertToDatabaseValueSQL('?', $this->platform);
		}
		break;
	case isset($this->quotedColumns[$columnName]):
		$column = $this->quotedColumns[$columnName];
		break;
}

In newer version on master (3.0) there are similar occurences of switch, but a bit more civilized. Example:
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L1533

 switch (true) {
	case ($property instanceof VersionFieldMetadata):
		// Do nothing
		break;
	case ($property instanceof LocalColumnMetadata):
		if (($property instanceof FieldMetadata
			...

Why do you use that tricky code? Is it faster than normal if()..elseif()?

Originally created by @ZielonyBuszmen on GitHub (Jul 16, 2018). Originally assigned to: @ostrolucky on GitHub. | Q | A |------------ | ----- | Version | 2.5 ### Support Question I have small question about code and architecture. In source code I found odd switches like that: https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L399 ``` switch (true) { case isset($this->class->fieldNames[$columnName]): $fieldName = $this->class->fieldNames[$columnName]; $column = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform); if (isset($this->class->fieldMappings[$fieldName]['requireSQLConversion'])) { $type = Type::getType($this->columnTypes[$columnName]); $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform); } break; case isset($this->quotedColumns[$columnName]): $column = $this->quotedColumns[$columnName]; break; } ``` In newer version on master (3.0) there are similar occurences of switch, but a bit more civilized. Example: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L1533 ``` switch (true) { case ($property instanceof VersionFieldMetadata): // Do nothing break; case ($property instanceof LocalColumnMetadata): if (($property instanceof FieldMetadata ... ``` Why do you use that tricky code? Is it faster than normal if()..elseif()?
admin closed this issue 2026-01-22 15:24:52 +01:00
Author
Owner

@ostrolucky commented on GitHub (Aug 5, 2018):

I'm sorry but I have to close this, as this is not an actual issue. Drop the question in one of our community channels.

@ostrolucky commented on GitHub (Aug 5, 2018): I'm sorry but I have to close this, as this is not an actual issue. Drop the question in one of our [community channels](https://www.doctrine-project.org/community/).
Author
Owner

@Ocramius commented on GitHub (Aug 5, 2018):

I think this was just a leftover - already fixed in master IIRC

@Ocramius commented on GitHub (Aug 5, 2018): I think this was just a leftover - already fixed in `master` IIRC
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6018