Skip to content

Commit

Permalink
Merge pull request #548 from patchlevel/check-docs
Browse files Browse the repository at this point in the history
 docs: format & fix php errors
  • Loading branch information
DavidBadura authored Mar 22, 2024
2 parents a7ebc6e + 2c6770c commit a796a60
Show file tree
Hide file tree
Showing 28 changed files with 1,266 additions and 778 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/psalm-baseline.xml export-ignore
/test/ export-ignore
/tools/ export-ignore
/bin/ export-ignore
49 changes: 49 additions & 0 deletions .github/workflows/docs-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

name: "Check Docs"

on:
pull_request:
push:
branches:
- "[0-9]+.[0-9]+.x"

jobs:
checkdocs:
name: "Check Docs"

runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
dependencies:
- "locked"
php-version:
- "8.3"
operating-system:
- "ubuntu-latest"

steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/[email protected]"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
extensions: pdo_sqlite

- uses: ramsey/[email protected]
with:
dependency-versions: ${{ matrix.dependencies }}

- name: "extract php code"
run: "bin/docs-extract-php-code"

- name: "lint php"
run: "php -l docs_php/*.php"

- name: "docs code style"
run: "vendor/bin/phpcbf docs_php --exclude=SlevomatCodingStandard.TypeHints.DeclareStrictTypes"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ infection.log
.phpbench/
infection.html
*.sqlite3
.deptrac.cache
.deptrac.cache
docs_php/
31 changes: 29 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,32 @@ benchmark-diff-test: vendor #
dev: static test ## run dev tools

.PHONY: docs
docs: ## run mkdocs
cd docs && mkdocs serve
docs: mkdocs ## run mkdocs
cd docs && python3 -m mkdocs serve

.PHONY: mkdocs
mkdocs: ## run mkdocs
cd docs && pip3 install -r requirements.txt

.PHONY: docs-extract-php
docs-extract-php:
bin/docs-extract-php-code

.PHONY: docs-inject-php
docs-inject-php:
bin/docs-inject-php-code

.PHONY: docs-format
docs-format: docs-phpcs docs-inject-php

.PHONY: docs-php-lint
docs-php-lint: docs-extract-php
php -l docs_php/*.php | grep 'Parse error: '

.PHONY: docs-phpcs
docs-phpcs: docs-extract-php
vendor/bin/phpcbf docs_php --exclude=SlevomatCodingStandard.TypeHints.DeclareStrictTypes || true

.PHONY: docs-psalm
docs-psalm: docs-extract-php
vendor/bin/psalm --config=psalm_docs.xml
53 changes: 53 additions & 0 deletions bin/docs-extract-php-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Node\Query;
use League\CommonMark\Parser\MarkdownParser;
use Wnx\CommonmarkMarkdownRenderer\MarkdownRendererExtension;

require __DIR__ . '/../vendor/autoload.php';

$environment = new Environment([]);
$environment->addExtension(new MarkdownRendererExtension());

$parser = new MarkdownParser($environment);

$targetDir = __DIR__ . '/../docs_php';

if (file_exists($targetDir)) {
exec('rm -rf ' . $targetDir);
}

mkdir($targetDir);

$finder = new Symfony\Component\Finder\Finder();
$finder->files()->in(__DIR__ . '/../docs/pages')->name('*.md');

foreach ($finder as $file) {
$fileName = pathinfo($file->getBasename(), PATHINFO_FILENAME);

$content = file_get_contents($file->getPathname());
$document = $parser->parse($content);

$result = (new Query())
->where(Query::type(FencedCode::class))
->findAll($document);

/**
* @var FencedCode $node
*/
foreach ($result as $i => $node) {
if ($node->getInfo() !== 'php') {
continue;
}

$source = sprintf('%s:%s', $file->getRealPath(), $node->getStartLine());

$code = "<?php\n // " . $source . "\n\n" . $node->getLiteral();

$targetPath = $targetDir . '/' . $fileName . '_' . $i . '.php';
file_put_contents($targetPath, $code);
}
}
70 changes: 70 additions & 0 deletions bin/docs-inject-php-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env php
<?php

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Node\Query;
use League\CommonMark\Parser\MarkdownParser;
use Wnx\CommonmarkMarkdownRenderer\MarkdownRendererExtension;
use Wnx\CommonmarkMarkdownRenderer\Renderer\MarkdownRenderer;

require __DIR__ . '/../vendor/autoload.php';



$environment = new Environment([]);
$environment->addExtension(new MarkdownRendererExtension());

$parser = new MarkdownParser($environment);
$markdownRenderer = new MarkdownRenderer($environment);

$targetDir = __DIR__ . '/../docs_php';

if (!file_exists($targetDir)) {
exit(1);
}

$finder = new Symfony\Component\Finder\Finder();
$finder->files()->in(__DIR__ . '/../docs/pages')->name('*.md');

foreach ($finder as $file) {
$fileName = pathinfo($file->getBasename(), PATHINFO_FILENAME);

$content = file_get_contents($file->getPathname());
$document = $parser->parse($content);

$result = (new Query())
->where(Query::type(FencedCode::class))
->findAll($document);

/**
* @var FencedCode $node
*/
foreach ($result as $i => $node) {
if ($node->getInfo() !== 'php') {
$node->setLiteral(trim($node->getLiteral()));
continue;
}

$targetPath = $targetDir . '/' . $fileName . '_' . $i . '.php';

if (!file_exists($targetPath)) {
$node->setLiteral(trim($node->getLiteral()));
continue;
}

$code = file_get_contents($targetPath);

$lines = explode("\n", $code);
array_splice($lines, 0, 2);
$code = implode("\n", $lines);

$node->setLiteral(trim($code));
}

file_put_contents($file->getPathname(), $markdownRenderer->renderDocument($document));
}

if (file_exists($targetDir)) {
exec('rm -rf ' . $targetDir);
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cspray/phinal": "^2.0.0",
"doctrine/orm": "^2.18.0|^3.0.0",
"infection/infection": "^0.27.0",
"league/commonmark": "^2.4",
"patchlevel/coding-standard": "^1.3.0",
"patchlevel/event-sourcing-psalm-plugin": "^2.1.0",
"phpbench/phpbench": "^1.2.15",
Expand All @@ -48,7 +49,8 @@
"roave/infection-static-analysis-plugin": "^1.34.0",
"symfony/messenger": "^5.4.31|^6.4.0|^7.0.1",
"symfony/var-dumper": "^5.4.29|^6.4.0|^7.0.0",
"vimeo/psalm": "^5.17.0"
"vimeo/psalm": "^5.17.0",
"wnx/commonmark-markdown-renderer": "^1.4"
},
"suggest": {
"patchlevel/event-sourcing-psalm-plugin": "for psalm support"
Expand Down
Loading

0 comments on commit a796a60

Please sign in to comment.