Skip to content

Commit

Permalink
Allow dynamic properties on CommonITILObject
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and trasher committed Sep 7, 2023
1 parent 870ebf2 commit 9b5485e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/CommonITILObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ public function __set(string $property_name, $value)
// TODO 10.1: throw exception instead
trigger_error("Readonly field: '$property_name'", E_USER_WARNING);
break;

default:
if (version_compare(PHP_VERSION, '8.2.0', '<')) {
// Trigger same deprecation notice as the one triggered by PHP 8.2+
trigger_error(
sprintf('Creation of dynamic property %s::$%s is deprecated', get_called_class(), $property_name),
E_USER_DEPRECATED
);
}
$this->$property_name = $value;
break;
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/functional/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6328,4 +6328,21 @@ public function testActorsMagicProperties(
$this->array($simplied_actors[Group::class])->isEqualTo($expected_groups);
$this->array($simplied_actors[Supplier::class])->isEqualTo($expected_suppliers);
}

public function testDynamicProperties(): void
{
$ticket = new \Ticket();

$this->when(
function () use ($ticket) {
$ticket->plugin_xxx_data = 'test';
}
)
->error
->withMessage('Creation of dynamic property Ticket::$plugin_xxx_data is deprecated')
->exists();

$this->boolean(property_exists($ticket, 'plugin_xxx_data'))->isTrue();
$this->string($ticket->plugin_xxx_data)->isEqualTo('test');
}
}

0 comments on commit 9b5485e

Please sign in to comment.