Skip to content

Commit 17e96c2

Browse files
committed
Automated code improvements
1 parent bf0d7cf commit 17e96c2

35 files changed

+92
-127
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ vendor: composer.json composer.lock
7171

7272
.PHONY: rector
7373
rector: ## Refactor code using rector
74-
$(PHP_BIN) vendor/bin/rector process packages
74+
$(PHP_BIN) vendor/bin/rector process
7575

7676
.PHONY: pre-commit-test
7777
pre-commit-test: fix-code-style test code-style static-code-analysis

packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testHasBeenCalledReturnsFalseByDefault(): void
1212
{
1313
$spyProcessor = new SpyProcessor();
1414

15-
$this->assertFalse($spyProcessor->hasBeenCalled());
15+
self::assertFalse($spyProcessor->hasBeenCalled());
1616
}
1717

1818
public function testItKnowsWhenALogIsEmitted(): void

packages/guides-graphs/tests/unit/Nodes/UmlNodeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function test_it_can_be_created_with_a_value(): void
1212
{
1313
$node = new UmlNode('value');
1414

15-
$this->assertSame('value', $node->getValue());
15+
self::assertSame('value', $node->getValue());
1616
}
1717

1818
public function test_you_can_set_a_caption_for_underneath_diagrams(): void
@@ -22,7 +22,7 @@ public function test_you_can_set_a_caption_for_underneath_diagrams(): void
2222
$node = new UmlNode('value');
2323
$node->setCaption($caption);
2424

25-
$this->assertSame($caption, $node->getCaption());
25+
self::assertSame($caption, $node->getCaption());
2626
}
2727

2828
public function test_you_can_pass_classes_for_in_templates(): void
@@ -32,7 +32,7 @@ public function test_you_can_pass_classes_for_in_templates(): void
3232
$node = new UmlNode('value');
3333
$node->setClasses($classes);
3434

35-
$this->assertSame($classes, $node->getClasses());
36-
$this->assertSame('float-left my-class', $node->getClassesString());
35+
self::assertSame($classes, $node->getClasses());
36+
self::assertSame('float-left my-class', $node->getClassesString());
3737
}
3838
}

packages/guides-restructured-text/src/RestructuredText/Parser/BlockContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class BlockContext
2222
{
23-
private LinesIterator $documentIterator;
23+
private readonly LinesIterator $documentIterator;
2424

2525
public function __construct(
2626
private readonly DocumentParserContext $documentParserContext,
@@ -50,6 +50,6 @@ public function getLoggerInformation(): array
5050
'currentLineNumber' => $this->lineOffset + $this->documentIterator->key(),
5151
];
5252

53-
return array_merge($this->getDocumentParserContext()->getLoggerInformation(), $info);
53+
return [...$this->documentParserContext->getLoggerInformation(), ...$info];
5454
}
5555
}

packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class DocumentParserContext
3131
public bool $nextIndentedBlockShouldBeALiteralBlock = false;
3232

3333
public DocumentNode|null $document = null;
34-
34+
3535
private int $currentTitleLevel;
3636
/* Each Document has its own text role factory as text roles can be changed on a per document base
3737
by directives */
38-
private TextRoleFactory $textRoleFactoryForDocument;
39-
38+
private readonly TextRoleFactory $textRoleFactoryForDocument;
39+
4040
private string $codeBlockDefaultLanguage = '';
4141

4242
/** @var string[] */
@@ -79,7 +79,7 @@ public function setDocument(DocumentNode $document): void
7979
{
8080
$this->document = $document;
8181
}
82-
82+
8383
public function getLevel(string $overlineLetter, string $underlineLetter): int
8484
{
8585
$letter = $overlineLetter . ':' . $underlineLetter;
@@ -99,7 +99,7 @@ public function getTextRoleFactoryForDocument(): TextRoleFactory
9999
{
100100
return $this->textRoleFactoryForDocument;
101101
}
102-
102+
103103
public function getCodeBlockDefaultLanguage(): string
104104
{
105105
return $this->codeBlockDefaultLanguage;
@@ -109,7 +109,7 @@ public function setCodeBlockDefaultLanguage(string $codeBlockDefaultLanguage): v
109109
{
110110
$this->codeBlockDefaultLanguage = $codeBlockDefaultLanguage;
111111
}
112-
112+
113113
/** @return array<string, string> */
114114
public function getLoggerInformation(): array
115115
{
@@ -120,6 +120,6 @@ public function getLoggerInformation(): array
120120
$info['documentNode'] = 'null';
121121
}
122122

123-
return array_merge($this->getContext()->getLoggerInformation(), $info);
123+
return [...$this->context->getLoggerInformation(), ...$info];
124124
}
125125
}

packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -132,48 +132,21 @@ protected function getType(string &$value)
132132
return self::WHITESPACE;
133133
}
134134

