-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllExamples.php
More file actions
executable file
·153 lines (128 loc) · 4.15 KB
/
AllExamples.php
File metadata and controls
executable file
·153 lines (128 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php declare(strict_types=1);
namespace TH\DocTest\Iterator;
use TH\DocTest\Example;
use TH\DocTest\Location;
use TH\Maybe\Option;
final class AllExamples implements Examples
{
/**
* @param Option<array<string>> $languageFilter Use empty string for unspecified language
*/
public function __construct(
private readonly Comments $comments,
private readonly Option $languageFilter,
) {}
/**
* @return \Traversable<Example>
*/
public function getIterator(): \Traversable
{
foreach ($this->comments as $location => $comment) {
yield from $this->iterateComment($comment, $location);
}
}
/**
* @param array<string> $paths paths to files and folder to look for PHP comments code examples in
* @param Option<array<string>> $languageFilter Use empty string for unspecified language
*/
public static function fromPaths(
array $paths,
Option $languageFilter,
): self {
return new self(
SourceComments::fromPaths($paths),
$languageFilter,
);
}
/**
* @return \Traversable<Example>
*/
private function iterateComment(
string $comment,
Location $location,
): \Traversable {
$lines = new \ArrayIterator(\explode(PHP_EOL, $comment));
$index = 1;
while (($example = $this->nextExample($lines, $location, $index++))->isSome()) {
yield $example->unwrap();
}
}
/**
* @param \ArrayIterator<int,string> $lines
* @return Option<Example>
*/
private function nextExample(\ArrayIterator $lines, Location $location, int $index): Option
{
return $this->findFencedCodeBlockStart($lines)->andThen(
fn (int $codeblockStartedAt)
=> $this->readExample($lines, $location->startingAt($codeblockStartedAt, $index)),
);
}
/**
* @param \ArrayIterator<int,string> $lines
* @return Option<Example>
*/
private function readExample(\ArrayIterator $lines, Location $location): Option
{
$buffer = [];
while ($lines->valid()) {
$line = $lines->current();
$lines->next();
if ($this->endOfAFencedCodeBlock($line)) {
return Option\some(new Example(\implode(PHP_EOL, $buffer), $location->ofLength($lines->key())));
}
$buffer[] = \preg_replace("/^\s*\*( ?)/", "", $line);
}
return Option\none();
}
/**
* @param \ArrayIterator<int,string> $lines
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
* @return Option<int>
*/
private function findFencedCodeBlockStart(\ArrayIterator $lines): Option
{
$insideAFencedCodeBlock = false;
while ($lines->valid()) {
$line = $lines->current();
$lines->next();
if ($insideAFencedCodeBlock) {
if ($this->endOfAFencedCodeBlock($line)) {
$insideAFencedCodeBlock = false;
}
} else {
$lang = $this->startOfAFencedCodeBlock($line);
if ($lang->mapOr($this->isAcceptedLanguage(...), default: false)) {
return Option\some($lines->key());
}
if ($lang->isNone()) {
continue;
}
$insideAFencedCodeBlock = true;
}
}
return Option\none();
}
private function isAcceptedLanguage(string $lang): bool
{
return $this->languageFilter->mapOr(
callback: static fn (array $languages) => \in_array(needle: $lang, haystack: $languages, strict: true),
default: true,
);
}
private function endOfAFencedCodeBlock(string $line): bool
{
return \ltrim($line) === "* ```";
}
/**
* @return Option<string>
*/
private function startOfAFencedCodeBlock(string $line): Option
{
$line = \trim($line);
if (!\str_starts_with($line, "* ```")) {
return Option\none();
}
return Option\some(\substr($line, 5));
}
}