-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Add PhpDoc check #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jean-Beru
merged 13 commits into
sensiolabs:1.x
from
StevenRenaux:features/Add-phpdoc-links-check
Nov 25, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba1986f
Add PhpDoc check
StevenRenaux b6e8852
Update after reviews + fix invalids URLs
StevenRenaux be9325e
Fix CI
StevenRenaux daaba43
Parallel Client request
StevenRenaux 92fb666
Fix review
StevenRenaux 4f6f86b
Add URL check in dagger CI
StevenRenaux a3b11a4
Add URL test in dagger CI
StevenRenaux 89df432
Add status code check
StevenRenaux ebc7df7
Add regex101 links
StevenRenaux ff400d1
Fix reviews
StevenRenaux eaef435
Fix multiplexing + some URLS
StevenRenaux d646abd
Fix reviews
StevenRenaux 7cc4683
Remove amqp
StevenRenaux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| name: 'Doc URLS test' | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ 'main', '1.x' ] | ||
| pull_request: | ||
| branches: [ 'main', '1.x' ] | ||
| schedule: | ||
| - cron: '0 1 1 * *' | ||
|
|
||
| jobs: | ||
|
|
||
| check: | ||
| name: 'Validate URL docs' | ||
| runs-on: 'ubuntu-latest' | ||
| steps: | ||
| - uses: 'actions/checkout@v4' | ||
|
|
||
| - name: 'Validate URLS into PhpDoc' | ||
| uses: 'dagger/dagger-for-github@8.0.0' | ||
| with: | ||
| version: 'latest' | ||
| call: 'test validate-url-doc' | ||
| dagger-flags: '--silent' | ||
| env: | ||
Neirda24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DAGGER_NO_NAG: '1' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| use Symfony\Component\Console\Application; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\ProgressIndicator; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\DomCrawler\Crawler; | ||
| use Symfony\Component\HttpClient\HttpClient; | ||
|
|
||
| require_once \dirname(__DIR__).'/vendor/autoload.php'; | ||
|
|
||
| class ValidateUrlDoc | ||
| { | ||
| private const AVAILABLE_EXTENSIONS = ['php', 'md']; | ||
|
|
||
| private const DIR_AND_FILES_TO_CHECK = [ | ||
| __DIR__.'/../src/Builder/Pdf', | ||
| __DIR__.'/../src/Builder/Screenshot', | ||
| __DIR__.'/../src/Builder/Behaviors', | ||
| __DIR__, | ||
| __DIR__.'/../README.md', | ||
| __DIR__.'/../src/DependencyInjection/Configuration.php', | ||
| ]; | ||
|
|
||
| public function validate(OutputInterface $output, SymfonyStyle $io): int | ||
| { | ||
| $progressBar = new ProgressIndicator($output); | ||
| $progressBar->start('Processing...'); | ||
|
|
||
| $urls = []; | ||
| $files = $this->getFiles(self::DIR_AND_FILES_TO_CHECK); | ||
| foreach ($files as $file) { | ||
| array_push($urls, ...$this->extractUrls($file)); | ||
Neirda24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $urls = array_unique($urls); | ||
StevenRenaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $client = HttpClient::create(); | ||
| $allResponses = []; | ||
| foreach ($urls as $url) { | ||
| $allResponses[] = $client->request('GET', $url); | ||
| } | ||
|
|
||
| $failedUrls = []; | ||
| foreach ($allResponses as $response) { | ||
| try { | ||
| $url = $response->getInfo('url'); | ||
|
|
||
| $statusCode = $response->getStatusCode(); | ||
| if (200 !== $statusCode) { | ||
| $failedUrls[$url] = "HTTP {$statusCode} error for: {$url}"; | ||
| } | ||
|
|
||
| if (!\array_key_exists($url, $failedUrls)) { | ||
| $checkError = $this->checkContentResponse($response->getInfo('url'), $response->getContent()); | ||
| if (\is_string($checkError)) { | ||
| $failedUrls[$url] = $checkError; | ||
| } | ||
| } | ||
| } catch (Throwable $e) { | ||
| $url = $response->getInfo('url'); | ||
| $failedUrls[$url] = "Error for {$url}: {$e->getMessage()}"; | ||
| } finally { | ||
| $progressBar->advance(); | ||
| } | ||
| } | ||
|
|
||
| $progressBar->finish('Finished'); | ||
| $failedUrls = array_unique($failedUrls); | ||
|
|
||
| if (\count($failedUrls) > 0) { | ||
| $io->error('Some external links are invalid:'); | ||
| $io->listing($failedUrls); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $io->success('All external links are valid.'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| private function getFiles(array $paths): Generator | ||
| { | ||
| foreach ($paths as $path) { | ||
| if (is_file($path)) { | ||
| $splInfo = new SplFileInfo($path); | ||
| if (!\in_array($splInfo->getExtension(), self::AVAILABLE_EXTENSIONS, true)) { | ||
| throw new RuntimeException(\sprintf('File "%s" with "%s" extension is not allowed.', $splInfo->getFilename(), $splInfo->getExtension())); | ||
| } | ||
|
|
||
| yield $splInfo; | ||
| continue; | ||
| } | ||
|
|
||
| $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); | ||
| foreach ($iterator as $file) { | ||
| if ($file->isFile() && \in_array(pathinfo($file, \PATHINFO_EXTENSION), self::AVAILABLE_EXTENSIONS, true)) { | ||
| yield $file; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function extractUrls(SplFileInfo $file): array | ||
| { | ||
| $content = file_get_contents($file->getPathname()); | ||
|
|
||
| // https://regex101.com/r/t5uiUp/1 | ||
| // https://regex101.com/r/p62cij/1 | ||
| match ($file->getExtension()) { | ||
| 'php' => preg_match_all('/(?:@see\s+|->info\([^)]*)(https?:\/\/[^\s\'")]+)(?:[^)]*\))?/', $content, $matches), | ||
| 'md' => preg_match_all('/\[[^\]]+\]\((https?:\/\/[^\s\*)]+)\)/', $content, $matches), | ||
StevenRenaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| return $matches[1] ?? []; | ||
| } | ||
|
|
||
| private function checkContentResponse(string $url, string $content): string|null | ||
| { | ||
| $crawler = new Crawler($content); | ||
| $parsedUrl = parse_url($url); | ||
|
|
||
| if (\array_key_exists('fragment', $parsedUrl)) { | ||
| $fragment = $parsedUrl['fragment']; | ||
| if ($crawler->filter('#'.$fragment)->count() > 0 || $crawler->filter('a[name="'.$fragment.'"]')->count() > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return \sprintf('Cannot find anchor "%s" for "%s", remove or update the link in the PHPdoc', $fragment, $url); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| $application = new Application(); | ||
| $application->register('check') | ||
| ->setCode(function (OutputInterface $output, SymfonyStyle $io) { | ||
| return (new ValidateUrlDoc())->validate($output, $io); | ||
| }) | ||
| ; | ||
|
|
||
| $application->setDefaultCommand('check'); | ||
| $application->run(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.