♻️ Rename boolean getters/setters for data models

To make the function names more semantic
This commit is contained in:
Philipp Stracker 2024-10-30 18:52:10 +01:00
parent 6e3664ade8
commit e28b6e35aa
No known key found for this signature in database
2 changed files with 34 additions and 6 deletions

View file

@ -89,10 +89,38 @@ abstract class AbstractDataModel {
continue;
}
$setter = "set_$key";
if ( method_exists( $this, $setter ) ) {
$setter = $this->get_setter_name( $key );
if ( $setter && method_exists( $this, $setter ) ) {
$this->$setter( $value );
}
}
}
/**
* Generates a setter method name for a given key, stripping the prefix from
* boolean fields (is_, use_, has_).
*
* @param int|string $field_key The key for which to generate a setter name.
*
* @return string The generated setter method name.
*/
private function get_setter_name( $field_key ) : string {
if ( ! is_string( $field_key ) ) {
return '';
}
$prefixes_to_strip = array( 'is_', 'use_', 'has_' );
$stripped_key = $field_key;
foreach ( $prefixes_to_strip as $prefix ) {
if ( str_starts_with( $field_key, $prefix ) ) {
$stripped_key = substr( $field_key, strlen( $prefix ) );
break;
}
}
return $stripped_key ? "set_$stripped_key" : '';
}
}

View file

@ -116,7 +116,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @return bool
*/
public function get_use_sandbox() : bool {
public function get_sandbox() : bool {
return (bool) $this->data['use_sandbox'];
}
@ -125,7 +125,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @param bool $use_sandbox Whether to use sandbox mode.
*/
public function set_use_sandbox( bool $use_sandbox ) : void {
public function set_sandbox( bool $use_sandbox ) : void {
$this->data['use_sandbox'] = $use_sandbox;
}
@ -134,7 +134,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @return bool
*/
public function get_use_manual_connection() : bool {
public function get_manual_connection() : bool {
return (bool) $this->data['use_manual_connection'];
}
@ -143,7 +143,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @param bool $use_manual_connection Whether to use manual connection.
*/
public function set_use_manual_connection( bool $use_manual_connection ) : void {
public function set_manual_connection( bool $use_manual_connection ) : void {
$this->data['use_manual_connection'] = $use_manual_connection;
}