Skip to content

Commit 7e3e7a3

Browse files
Fixes deprecation notices about dynamic properties creation (#106)
* Fixes that deprecation notices about dynamic properties creation are issued when used with PHP8.2 and greater. Dynamic property creation has been deprecated in PHP8.2 and are likely to be removed from PHP in an upcoming version. Since JIRA allows to create custom fields, this will lead to errors in upcoming PHP versions. Thus a trait was added that allows to create dynamic properties using magic method `__set`, retrieve them via magic method `__get` and check their existance via magic method `__isset`. The trait was only assigned to a couple of classes yet but can be assigned whenever the need arises. * Fixes code style issues
1 parent 1fa9580 commit 7e3e7a3

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

src/DynamicPropertiesTrait.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JiraCloud;
6+
7+
/**
8+
* Defines a trait that allows to dynamically assign properties to objects as this has been deprecated in PHP8.2.
9+
*/
10+
trait DynamicPropertiesTrait
11+
{
12+
/** @var array<string, mixed> */
13+
protected array $dynamicProperties = [];
14+
15+
/**
16+
* Attempts to retrieve a dynamic property from {@link static::$dynamicProperties}.
17+
*
18+
* @param string $name
19+
*
20+
* @return mixed The requested value if found, `null` otherwise.
21+
*/
22+
public function __get(string $name): mixed
23+
{
24+
return $this->dynamicProperties[$name] ?? null;
25+
}
26+
27+
/**
28+
* Returns whether the dynamic property `$name` is set (not `null`!) in {@link static::$dynamicProperties}.
29+
*
30+
* @param string $name
31+
*
32+
* @return bool
33+
*/
34+
public function __isset(string $name): bool
35+
{
36+
return isset($this->dynamicProperties[$name]);
37+
}
38+
39+
/**
40+
* Applies `$value` using `$name` as key on {@link static::$dynamicProperties}.
41+
*
42+
* @param string $name
43+
* @param mixed $value
44+
*
45+
* @return void
46+
*/
47+
public function __set(string $name, mixed $value): void
48+
{
49+
$this->dynamicProperties[$name] = $value;
50+
}
51+
52+
/**
53+
* Accessor for {@link static::$dynamicProperties}.
54+
*
55+
* @return array
56+
*/
57+
public function getDynamicProperties(): array
58+
{
59+
return $this->dynamicProperties;
60+
}
61+
}

src/Issue/History.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace JiraCloud\Issue;
44

5+
use JiraCloud\DynamicPropertiesTrait;
6+
57
/**
68
* ChangeLog History.
79
*
810
* Class History
911
*/
1012
class History implements \JsonSerializable
1113
{
14+
use DynamicPropertiesTrait;
15+
1216
public int $id;
1317

1418
public Reporter $author;

src/Issue/Issue.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace JiraCloud\Issue;
44

5+
use JiraCloud\DynamicPropertiesTrait;
6+
57
class Issue implements \JsonSerializable
68
{
9+
use DynamicPropertiesTrait;
10+
711
/**
812
* return only if Project query by key(not id).
913
*/

src/Issue/IssueSearchResult.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace JiraCloud\Issue;
44

5+
use JiraCloud\DynamicPropertiesTrait;
6+
57
/**
68
* Issue search result.
79
*/
810
class IssueSearchResult
911
{
12+
use DynamicPropertiesTrait;
13+
1014
/**
1115
* @var string|null
1216
*/

src/Issue/Visibility.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace JiraCloud\Issue;
44

5+
use JiraCloud\DynamicPropertiesTrait;
6+
57
class Visibility implements \JsonSerializable
68
{
9+
use DynamicPropertiesTrait;
10+
711
private string $type;
812
private string $value;
913

src/Sprint/Sprint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace JiraCloud\Sprint;
66

7+
use JiraCloud\DynamicPropertiesTrait;
78
use JiraCloud\JsonSerializableTrait;
89

910
class Sprint implements \JsonSerializable
1011
{
1112
use JsonSerializableTrait;
13+
use DynamicPropertiesTrait;
1214

1315
public string $self;
1416

0 commit comments

Comments
 (0)