135-
switch ($value) {
136-
case '`':
137-
return self::BACKTICK;
138-
139-
case '**':
140-
return self::STRONG_DELIMITER;
141-
142-
case '*':
143-
return self::EMPHASIS_DELIMITER;
144-
145-
case '|':
146-
return self::VARIABLE_DELIMITER;
147-
148-
case '<':
149-
return self::EMBEDED_URL_START;
150-
151-
case '>':
152-
return self::EMBEDED_URL_END;
153-
154-
case '_':
155-
return self::UNDERSCORE;
156-
157-
case '__':
158-
return self::ANONYMOUS_END;
159-
160-
case ':':
161-
return self::COLON;
162-
163-
case '#':
164-
return self::OCTOTHORPE;
165-
166-
case '[':
167-
return self::ANNOTATION_START;
168-
169-
case ']':
170-
return self::ANNOTATION_END;
171-
172-
case '~':
173-
return self::NBSP;
174-
175-
default:
176-
return self::WORD;
177-
}
135+
return match ($value) {
136+
'`' => self::BACKTICK,
137+
'**' => self::STRONG_DELIMITER,
138+
'*' => self::EMPHASIS_DELIMITER,
139+
'|' => self::VARIABLE_DELIMITER,
140+
'<' => self::EMBEDED_URL_START,
141+
'>' => self::EMBEDED_URL_END,
142+
'_' => self::UNDERSCORE,
143+
'__' => self::ANONYMOUS_END,
144+
':' => self::COLON,
145+
'#' => self::OCTOTHORPE,
146+
'[' => self::ANNOTATION_START,
147+
']' => self::ANNOTATION_END,
148+
'~' => self::NBSP,
149+
default => self::WORD,
150+
};
178151
}
179152
}

