Skip to content

Commit 11a5bc2

Browse files
committed
Cleanup inline node constructors signature
The `string $content` parameter is a legacy from before Markdown. It is redundant and replaced by `array $children`.
1 parent c81430b commit 11a5bc2

File tree

7 files changed

+115
-22
lines changed

7 files changed

+115
-22
lines changed

packages/guides/src/Nodes/Inline/AbstractLinkInlineNode.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use Doctrine\Deprecations\Deprecation;
1717
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
1818

19+
use function func_get_arg;
20+
use function func_num_args;
21+
use function is_string;
22+
1923
abstract class AbstractLinkInlineNode extends InlineCompoundNode implements LinkInlineNode
2024
{
2125
use BCInlineNodeBehavior;
@@ -26,16 +30,24 @@ abstract class AbstractLinkInlineNode extends InlineCompoundNode implements Link
2630
public function __construct(
2731
private readonly string $type,
2832
private readonly string $targetReference,
29-
string $value = '',
30-
array $children = [],
33+
string|array $children = [],
3134
) {
32-
if (empty($children)) {
35+
if (is_string($children)) {
3336
Deprecation::trigger(
3437
'phpdocumentor/guides',
3538
'https://github.com/phpDocumentor/guides/issues/1161',
36-
'Please provide the children as an array of InlineNodeInterface instances instead of a string.',
39+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: string $type, string $targetReference, array $children',
40+
static::class,
3741
);
38-
$children = [new PlainTextInlineNode($value)];
42+
43+
if (func_num_args() < 4) {
44+
// compat with (string $type, string $targetReference, string $value) signature
45+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
46+
} else {
47+
// compat with (string $type, string $targetReference, string $value, array $children = []) signature
48+
/** @var InlineNodeInterface[] $children */
49+
$children = func_get_arg(3);
50+
}
3951
}
4052

4153
parent::__construct($children);

packages/guides/src/Nodes/Inline/BCInlineNodeBehavior.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function getValue(): string
2424
Deprecation::trigger(
2525
'phpdocumentor/guides',
2626
'https://github.com/phpDocumentor/guides/issues/1161',
27-
'Use getChildren to access the value of this node.',
27+
'Use getChildren to access the value of %s.',
28+
static::class,
2829
);
2930

3031
return $this->toString();
@@ -34,12 +35,13 @@ public function getValue(): string
3435
public function setValue(mixed $value): void
3536
{
3637
if (is_string($value)) {
37-
$value = [new PlainTextInlineNode($value)];
38+
$value = $value === '' ? [] : [new PlainTextInlineNode($value)];
3839

3940
Deprecation::trigger(
4041
'phpdocumentor/guides',
4142
'https://github.com/phpDocumentor/guides/issues/1161',
42-
'Please provide the children as an array of InlineNodeInterface instances instead of a string.',
43+
'Passing a string to %s is deprecated, pass an array of InlineNodeInterface instances instead.',
44+
__METHOD__,
4345
);
4446
}
4547

packages/guides/src/Nodes/Inline/DocReferenceNode.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
namespace phpDocumentor\Guides\Nodes\Inline;
1515

16+
use Doctrine\Deprecations\Deprecation;
17+
1618
use function array_merge;
19+
use function is_string;
1720

1821
/**
1922
* Represents a link to document
@@ -29,12 +32,24 @@ final class DocReferenceNode extends AbstractLinkInlineNode implements CrossRefe
2932
{
3033
final public const TYPE = 'doc';
3134

35+
/** @param InlineNodeInterface[] $children */
3236
public function __construct(
3337
string $targetDocument,
34-
string $value = '',
38+
string|array $children = [],
3539
private readonly string $interlinkDomain = '',
3640
) {
37-
parent::__construct(self::TYPE, $targetDocument, $value);
41+
if (is_string($children)) {
42+
Deprecation::trigger(
43+
'phpdocumentor/guides',
44+
'https://github.com/phpDocumentor/guides/issues/1161',
45+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: string $targetDocument, array $children, string $interlinkDomain',
46+
static::class,
47+
);
48+
49+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
50+
}
51+
52+
parent::__construct(self::TYPE, $targetDocument, $children);
3853
}
3954

4055
public function getInterlinkDomain(): string

packages/guides/src/Nodes/Inline/EmphasisInlineNode.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
use Doctrine\Deprecations\Deprecation;
1717
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
1818

19+
use function func_get_arg;
20+
use function func_num_args;
21+
use function is_string;
22+
1923
final class EmphasisInlineNode extends InlineCompoundNode
2024
{
2125
use BCInlineNodeBehavior;
2226

2327
public const TYPE = 'emphasis';
2428

2529
/** @param InlineNodeInterface[] $children */
26-
public function __construct(string $value, array $children = [])
30+
public function __construct(string|array $children = [])
2731
{
28-
if (empty($children)) {
29-
$children = [new PlainTextInlineNode($value)];
32+
if (is_string($children)) {
3033
Deprecation::trigger(
3134
'phpdocumentor/guides',
3235
'https://github.com/phpDocumentor/guides/issues/1161',
33-
'Please provide the children as an array of InlineNodeInterface instances instead of a string.',
36+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: array $children',
37+
static::class,
3438
);
39+
40+
if (func_num_args() < 2) {
41+
// compat with (string $value) signature
42+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
43+
} else {
44+
// compat with (string $value, array $children = []) signature
45+
/** @var InlineNodeInterface[] $children */
46+
$children = func_get_arg(1);
47+
}
3548
}
3649

3750
parent::__construct($children);

packages/guides/src/Nodes/Inline/HyperLinkNode.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,38 @@
1313

1414
namespace phpDocumentor\Guides\Nodes\Inline;
1515

16+
use Doctrine\Deprecations\Deprecation;
17+
18+
use function func_get_arg;
19+
use function func_num_args;
20+
use function is_string;
21+
1622
/**
1723
* Represents a link to an external source or email
1824
*/
1925
final class HyperLinkNode extends AbstractLinkInlineNode
2026
{
2127
/** @param InlineNodeInterface[] $children */
22-
public function __construct(string $value, string $targetReference, array $children = [])
28+
public function __construct(string|array $children, string $targetReference)
2329
{
24-
parent::__construct('link', $targetReference, $value, $children);
30+
if (is_string($children)) {
31+
Deprecation::trigger(
32+
'phpdocumentor/guides',
33+
'https://github.com/phpDocumentor/guides/issues/1161',
34+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: array $children, string $targetReference',
35+
static::class,
36+
);
37+
38+
if (func_num_args() < 3) {
39+
// compat with (string $value, string $targetReference) signature
40+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
41+
} else {
42+
// compat with (string $value, string $targetReference, array $children = []) signature
43+
/** @var InlineNodeInterface[] $children */
44+
$children = func_get_arg(2);
45+
}
46+
}
47+
48+
parent::__construct('link', $targetReference, $children);
2549
}
2650
}

packages/guides/src/Nodes/Inline/ReferenceNode.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
namespace phpDocumentor\Guides\Nodes\Inline;
1515

16+
use Doctrine\Deprecations\Deprecation;
1617
use phpDocumentor\Guides\Nodes\SectionNode;
1718

1819
use function array_merge;
20+
use function is_string;
1921

2022
/**
2123
* CrossReferences are references outside a document. As parsing is file based normal references are in document,
@@ -32,14 +34,26 @@ final class ReferenceNode extends AbstractLinkInlineNode implements CrossReferen
3234
{
3335
final public const TYPE = 'ref';
3436

37+
/** @param InlineNodeInterface[] $children */
3538
public function __construct(
3639
string $targetReference,
37-
string $value = '',
40+
string|array $children = [],
3841
private readonly string $interlinkDomain = '',
3942
private readonly string $linkType = SectionNode::STD_LABEL,
4043
private readonly string $prefix = '',
4144
) {
42-
parent::__construct(self::TYPE, $targetReference, $value);
45+
if (is_string($children)) {
46+
Deprecation::trigger(
47+
'phpdocumentor/guides',
48+
'https://github.com/phpDocumentor/guides/issues/1161',
49+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: string $targetReference, $children, string $interlinkDomain, string $linkType, string $prefix',
50+
static::class,
51+
);
52+
53+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
54+
}
55+
56+
parent::__construct(self::TYPE, $targetReference, $children);
4357
}
4458

4559
public function getLinkType(): string

packages/guides/src/Nodes/Inline/StrongInlineNode.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
use Doctrine\Deprecations\Deprecation;
1717
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
1818

19+
use function func_get_arg;
20+
use function func_num_args;
21+
use function is_string;
22+
1923
final class StrongInlineNode extends InlineCompoundNode
2024
{
2125
use BCInlineNodeBehavior;
2226

2327
public const TYPE = 'strong';
2428

2529
/** @param InlineNodeInterface[] $children */
26-
public function __construct(string $value, array $children = [])
30+
public function __construct(string|array $children = [])
2731
{
28-
if (empty($children)) {
29-
$children = [new PlainTextInlineNode($value)];
32+
if (is_string($children)) {
3033
Deprecation::trigger(
3134
'phpdocumentor/guides',
3235
'https://github.com/phpDocumentor/guides/issues/1161',
33-
'Please provide the children as an array of InlineNodeInterface instances instead of a string.',
36+
'Passing the content of %s as string is deprecated, pass an array of InlineNodeInterface instances instead. New signature: array $children',
37+
static::class,
3438
);
39+
40+
if (func_num_args() < 2) {
41+
// compat with (string $value) signature
42+
$children = $children === '' ? [] : [new PlainTextInlineNode($children)];
43+
} else {
44+
// compat with (string $value, array $children = []) signature
45+
/** @var InlineNodeInterface[] $children */
46+
$children = func_get_arg(1);
47+
}
3548
}
3649

3750
parent::__construct($children);

0 commit comments

Comments
 (0)