Skip to content

Commit 1c027ff

Browse files
committed
Preserve relative phpDocumentor API paths
1 parent bf7765f commit 1c027ff

6 files changed

Lines changed: 45 additions & 35 deletions

File tree

src/Console/Command/WikiCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
179179
}
180180

181181
foreach ($projectCapabilities->getApiDirectories() as $path) {
182-
$processBuilder = $processBuilder->withArgument('--directory', $path);
182+
$processBuilder = $processBuilder->withArgument(
183+
'--directory',
184+
$this->filesystem->getAbsolutePath($path)
185+
);
183186
}
184187

185188
if (null !== $defaultPackageName = $projectCapabilities->getDefaultPackageName()) {

src/Project/ProjectCapabilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
final readonly class ProjectCapabilities
2626
{
2727
/**
28-
* @param list<string> $apiDirectories absolute directories that contain autoloaded PHP API source
28+
* @param list<string> $apiDirectories project-relative directories that contain autoloaded PHP API source
2929
* @param string|null $defaultPackageName the default package name derived from Composer namespaces when available
3030
* @param bool $hasGuideDirectory whether the configured guide directory exists
3131
* @param bool $hasTestsPath whether the configured tests path exists

src/Project/ProjectCapabilitiesResolver.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,39 @@ private function resolveApiDirectories(): array
9494

9595
foreach (self::API_AUTOLOAD_TYPES as $autoloadType) {
9696
foreach ($this->normalizeAutoloadPaths($this->composer->getAutoload($autoloadType)) as $path) {
97-
$absolutePath = $this->filesystem->getAbsolutePath($path);
97+
$relativePath = $this->resolveRelativeApiDirectory($path);
9898

99-
if (! \is_string($absolutePath)) {
99+
if (null === $relativePath) {
100100
continue;
101101
}
102102

103-
if (! is_dir($absolutePath)) {
104-
continue;
105-
}
106-
107-
$directories[$absolutePath] = $absolutePath;
103+
$directories[$relativePath] = $relativePath;
108104
}
109105
}
110106

111107
return array_values($directories);
112108
}
113109

110+
/**
111+
* @param string $path
112+
*
113+
* @return string|null
114+
*/
115+
private function resolveRelativeApiDirectory(string $path): ?string
116+
{
117+
$absolutePath = $this->filesystem->getAbsolutePath($path);
118+
119+
if (! \is_string($absolutePath)) {
120+
return null;
121+
}
122+
123+
if (! is_dir($absolutePath)) {
124+
return null;
125+
}
126+
127+
return $this->filesystem->makePathRelative($absolutePath);
128+
}
129+
114130
/**
115131
* @param array<string, mixed> $autoload
116132
*

tests/Console/Command/DocsCommandTest.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,7 @@ protected function setUp(): void
133133
$this->filesystem->exists('/repo/docs')
134134
->willReturn(true);
135135
$this->projectCapabilitiesResolver->resolve(Argument::any(), Argument::any())
136-
->willReturn(new ProjectCapabilities(
137-
['/repo/src'],
138-
'FastForward\\DevTools',
139-
true,
140-
false,
141-
false,
142-
true,
143-
));
136+
->willReturn(new ProjectCapabilities(['src/'], 'FastForward\\DevTools', true, false, false, true));
144137
$this->composer->getAutoload('psr-4')
145138
->willReturn([
146139
'FastForward\\DevTools\\' => 'src/',
@@ -153,6 +146,7 @@ protected function setUp(): void
153146
static fn(array $context): bool => DevToolsPathResolver::getPreferredVendorPath(
154147
'vendor/fast-forward/phpdoc-bootstrap-template'
155148
) === $context['template']
149+
&& ['src/'] === $context['apiDirectories']
156150
)
157151
)
158152
->willReturn('<phpdocumentor />');
@@ -208,14 +202,7 @@ public function executeWillFailWhenCustomGuideSourceDoesNotExist(): void
208202
$this->filesystem->getAbsolutePath('missing-guides')
209203
->willReturn('/repo/missing-guides');
210204
$this->projectCapabilitiesResolver->resolve(Argument::any(), Argument::any())
211-
->willReturn(new ProjectCapabilities(
212-
['/repo/src'],
213-
'FastForward\\DevTools',
214-
false,
215-
false,
216-
false,
217-
true,
218-
));
205+
->willReturn(new ProjectCapabilities(['src/'], 'FastForward\\DevTools', false, false, false, true));
219206
$this->processQueue->add(Argument::cetera())
220207
->shouldNotBeCalled();
221208
$this->logger->info('Generating API documentation...', Argument::that(

tests/Console/Command/WikiCommandTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,7 @@ protected function setUp(): void
102102
'FastForward\\DevTools\\' => 'src/',
103103
]);
104104
$this->projectCapabilitiesResolver->resolve(Argument::any(), Argument::any(), Argument::any())
105-
->willReturn(new ProjectCapabilities(
106-
[getcwd() . '/src'],
107-
'FastForward\\DevTools',
108-
false,
109-
false,
110-
true,
111-
true,
112-
));
105+
->willReturn(new ProjectCapabilities(['src/'], 'FastForward\\DevTools', false, false, true, true));
113106
$this->input->getOption('target')
114107
->willReturn('.github/wiki');
115108
$this->input->getOption('cache-dir')
@@ -160,6 +153,12 @@ public function executeWillReturnSuccessWhenProcessQueueSucceeds(): void
160153
)
161154
->willReturn($this->processBuilder->reveal())
162155
->shouldBeCalled();
156+
$this->filesystem->getAbsolutePath('src/')
157+
->willReturn(getcwd() . '/src')
158+
->shouldBeCalled();
159+
$this->processBuilder->withArgument('--directory', getcwd() . '/src')
160+
->willReturn($this->processBuilder->reveal())
161+
->shouldBeCalled();
163162
$this->processQueue->add($this->process->reveal(), Argument::cetera())
164163
->shouldBeCalled();
165164
$this->processQueue->run($this->output->reveal())
@@ -189,6 +188,12 @@ public function executeWithNoCacheWillSkipPhpDocumentorCacheFolder(): void
189188
->willReturn(true);
190189
$this->processBuilder->withArgument('--cache-folder', Argument::cetera())
191190
->shouldNotBeCalled();
191+
$this->filesystem->getAbsolutePath('src/')
192+
->willReturn(getcwd() . '/src')
193+
->shouldBeCalled();
194+
$this->processBuilder->withArgument('--directory', getcwd() . '/src')
195+
->willReturn($this->processBuilder->reveal())
196+
->shouldBeCalled();
192197
$this->processQueue->add($this->process->reveal(), Argument::cetera())
193198
->shouldBeCalled();
194199
$this->processQueue->run($this->output->reveal())

tests/Project/ProjectCapabilitiesResolverTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use function Safe\file_put_contents;
3838
use function Safe\getcwd;
3939
use function Safe\mkdir;
40-
use function Safe\realpath;
4140

4241
#[CoversClass(ProjectCapabilitiesResolver::class)]
4342
#[UsesClass(Filesystem::class)]
@@ -149,7 +148,7 @@ public function resolveWillDetectPhpPackageCapabilities(): void
149148
self::assertTrue($capabilities->canRunTests());
150149
self::assertTrue($capabilities->canGenerateMetrics());
151150
self::assertTrue($capabilities->canGenerateWiki());
152-
self::assertSame([realpath($this->workspace . '/src')], $capabilities->getApiDirectories());
151+
self::assertSame(['src/'], $capabilities->getApiDirectories());
153152
self::assertSame('App', $capabilities->getDefaultPackageName());
154153
}
155154
}

0 commit comments

Comments
 (0)