Skip to content

Commit 54836c6

Browse files
committed
A little code improvements
1 parent 3f15d84 commit 54836c6

File tree

9 files changed

+54
-50
lines changed

9 files changed

+54
-50
lines changed

Diff for: src/DocBlock.php

-30
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,6 @@ public function __construct(
4646
$this->bootTagProvider($tags);
4747
}
4848

49-
public function offsetExists(mixed $offset): bool
50-
{
51-
return isset($this->tags[$offset]);
52-
}
53-
54-
public function offsetGet(mixed $offset): ?TagInterface
55-
{
56-
return $this->tags[$offset] ?? null;
57-
}
58-
59-
/**
60-
* {@inheritDoc}
61-
*
62-
* @throws \BadMethodCallException
63-
*/
64-
public function offsetSet(mixed $offset, mixed $value): void
65-
{
66-
throw new \BadMethodCallException(self::class . ' objects are immutable');
67-
}
68-
69-
/**
70-
* {@inheritDoc}
71-
*
72-
* @throws \BadMethodCallException
73-
*/
74-
public function offsetUnset(mixed $offset): void
75-
{
76-
throw new \BadMethodCallException(self::class . ' objects are immutable');
77-
}
78-
7949
public function getDescription(): DescriptionInterface
8050
{
8151
return $this->description;

Diff for: src/Parser.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
namespace TypeLang\PHPDoc;
66

77
use JetBrains\PhpStorm\Language;
8-
use TypeLang\PHPDoc\Exception\InvalidTagException;
9-
use TypeLang\PHPDoc\Exception\InvalidTagNameException;
108
use TypeLang\PHPDoc\Exception\ParsingException;
119
use TypeLang\PHPDoc\Exception\RuntimeExceptionInterface;
1210
use TypeLang\PHPDoc\Parser\Comment\CommentParserInterface;
1311
use TypeLang\PHPDoc\Parser\Comment\RegexCommentParser;
1412
use TypeLang\PHPDoc\Parser\Comment\Segment;
1513
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
16-
use TypeLang\PHPDoc\Parser\Description\SprintfDescriptionReader;
14+
use TypeLang\PHPDoc\Parser\Description\SprintfDescriptionParser;
1715
use TypeLang\PHPDoc\Parser\SourceMap;
18-
use TypeLang\PHPDoc\Parser\Tag\TagParser;
16+
use TypeLang\PHPDoc\Parser\Tag\RegexTagParser;
1917
use TypeLang\PHPDoc\Parser\Tag\TagParserInterface;
2018
use TypeLang\PHPDoc\Tag\Factory\FactoryInterface;
2119
use TypeLang\PHPDoc\Tag\Factory\TagFactory;
@@ -31,8 +29,8 @@ class Parser implements ParserInterface
3129
public function __construct(
3230
FactoryInterface $tags = new TagFactory(),
3331
) {
34-
$this->tags = new TagParser($tags);
35-
$this->descriptions = new SprintfDescriptionReader($this->tags);
32+
$this->tags = new RegexTagParser($tags);
33+
$this->descriptions = new SprintfDescriptionParser($this->tags);
3634
$this->comments = new RegexCommentParser();
3735
}
3836

Diff for: src/Parser/Comment/Segment.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44

55
namespace TypeLang\PHPDoc\Parser\Comment;
66

7-
final class Segment implements \Stringable
7+
final class Segment
88
{
99
/**
1010
* @param int<0, max> $offset
1111
*/
1212
public function __construct(
13-
public string $text = '',
14-
public int $offset = 0,
13+
public readonly string $text = '',
14+
public readonly int $offset = 0,
1515
) {}
16-
17-
public function __toString(): string
18-
{
19-
return $this->text;
20-
}
2116
}

Diff for: src/Parser/Description/DescriptionParser.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace TypeLang\PHPDoc\Parser\Description;
66

7+
use TypeLang\PHPDoc\Parser\Tag\RegexTagParser;
78
use TypeLang\PHPDoc\Parser\Tag\TagParserInterface;
89
use TypeLang\PHPDoc\Tag\Description;
910

1011
abstract class DescriptionParser implements DescriptionParserInterface
1112
{
1213
public function __construct(
13-
private readonly TagParserInterface $tags,
14+
private readonly TagParserInterface $tags = new RegexTagParser(),
1415
) {}
1516

1617
public function parse(string $description): Description

Diff for: src/Parser/Description/SprintfDescriptionReader.php renamed to src/Parser/Description/SprintfDescriptionParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace TypeLang\PHPDoc\Parser\Description;
66

7-
final class SprintfDescriptionReader extends DescriptionParser
7+
final class SprintfDescriptionParser extends DescriptionParser
88
{
99
/**
1010
* @param int<0, max> $tagId

Diff for: src/Parser/Tag/TagParser.php renamed to src/Parser/Tag/RegexTagParser.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
1010
use TypeLang\PHPDoc\Tag\Factory\FactoryInterface;
1111
use TypeLang\PHPDoc\Tag\Content;
12+
use TypeLang\PHPDoc\Tag\Factory\TagFactory;
1213
use TypeLang\PHPDoc\Tag\InvalidTag;
1314
use TypeLang\PHPDoc\Tag\TagInterface;
1415

15-
final class TagParser implements TagParserInterface
16+
final class RegexTagParser implements TagParserInterface
1617
{
1718
/**
1819
* @var non-empty-string
1920
*/
2021
private const PATTERN_TAG = '\G@[a-zA-Z_\x80-\xff\\\][\w\x80-\xff\-:\\\]*';
2122

2223
public function __construct(
23-
private readonly FactoryInterface $tags,
24+
private readonly FactoryInterface $tags = new TagFactory(),
2425
) {}
2526

2627
/**

Diff for: src/Tag/Description.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
/**
88
* @template-implements \IteratorAggregate<int<0, max>, TagInterface>
9+
* @template-implements \ArrayAccess<int<0, max>, TagInterface|null>
910
*/
10-
class Description implements DescriptionInterface, \IteratorAggregate
11+
class Description implements DescriptionInterface, \IteratorAggregate, \ArrayAccess
1112
{
1213
use TagsProvider;
1314

Diff for: src/Tag/Factory/TagFactory.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function create(string $name, Content $content, DescriptionParserInterfac
5555
}
5656
}
5757

58-
return new Tag($name, $content->toDescription($descriptions));
58+
$description = null;
59+
60+
if ($content->value !== '') {
61+
$description = $content->toDescription($descriptions);
62+
}
63+
64+
return new Tag($name, $description);
5965
}
6066
}

Diff for: src/Tag/TagsProvider.php

+32
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
/**
88
* @mixin TagsProviderInterface
99
* @mixin \IteratorAggregate
10+
* @mixin \ArrayAccess
1011
*
1112
* @psalm-require-implements TagsProviderInterface
1213
* @psalm-require-implements \IteratorAggregate
14+
* @psalm-require-implements \ArrayAccess
1315
*
1416
* @internal This is an internal library trait, please do not use it in your code.
1517
* @psalm-internal TypeLang\PHPDoc\Tag
@@ -41,6 +43,36 @@ public function getTags(): array
4143
return $this->tags;
4244
}
4345

46+
public function offsetExists(mixed $offset): bool
47+
{
48+
return isset($this->tags[$offset]);
49+
}
50+
51+
public function offsetGet(mixed $offset): ?TagInterface
52+
{
53+
return $this->tags[$offset] ?? null;
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*
59+
* @throws \BadMethodCallException
60+
*/
61+
public function offsetSet(mixed $offset, mixed $value): void
62+
{
63+
throw new \BadMethodCallException(self::class . ' objects are immutable');
64+
}
65+
66+
/**
67+
* {@inheritDoc}
68+
*
69+
* @throws \BadMethodCallException
70+
*/
71+
public function offsetUnset(mixed $offset): void
72+
{
73+
throw new \BadMethodCallException(self::class . ' objects are immutable');
74+
}
75+
4476
/**
4577
* @return \Traversable<int<0, max>, TagInterface>
4678
*/

0 commit comments

Comments
 (0)