packages/guides-restructured-text/src/RestructuredText/Parser/InlineParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class InlineParser
2020
public function __construct(iterable $inlineRules)
2121
{
2222
$this->rules = [...$inlineRules];
23-
usort($this->rules, static fn(InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
23+
usort($this->rules, static fn (InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
2424
}
2525

2626
public function parse(string $content, BlockContext $blockContext): InlineCompoundNode

packages/guides-restructured-text/src/RestructuredText/Parser/LineChecker.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44

55
namespace phpDocumentor\Guides\RestructuredText\Parser;
66

7-
use phpDocumentor\Guides\Nodes\Lists\ListItem;
8-
97
use function in_array;
108
use function mb_strlen;
11-
use function preg_match;
12-
use function strlen;
13-
use function trim;
149

1510
class LineChecker
1611
{
@@ -60,6 +55,7 @@ public static function isSpecialLine(string $line, int $minimumLength = 2): stri
6055
if (!in_array($letter, self::HEADER_LETTERS, true)) {
6156
return null;
6257
}
58+
6359
$max = mb_strlen($line);
6460
for ($i = 1; $i < $max; $i++) {
6561
if ($line[$i] !== $letter) {

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/BlockQuoteRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
use function count;
2424
use function max;
2525
use function mb_strlen;
26-
use function mb_strpos;
2726
use function str_repeat;
27+
use function str_starts_with;
2828
use function strlen;
2929
use function substr;
3030
use function trim;

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/FieldList/AuthorsFieldListItemRule.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use phpDocumentor\Guides\Nodes\RawNode;
1414
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
1515

16-
use function assert;
1716
use function count;
1817
use function explode;
1918
use function str_contains;
@@ -50,7 +49,6 @@ public function apply(FieldListItemNode $fieldListItemNode, BlockContext $blockC
5049
}
5150
}
5251

53-
5452
if ($firstChild instanceof ListNode) {
5553
// A bullet list whose elements each contain a single paragraph per author.
5654
foreach ($firstChild->getChildren() as $listItemNode) {

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/ParagraphRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions;
1515

1616
use phpDocumentor\Guides\Nodes\CompoundNode;
17-
use phpDocumentor\Guides\Nodes\Node;
1817
use phpDocumentor\Guides\Nodes\ParagraphNode;
1918
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
2019
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/SectionRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use phpDocumentor\Guides\Nodes\CompoundNode;
88
use phpDocumentor\Guides\Nodes\DocumentNode;
9-
use phpDocumentor\Guides\Nodes\Node;
109
use phpDocumentor\Guides\Nodes\SectionNode;
1110
use phpDocumentor\Guides\Nodes\TitleNode;
1211
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
use function array_reverse;
1919
use function count;
2020
use function mb_strlen;
21-
use function mb_strpos;
2221
use function mb_substr;
2322
use function preg_match;
2423
use function sprintf;
24+
use function str_contains;
2525
use function str_repeat;
2626
use function trim;
2727

packages/guides-restructured-text/src/RestructuredText/TextRoles/AbstractReferenceTextRole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/** @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#embedded-uris-and-aliases */
1616
abstract class AbstractReferenceTextRole implements TextRole
1717
{
18-
private InlineLexer $lexer;
18+
private readonly InlineLexer $lexer;
1919

2020
public function __construct(
2121
private readonly LoggerInterface $logger,

packages/guides-restructured-text/src/RestructuredText/TextRoles/DefaultTextRoleFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(
1919
private readonly TextRole $genericTextRole,
2020
private TextRole $defaultTextRole,
2121
iterable $textRoles = [],
22-
private array $domains = [],
22+
private readonly array $domains = [],
2323
) {
2424
$this->textRoles = [...$textRoles];
2525
}
@@ -71,7 +71,7 @@ private function findTextRole(array $textRoles, string $name): TextRole
7171

7272
// Textrole name takes precedence over alias
7373
foreach ($textRoles as $textRole) {
74-
if (in_array($name, $textRole->getAliases())) {
74+
if (in_array($name, $textRole->getAliases(), true)) {
7575
return $textRole;
7676
}
7777
}

packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function testErrorMultipleHeaderRows(): void
295295

296296
public function testNotEndingWithWhiteLine(): never
297297
{
298-
$this->markTestSkipped('Not correct yet');
298+
self::markTestSkipped('Not correct yet');
299299
$input = <<<'RST'
300300
+-----------------------------------+---------------+
301301
| Property | Data Type |

packages/guides-restructured-text/tests/unit/Parser/Productions/InlineRules/TextRoleRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(
8080
}
8181
};
8282

83-
$textRoleFactory->expects($this->once())
83+
$textRoleFactory->expects(self::once())
8484
->method('getTextRole')
8585
->with('role', $expectedDomain)
8686
->willReturn($collectingRole);
@@ -106,16 +106,16 @@ public function __construct(
106106
* @psalm-suppress UndefinedPropertyFetch
107107
* @phpstan-ignore-next-line
108108
*/
109-
$this->assertSame($expectedRole, $node->role);
109+
self::assertSame($expectedRole, $node->role);
110110
/**
111111
* @psalm-suppress UndefinedPropertyFetch
112112
* @phpstan-ignore-next-line
113113
*/
114-
$this->assertSame($expectedContent, $node->content);
114+
self::assertSame($expectedContent, $node->content);
115115
/**
116116
* @psalm-suppress UndefinedPropertyFetch
117117
* @phpstan-ignore-next-line
118118
*/
119-
$this->assertSame($expectedRawContent, $node->rawContent);
119+
self::assertSame($expectedRawContent, $node->rawContent);
120120
}
121121
}

packages/guides/src/Compiler/CompilerContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ public function getShadowTree(): TreeNode
7575
/** @return array<string, string> */
7676
public function getLoggerInformation(): array
7777
{
78-
return array_merge($this->getDocumentNode()->getLoggerInformation());
78+
return [...$this->getDocumentNode()->getLoggerInformation()];
7979
}
8080
}

packages/guides/src/Compiler/NodeTransformers/CustomNodeTransformerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getPriorities(): array
2929
{
3030
$transformers = [...$this->transformers];
3131
$priorites = array_map(
32-
static fn(NodeTransformer $transformer): int => $transformer->getPriority(),
32+
static fn (NodeTransformer $transformer): int => $transformer->getPriority(),
3333
$transformers,
3434
);
3535
sort($priorites);

packages/guides/src/Compiler/NodeTransformers/DocumentBlockNodeTransformer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use phpDocumentor\Guides\Nodes\Menu\TocNode;
1111
use phpDocumentor\Guides\Nodes\Node;
1212

13-
use function array_merge;
14-
1513
/**
1614
* @implements NodeTransformer<Node>
1715
*

packages/guides/src/NodeRenderers/PreRenderers/PreNodeRendererFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
final class PreNodeRendererFactory implements NodeRendererFactory
1717
{
1818
public function __construct(
19-
private NodeRendererFactory $innerFactory,
19+
private readonly NodeRendererFactory $innerFactory,
2020
/** @var iterable<PreNodeRenderer> */
21-
private iterable $preRenderers,
21+
private readonly iterable $preRenderers,
2222
) {
2323
}
2424

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ abstract class AbstractLinkInlineNode extends InlineNode implements LinkInlineNo
88
{
99
private string $url = '';
1010

11-
public function __construct(string $type, private string $targetReference, string $value = '')
11+
public function __construct(string $type, private readonly string $targetReference, string $value = '')
1212
{
1313
parent::__construct($type, $value);
1414
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class DocReferenceNode extends AbstractLinkInlineNode implements CrossReferenceNode
1818
{
19-
public const TYPE = 'doc';
19+
final public const TYPE = 'doc';
2020

2121
public function __construct(
2222
string $targetDocument,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
class GenericTextRoleInlineNode extends InlineNode
88
{
9-
public const TYPE = 'role';
10-
119
public function __construct(private readonly string $role, private readonly string $content, private readonly string $class = '')
1210
{
1311
parent::__construct($role, $content);

0 commit comments

Comments
 (0)