Skip to content

Commit aa311b4

Browse files
author
Daniel Kurowski
committed
support description as well
1 parent cb988c2 commit aa311b4

7 files changed

+114
-17
lines changed

src/Capabilities/Deprecated.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ final class Deprecated implements Capability
1414
* @param class-string $replacement
1515
*/
1616
public function __construct(
17-
private string $replacement,
17+
private ?string $description = null,
18+
private ?string $replacement = null,
1819
) {}
1920

2021

@@ -24,10 +25,17 @@ public function applyTo(
2425
?ClassInNamespace $current,
2526
): void
2627
{
27-
$draft->getClassType()->addComment(sprintf(
28-
"@deprecated use {@see %s} instead",
29-
$this->replacement,
30-
));
28+
$annotation = '@deprecated';
29+
if ($this->description !== null) {
30+
$annotation .= ' ' . $this->description;
31+
}
32+
33+
if ($this->replacement !== null) {
34+
$annotation .= $this->description !== null ? ', ' : ' ';
35+
$annotation .= sprintf('use {@see %s} instead', $this->replacement);
36+
}
37+
38+
$draft->getClassType()->addComment($annotation);
3139
}
3240

3341
}

src/Capabilities/functions.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ function constructorWithPromotedProperties(): ConstructorWithPromotedProperties
1111
/**
1212
* @param class-string $replacement
1313
*/
14-
function deprecated(string $replacement): Deprecated {
15-
return new Deprecated($replacement);
14+
function deprecated(
15+
?string $description = null,
16+
?string $replacement = null,
17+
): Deprecated {
18+
return new Deprecated($description, $replacement);
1619
}
1720

1821
function getters(): Getters {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Do not edit. This is generated file. Modify definition file instead.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
/**
10+
* @deprecated
11+
*/
12+
final class ClassName
13+
{
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Do not edit. This is generated file. Modify definition file instead.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
/**
10+
* @deprecated will be removed in v4.0.0, use {@see Grifart\ClassScaffolder\Test\Capabilities\Deprecated\Replacement} instead
11+
*/
12+
final class ClassName
13+
{
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Do not edit. This is generated file. Modify definition file instead.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
/**
10+
* @deprecated will be removed in v4.0.0
11+
*/
12+
final class ClassName
13+
{
14+
}

tests/Capabilities/Deprecated/DeprecatedTest.expected.phps renamed to tests/Capabilities/Deprecated/DeprecatedTest.expected.withReplacement.phps

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
declare(strict_types=1);
88

9-
namespace RootNamespace\SubNamespace;
10-
11-
use Iterator;
12-
139
/**
1410
* @deprecated use {@see Grifart\ClassScaffolder\Test\Capabilities\Deprecated\Replacement} instead
1511
*/

tests/Capabilities/Deprecated/DeprecatedTest.phpt

+54-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,67 @@
22

33
namespace Grifart\ClassScaffolder\Test\Capabilities\Deprecated;
44

5-
use Grifart\ClassScaffolder\Test\Capabilities\CapabilityTestCase;
5+
use Grifart\ClassScaffolder\ClassGenerator;
6+
use Grifart\ClassScaffolder\Definition\ClassDefinition;
7+
use Tester\Assert;
8+
use Tester\TestCase;
69
use function Grifart\ClassScaffolder\Capabilities\deprecated;
10+
use function Grifart\ClassScaffolder\Definition\definitionOf;
711

812

913
require __DIR__ . '/../../bootstrap.php';
1014

11-
final class DeprecatedTest extends CapabilityTestCase
15+
final class DeprecatedTest extends TestCase
1216
{
13-
protected function getCapabilities(): array
17+
private function doAssert(
18+
ClassDefinition $definition,
19+
string $expectedFilePath,
20+
): void
1421
{
15-
return [
16-
deprecated(Replacement::class),
17-
];
22+
$generator = new ClassGenerator();
23+
$phpFile = $generator->generateClass($definition);
24+
$code = (string) $phpFile;
25+
26+
Assert::matchFile($expectedFilePath, $code);
27+
}
28+
29+
public function testWithoutParams(): void
30+
{
31+
$this->doAssert(
32+
definition: definitionOf('ClassName')
33+
->with(deprecated()),
34+
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.onlyAnnotation.phps',
35+
);
36+
}
37+
38+
public function testWithDescription(): void
39+
{
40+
$this->doAssert(
41+
definition: definitionOf('ClassName')
42+
->with(deprecated(description: 'will be removed in v4.0.0')),
43+
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withDescription.phps',
44+
);
45+
}
46+
47+
public function testWithReplacement(): void
48+
{
49+
$this->doAssert(
50+
definition: definitionOf('ClassName')
51+
->with(deprecated(replacement: Replacement::class)),
52+
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withReplacement.phps',
53+
);
54+
}
55+
56+
public function testWithAll(): void
57+
{
58+
$this->doAssert(
59+
definition: definitionOf('ClassName')
60+
->with(deprecated(
61+
description: 'will be removed in v4.0.0',
62+
replacement: Replacement::class,
63+
)),
64+
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withAll.phps',
65+
);
1866
}
1967
}
2068

0 commit comments

Comments
 (0)