Skip to content

Commit 151e0f0

Browse files
committed
feat: create TimeEntry::fromHttpClient() and TimeEntryActivity::fromHttpClient()
1 parent 60e4011 commit 151e0f0

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

src/Redmine/Api/TimeEntry.php

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

33
namespace Redmine\Api;
44

5+
use Redmine\Client\Client;
56
use Redmine\Exception;
67
use Redmine\Exception\MissingParameterException;
78
use Redmine\Exception\SerializerException;
89
use Redmine\Exception\UnexpectedResponseException;
10+
use Redmine\Http\HttpClient;
911
use Redmine\Http\HttpFactory;
1012
use Redmine\Serializer\JsonSerializer;
1113
use Redmine\Serializer\XmlSerializer;
@@ -20,6 +22,37 @@
2022
*/
2123
class TimeEntry extends AbstractApi
2224
{
25+
final public static function fromHttpClient(HttpClient $httpClient): self
26+
{
27+
return new self($httpClient, true);
28+
}
29+
30+
/**
31+
* @deprecated v2.9.0 Use fromHttpClient() instead.
32+
* @see TimeEntry::fromHttpClient()
33+
*
34+
* @param Client|HttpClient $client
35+
*/
36+
public function __construct($client/*, bool $privatelyCalled = false*/)
37+
{
38+
$privatelyCalled = (func_num_args() > 1) ? func_get_arg(1) : false;
39+
40+
if ($privatelyCalled === true) {
41+
parent::__construct($client);
42+
43+
return;
44+
}
45+
46+
if (static::class !== self::class) {
47+
$className = (new \ReflectionClass($this))->isAnonymous() ? '' : ' in `' . static::class . '`';
48+
@trigger_error('Class `' . self::class . '` will declared as final in v3.0.0, stop extending it' . $className . '.', E_USER_DEPRECATED);
49+
} else {
50+
@trigger_error('Method `' . __METHOD__ . '()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `' . self::class . '::fromHttpClient()` instead.', E_USER_DEPRECATED);
51+
}
52+
53+
parent::__construct($client);
54+
}
55+
2356
/**
2457
* List time entries.
2558
*

src/Redmine/Api/TimeEntryActivity.php

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

33
namespace Redmine\Api;
44

5+
use Redmine\Client\Client;
56
use Redmine\Exception;
67
use Redmine\Exception\SerializerException;
78
use Redmine\Exception\UnexpectedResponseException;
9+
use Redmine\Http\HttpClient;
810

911
/**
1012
* Listing time entry activities.
@@ -15,6 +17,11 @@
1517
*/
1618
class TimeEntryActivity extends AbstractApi
1719
{
20+
final public static function fromHttpClient(HttpClient $httpClient): self
21+
{
22+
return new self($httpClient, true);
23+
}
24+
1825
/**
1926
* @var null|array<mixed>
2027
*/
@@ -25,6 +32,32 @@ class TimeEntryActivity extends AbstractApi
2532
*/
2633
private ?array $timeEntryActivityNames = null;
2734

35+
/**
36+
* @deprecated v2.9.0 Use fromHttpClient() instead.
37+
* @see TimeEntryActivity::fromHttpClient()
38+
*
39+
* @param Client|HttpClient $client
40+
*/
41+
public function __construct($client/*, bool $privatelyCalled = false*/)
42+
{
43+
$privatelyCalled = (func_num_args() > 1) ? func_get_arg(1) : false;
44+
45+
if ($privatelyCalled === true) {
46+
parent::__construct($client);
47+
48+
return;
49+
}
50+
51+
if (static::class !== self::class) {
52+
$className = (new \ReflectionClass($this))->isAnonymous() ? '' : ' in `' . static::class . '`';
53+
@trigger_error('Class `' . self::class . '` will declared as final in v3.0.0, stop extending it' . $className . '.', E_USER_DEPRECATED);
54+
} else {
55+
@trigger_error('Method `' . __METHOD__ . '()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `' . self::class . '::fromHttpClient()` instead.', E_USER_DEPRECATED);
56+
}
57+
58+
parent::__construct($client);
59+
}
60+
2861
/**
2962
* List time entry activities.
3063
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Api\TimeEntry;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\TestCase;
7+
use Redmine\Api\TimeEntry;
8+
use Redmine\Http\HttpClient;
9+
10+
#[CoversClass(TimeEntry::class)]
11+
class FromHttpClientTest extends TestCase
12+
{
13+
public function testReturnsCorrectObject(): void
14+
{
15+
$httpClient = $this->createStub(HttpClient::class);
16+
17+
$api = TimeEntry::fromHttpClient($httpClient);
18+
19+
$this->assertInstanceOf(TimeEntry::class, $api);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Api\TimeEntryActivity;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\TestCase;
7+
use Redmine\Api\TimeEntryActivity;
8+
use Redmine\Http\HttpClient;
9+
10+
#[CoversClass(TimeEntryActivity::class)]
11+
class FromHttpClientTest extends TestCase
12+
{
13+
public function testReturnsCorrectObject(): void
14+
{
15+
$httpClient = $this->createStub(HttpClient::class);
16+
17+
$api = TimeEntryActivity::fromHttpClient($httpClient);
18+
19+
$this->assertInstanceOf(TimeEntryActivity::class, $api);
20+
}
21+
}

tests/Unit/Api/TimeEntryActivityTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\TimeEntryActivity;
99
use Redmine\Client\Client;
10+
use Redmine\Http\HttpClient;
1011
use Redmine\Tests\Fixtures\MockClient;
1112

1213
/**
@@ -15,6 +16,44 @@
1516
#[CoversClass(TimeEntryActivity::class)]
1617
class TimeEntryActivityTest extends TestCase
1718
{
19+
public function testExtendingTheClassTriggersDeprecationWarning(): void
20+
{
21+
// PHPUnit 10 compatible way to test trigger_error().
22+
set_error_handler(
23+
function ($errno, $errstr): bool {
24+
$this->assertSame(
25+
'Class `Redmine\Api\TimeEntryActivity` will declared as final in v3.0.0, stop extending it.',
26+
$errstr,
27+
);
28+
29+
restore_error_handler();
30+
return true;
31+
},
32+
E_USER_DEPRECATED,
33+
);
34+
35+
new class ($this->createStub(HttpClient::class)) extends TimeEntryActivity {};
36+
}
37+
38+
public function testConstructorTriggersDeprecationWarning(): void
39+
{
40+
// PHPUnit 10 compatible way to test trigger_error().
41+
set_error_handler(
42+
function ($errno, $errstr): bool {
43+
$this->assertSame(
44+
'Method `Redmine\Api\TimeEntryActivity::__construct()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `Redmine\Api\TimeEntryActivity::fromHttpClient()` instead.',
45+
$errstr,
46+
);
47+
48+
restore_error_handler();
49+
return true;
50+
},
51+
E_USER_DEPRECATED,
52+
);
53+
54+
new TimeEntryActivity($this->createStub(HttpClient::class));
55+
}
56+
1857
/**
1958
* Test all().
2059
*/

tests/Unit/Api/TimeEntryTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\TimeEntry;
99
use Redmine\Client\Client;
10+
use Redmine\Http\HttpClient;
1011
use Redmine\Tests\Fixtures\MockClient;
1112

1213
/**
@@ -15,6 +16,44 @@
1516
#[CoversClass(TimeEntry::class)]
1617
class TimeEntryTest extends TestCase
1718
{
19+
public function testExtendingTheClassTriggersDeprecationWarning(): void
20+
{
21+
// PHPUnit 10 compatible way to test trigger_error().
22+
set_error_handler(
23+
function ($errno, $errstr): bool {
24+
$this->assertSame(
25+
'Class `Redmine\Api\TimeEntry` will declared as final in v3.0.0, stop extending it.',
26+
$errstr,
27+
);
28+
29+
restore_error_handler();
30+
return true;
31+
},
32+
E_USER_DEPRECATED,
33+
);
34+
35+
new class ($this->createStub(HttpClient::class)) extends TimeEntry {};
36+
}
37+
38+
public function testConstructorTriggersDeprecationWarning(): void
39+
{
40+
// PHPUnit 10 compatible way to test trigger_error().
41+
set_error_handler(
42+
function ($errno, $errstr): bool {
43+
$this->assertSame(
44+
'Method `Redmine\Api\TimeEntry::__construct()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `Redmine\Api\TimeEntry::fromHttpClient()` instead.',
45+
$errstr,
46+
);
47+
48+
restore_error_handler();
49+
return true;
50+
},
51+
E_USER_DEPRECATED,
52+
);
53+
54+
new TimeEntry($this->createStub(HttpClient::class));
55+
}
56+
1857
/**
1958
* Test all().
2059
*/

0 commit comments

Comments
 (0)